Question

I have 'standard' MVC application in next structure: UI (MVC), Business logic and data access using FluentNHibernate. I've done all steps for registering in castle container my components as described in castle documentation, used FluentRegistration API. So, all components were registered using WIndsor Installers like this:

container.Register(Classes.FromThisAssembly()
            .BasedOn(typeof(IHandlerBase<>))
            .WithService.AllInterfaces()
            .LifestyleTransient());

In this code IHandlerBase - generic interface from which derives all my nested classes in business logic. Data access registered in same way and when I start my app all my components and services registered in container. All dependencies in MVC project relatively to BL resolved, but when I need my dependencies in business logic relatively to DataAccess they didn't. Stub example of my modules:

MVC

public ILogger Logger { get; set; } // Resolved, not null
public IHandlerBase<FooRerquest> FooHandler<FooRequest> { get; set; } // Resolved, not null
// Call Foohandler method
FooHandler.MethodName() { ... } // Here works fine

BusinessLogic

public ILogger Logger { get; set; } // Unresolved, null
public IRepository <FooCommand> FooRepository<FooCommand> { get; set; } // Unresolved, null
// Call FooRepository method
FooRepository.MethodName() { ... } // Doesn't work, catch ArgumentNullException

ILogger is a Castle NLog Facility

What and where I do in wrong way?

Thanks, Andrew

Was it helpful?

Solution

It works in UI because Controllers are created via custom ControllerFactory which uses container usually.

If instances of your business logic classes are resolved as part of MVC classes, they should be resolved. If simply constructors are used to create instances of your business logic classes then they know nothing about IoC and all references can't be resolved.

I suggest you use constructor injection since it makes Dependency Injection more visible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top