2
votes

I have a WCF service which calls the business component which calls the repository and I have got it end to end working using Castle Windsor using it's WCF Facility.

The WCF Facility registration and rest of the component registration happens in the Global.asax file like this.


    public class Global : System.Web.HttpApplication
    {
        public IWindsorContainer SystemContainer;

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterDependencies();
        }

        private void RegisterDependencies()
        {
            SystemContainer = new WindsorContainer();


            SystemContainer.AddFacility<WcfFacility>().Register(
                Component.For<IBookingRepository>().ImplementedBy<BookingRepository>(),
                Component.For<IBookingBll>().ImplementedBy<BookingBll>(),
                Component.For<IBookingService>().ImplementedBy<BookingService>(),
                Component.For<ILogger>().ImplementedBy<Logger>()

                );
        }

    }

All is well but now I need to refer this container in one of my component so that I can resolve other components like this.





 public class BookingBll : IBookingBll
    {
        private IBookingRepository _repository;
        private ILogger _logger;

        public BookingBll(IBookingRepository repository)
        {
            _repository = repository;
            _logger = SystemContainer.Resolve<ILogger>(); // This will not 
            //compile as we can't access a Global class property.
        }

        public void Add(Booking booking)
        {
            //Some implementation;
            _logger.Write();
        }
    }

Moreoever I want this container to be available globally and don't want to run registration over and over again, So should I look into shuving this container in HttpRuntime.Cache so it is available and any component can simple get it from Cache and resolve what ever interface they want to.

Pardon my ignorance as I am new to WCF and as well Castle Windsor and the whole architecture has been shuved down my throat with a steep deadline :-(

2
what did you end up doing here? I'm in the same situation.g.foley

2 Answers

0
votes

You can have a dependency on IKernel, it's automatically provided by Windsor.

 public BookingBll(IKernel kernel, IBookingRepository repository) {}

If you really, really need IWindsorContainer you can register it in the container , self-registration if you wish :-)

SystemContainer.Register(Component.For<IWindsorContainer>.Instance(SystemContainer));

Then your ctor will become:

public BookingBll(IWindsorContainer container, IBookingRepository repository) {}

It's generally frowned upon to take a dependency on the container/kernel, better to inject the components you need as it will make your dependencies much clearer and your code testable. Taking a container dependency is like making Global.SystemContainer 'public static' and reference it from everywhere in your app.

In your example, the ILogger (if it's Castle.Core.ILogger) can be injected by the LoggingFacility, no need to resolve it yourself.