C
C#2y ago
а

ASP NET Core default DI-Container, how pass in constructor not-registered parameter? [Answered]

I want to create a Model class using a DI container, services registered in the container are passed to its constructor, and a structure is passed that becomes known only in runtime.How can I make this model class created by the container? In Ninject, I used a Factory Interface for this, how can I implement this in a standard ASP NET Core container? Once again, I want to i can create Model like that: _modelFactory.Create(data);
public class Model
{
// ITimeService registered in container, data not registered ( creating in runtime )
public Model(ITimeService timeService, EngineData data)
{

}
}
public class Model
{
// ITimeService registered in container, data not registered ( creating in runtime )
public Model(ITimeService timeService, EngineData data)
{

}
}
3 Replies
Becquerel
Becquerel2y ago
why do you want to create the model via DI, rather than a factory class (which is itself injected where needed)? you can use the overloads of AddTransient etc to supply your own factory function (.AddTransient(x => new Model(x.GetService<ITimeService>(), data)) but I would suggest that dependency injection is really meant for injecting dependencies not every single instance of a class you might create
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Should I return the whole created object in POST or just return the id of that object ?When I creating a post method I find myself repeat what I did in GetById Should I just return the Iasp net cshtml javascript filehi all, i have an asp net project (SimpleApp) and in a cshtml file of my **SimpleApp ** project i trChange Visual Studio suggetions with custom analysersHiya.. ohh discords new "forum" channels are super useful for this huh?. anyway. So I managed to impProgram won’t run with just .net runtime [Answered]So my program that I published and is targeting .net 6 won’t run when the pc has just .net 6 runtimeNullReferenceException Object reference not set to an instance of an object```cs List<Effects> effectsList = activePlayerEffects[targetPlayer] ?? new List<Effects>(); ``` someDbContext.SaveChangesAsync has no effect on the collection of changed objectsHi. I get products collection from dbContext, change some values and want to save it in the databaseWindowsIdentity claims don't include all AD attributes, how do I fix that?I'm using windows authentication and want to display a user's full name, however on inspection the ihow to create a jwt for an existing user in identityI am looking to create a new JWT and validate it... for using tiwht signalr. so i want to know how tNeed help finding memory leak [Answered]We have a solution with a mix of VB and C# projects. Most of them in VB. We have a logger factory wrEF 6 - Creating entity with n-n```cs PostDtoWrite_2 newpost = new PostDtoWrite_2(); newpost.Title = request.Title; newp