开发者

Where is the best place to put a WindsorContainer and avoid circular project references?

开发者 https://www.devze.com 2023-04-01 20:22 出处:网络
I have a large existing solution with multiple tiers that uses CastleWindsor as a DI framework for the newer layers/projects. I am currently trying to implement some new functionality in a layer/proje

I have a large existing solution with multiple tiers that uses CastleWindsor as a DI framework for the newer layers/projects. I am currently trying to implement some new functionality in a layer/project that has no previous DI in place but I keep 开发者_JAVA百科getting "circular refernce" problems.

Given a solution like this...

  • MyCompany.Core - includes CastleWindsorConfiguration
  • MyCompany.BusinessLayer - includes entities & business logic
  • MyCompany.Data - includes NHibernate mapping & repositories
  • MyCompany.Emails - existsing non DI functionality to complement the BusinessLayer
  • MyCompany.Web - Web app fron end/display

In reality there are actually quite a few BusinessLayer supplimentry projects/layers

I cannot add a reference to MyCompany.Core in MyCompany.Emails because the CastleWindsor container in Core references the Emails project so it can be configured/mapped with the following...

_windsorContainer.Register(Component.For<IEMailPoolHandler>().ImplementedBy(typeof(EMailPoolHandler)).LifeStyle.PerWebRequest);

But I then need to resolve this dependency in the Emails project with something like the following...

private IEMailPoolHandler EMailPoolHandler
{
    get
    {
        return MyCompany.Core.WindsorContainer.Resolve<IEMailPoolHandler>();
    }
}

And here I get the circular reference error. Basically I cant reference Core because it references Emails.

So, the question really is, what is the best way to handle this kind of situation and where should I have my CastleWindsor Container? Or is it ok to have more than one? For example one in each project?


You should put it in the .Web project. I believe it's the project that is the actual application -controls lifetime and overall configuration - that should configure, instantiate and resolve from the container.

Then you should - if at all possible - inject (ctor or setter, automatically or manually) either the EmailPoolHandler or an EmailPoolHandlerFactory into the classes in the Email project, not access the container from there.


Can you not have it in it's own project and then just have everything else reference it?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号