简体   繁体   中英

how to install dependencies with castle windsor in a layered architecture

I am using Castle Windsor for ioc in an MVC project.

My architecture is basically Web -> Services -> Data

The controllers have services as dependencies. The services have repositories as dependencies in the data layer.

My Web layer does not have a reference to the Data layer. My problem is that I am trying to register my repositories which is a dependency of my services. If i have a separate container in my services layer that registers the repositories how should I bootstrap it?

Or I may be going about this in the wrong way.

Your Web layer would not have to reference the data layer if you have placed the interafces of the datalayer functional classes ( AKA Repositories ) in the Domain layer. Then you can easily depend upon single container which is initialized in the web layer.

This Question contains further details.

Refer mvc extensions for some advanced way of achieving this.

How about this. The downside is that you need to hardcode the Name of your repository dll, but you could always move this to web.config etc which would be slightly cleaner.

IWindsorContainer container = new WindsorContainer();

// Register repositories
_container.Register(
        AllTypes.Pick()
                .FromAssemblyNamed("MyDataLayerAssembly")
                .WithService
                .DefaultInterface());

// Register services
_container.Register(
        AllTypes.Pick()
                .FromAssemblyNamed(typeof(ISomeService).Assembly.GetName().Name)
                .WithService
                .DefaultInterface());

ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

You may just need to tweak precisely what's passed into the Register() methods to suit your needs however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM