Search code examples
design-patternsdependency-injectionstaticstatic-methodsstatic-classes

Dependency Injection with Static classes and properties


I have designed a multi-layer solution and created a bunch of Manager classes to implement Business Logic. All the managers are derived from BaseManager class. To be more clear, here's UserManager class:

public class UserManager : BaseManager
{
    public void Add(User user)
    {
       ...
    }
    public void Update(User user)
    {
       ...
    }
    public void Delete(User user)
    {
       ...
    }
    public bool GetAccessPermissions(User user)
    {
       ...
    }
}  

BaseManager is as follows:

public class BaseManager
{  
    protected IRepository repository = IoCLocator.Resolve<IRepository>();
    public BaseManager()
    {
        ...
    }
    // Some shared methods among manager classes.
}  

Now I skeptical that my manager classes should all be defined static, since they get their related entities on which they want to operate, as parameters. But As you see, I have some shared private/protected members which I should instantiate upon every call. (e.g. repository should be instantiated on each manager class's creation).

What are my choices to define managers classes as static and still have the protected members instantiated on each call to managers' methods?


Solution

  • Do you need your manager classes to be static, or is it just that you wish them to be singletons?

    Are you seeking a way to have a singleton class with transient dependencies?

    In which case I would answer that the singleton manager class should take a dependency on a factory which can create instances of the repositories as required.

    Windsor offers a typed factory facility which helps with creating such factories: http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx