StructureMap - Injecting a dependency into a base class?

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-05-27T15:56:52Z Indexed on 2010/05/27 16:01 UTC
Read the original article Hit count: 240

In my domain I have a handful of "processor" classes which hold the bulk of the business logic. Using StructureMap with default conventions, I inject repositories into those classes for their various IO (databases, file system, etc.). For example:

public interface IHelloWorldProcessor
{
    string HelloWorld();
}
public class HelloWorldProcessor : IHelloWorldProcessor
{
    private IDBRepository _dbRepository;
    public HelloWorldProcessor(IDBRepository dbRepository)
    {
        _dbRepository = dbrepository;
    }
    public string HelloWorld(){ return _dbRepository.GetHelloWorld(); }
}

Now, there are some repositories that I'd like to be available to all processors, so I made a base class like this:

public class BaseProcessor
{
    protected ICommonRepository _commonRepository;
    public BaseProcessor(ICommonRepository commonRepository)
    {
        _commonRepository = commonRepository;
    }
}

But when my other processors inherit from it, I get a compiler error on each one saying that there's no constructor for BaseProcessor which takes zero arguments.

Is there a way to do what I'm trying to do here? That is, to have common dependencies injected into a base class that my other classes can use without having to write the injections into each one?

© Stack Overflow or respective owner

Related posts about inheritance

Related posts about structuremap