Designer injection - where to call?

I'm a little confused around the constructor injection pattern and rules. Don't call the container; he will call you .

Can someone explain to me (and possibly someone else) how a real application should get all the benefits of DI using constructor injection? I give for this a few simple and, I think, a general example:

DomainObject
RepositoryObject
DaoObject

The relationship is obvious (I think) - for RepositoryObject you need the DaoObject repository, DomainObject.

Using constructor injection I assume that I can forget (in most cases) the NEW key, but when, where and how do I create new objects (mainly a domain)? Should I write factories for all classes? Should I reference the DI container in this factory?

Best of all, when someone shows me an example of a real application (please, not Asp.Net MVC :)) or draw some project structure.

+5
source share
2 answers

I do not get your relationship in the class, so here is a more obvious example :-):

class FooService
{
    IFooRepository FooRepository { get; set; }

    public Service(IFooRepository fooRepository)
    {
        this.FooRepository = fooRepository;
    }
}

class Controller
{
    IFooService FooService { get; set; }
    IBarService BarService { get; set; }

    public Controller(IFooService fooService, IBarService barService)
    {
        this.FooService = fooService;
        this.BarService = barService;
    }
}

As you have already said - the code new FooRepository()and new FooService()not in any place.

+1
source

Mark Seemann's answers and link are enough, but I want to add something. As a newcomer to DI (which I am), this question always scares me: "Well, there are no new ones, but when and how are my real objects called and introduced?". It took me a while for me to understand and apply.

, , . Global.asax -. , Ninject, nuget Ninject.Web( -) , http://azolotar.blog.com/2010/06/22/ninject-2-0-in-webforms/

Keypoint .

  • Global.asax NinjectHttpApplication ( Ninject.Web.dll)
  • CreateKernel , ​​ dependecy
  • BasePage: -, , .

, BasePage ( github), , , , KernelContainer.Inject(this); OnInit . , - ascx, ascx OnInit, .

, MVC :)

, , , .

+1

All Articles