quest
quest

Reputation: 107

Dependency Injection for models .net Core

I have a class where I want to populate the model class properties. It works if I create a model class instance with new keyword. But this does not satisfy Dependency injection.

We also never pass a model object in the constructor as a parameter. How do we create the request object i.e. the model inside myClass without newing it up via DI?

This scenario is around bot framework.

public class myDailog : ComponentDialog
{
    private readonly modelClass _req;
    private readonly serviceClass _sev

    public myDialog(IServiceProvider serviceProvider)
         : base(nameof(myDialog))
    {
        _sev = serviceProvider;
        modelClass _req = new modelClass();
    }

    public async Task<DialogTurnResult> method1()
    {
        req.name = "something";
        req.desc = "some description";
        // pass the req object to another service
        _sev.somemethod(req);
    }
}

Upvotes: 1

Views: 460

Answers (1)

keuleJ
keuleJ

Reputation: 3496

Model classes are normally not instanciated using Dependency Injection (DI). They are loaded from a service (read case) or created using new (create case). The service ist your depenendency that is injected via CI.

Upvotes: 1

Related Questions