简体   繁体   中英

Using Simple Injector with base classes

I'm trying to figure out what is the correct way to use Simple Injector and Dependency Injection.

I have MasterController and few controllers that work under that. So basically what I'm trying to archieve a MasterController that I can use from another classes or maybe from my user interface.

public class MasterController
{
    public static Container _container = new Container();

    ILogger logger;

    public IMainController MainController;
    public ISubController SubController;

    public MasterController()
    {
        _container.Register<ILogger, DebugLogger>(Lifestyle.Singleton);
        _container.Register<IMainController, TestController>(Lifestyle.Singleton);
        _container.Register<ISubController, Test2Controller>(Lifestyle.Transient);

        _container.Verify();

        logger = _container.GetInstance<ILogger>();
        MainController = _container.GetInstance<IMainController>();
    }

    public void RenameMainController(string _name)
    {
        MainController.Name = _name;
    }

    // more code here.
}

Then I have my IMainController , ISubController , TestController and Test2Controller .

public interface IMainController
{
    string Name { get; set; }
    void DoSomething();
}

public interface ISubController
{
    string Name { get; set; }
    void DoSomething();
    void SomeExtraFunctionality();
}

public class TestController : BaseController, IMainController
{
    private ILogger logger;

    public List<ISubController> SubControllers = new List<ISubController>();

    public TestController(ISubController _SubController, ILogger _logger)
    {
        logger = _logger;

        SubControllers.Add(_SubController);
        logger.Log("TestController ready");
    }

    public void DoSomething() { }
}

public class Test2Controller : BaseController, ISubController
{
    private ILogger logger;

    public Test2Controller(ILogger _logger)
    {
        logger = _logger;
        logger.Log("TestController ready");
    }

    public void DoSomething() { }
    public void SomeExtraFunctionality() { }
}

My idea is that i have few controllers and they use some common things from BaseController

public class BaseController
{
    public string Name { get; set; }

    // here is some code. 
}

But in BaseController I want to use the ILogger also. How do i do it the correct way?

Now I have added BaseController to container.

_container.Register<BaseController>(Lifestyle.Transient);

And then adding constructor to BaseController

    public BaseController(ILogger _logger)
    {
        logger = _logger;
    }

And then add the

: base(_logger) 

after all the controller constructors Example:

public TestController(ISubController _SubController, ILogger _logger) : base(_logger)

But is this the correct way of doing this or would there be a more "correct" way to do this?

Have you followed the docs? Using Simple Injector

Not even your master controller should be aware of the container and the container should not be widely accessible through the application. It is better to create and configure your container in the Composition Root of your application ie somewhere near Application_Start as per the Simple Injector documentation and do not expose that container to the rest of your application. Otherwise you are effectively ending up with the Service Locator Anti Pattern .

As part of the configuration you can set up Simple Injector for the PerWebApiRequest lifestyle (assuming you are using MVC or pick an appropriate alternative) and the container will automatically inject the dependencies into any controller constructor and dispose the IDisposable dependencies without you having to do anything other than configure the dependency in the composition root. No controller will therefore ever have to be aware the container actually exists.

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