C
C#3y 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
Becquerel3y 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 User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server