是否可以将服务注入单例类c#

时间:2015-11-20 13:41:14

标签: c# .net winforms dependency-injection

我想知道将服务注入单例类的正确方法是什么(例如,使用NInject框架。

实际上单一代码正在做什么 - 它在应用程序上下文中运行Windows窗体

public class FrontController
{
    private static volatile FrontController _instance;
    private static readonly object syncRoot = new Object();
    private ControlsContainer _controlsContainer;

    private FrontController() { }

    public static FrontController Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (syncRoot)
                {
                    if (_instance == null)
                    {
                        _instance = new FrontController();
                    }

                }
            }
            return _instance;
        }
    }

    public void StartApplication()
    {
        _controlsContainer = new ControlsContainer();
        Application.Run(_controlsContainer);
    }

    public void EndApplication()
    {
        //throw new NotImplementedException();
    }

    internal void Synchronize()
    {
        ISymantecService<ClientModel> service =
            new SymantecService<ClientModel>(new CustomerRepository<ClientModel>());

        service.Synchronize();
    }
}

我们有

 public void StartApplication()
 {
        _controlsContainer = new ControlsContainer();
        Application.Run(_controlsContainer);
 }

以及

internal void Synchronize()
{
    ISymantecService<ClientModel> service =
            new SymantecService<ClientModel>(new CustomerRepository<ClientModel>());

    quickBookservice.Synchronize();
}

有没有办法以线程安全的方式在这个类中注入ISymantecService和ICustomerRepository。

0 个答案:

没有答案