NInject自定义上下文

时间:2015-07-17 12:37:24

标签: ninject

我将揭露我现在正在做的事情,以及之后,我想用NInject做些什么。

类:

  1. 内核,
  2. ApiClient
  3. 的IIdentity
  4. 内核公开了几种方法:

    object method(IIdentity identity)
    -----------------------
    public List<Domain.Channel> getChannels(Domain.Identity.IIdentity identity) {...}
    

    内核包含多个存储的身份,根据每个上下文我需要做一件事或另一件事。 在这种情况下,我的上下文在方法内,上下文键是标识参数值。根据这个,我需要得到一个ApiClient实例。

    实际上,我是使用Kernel类的Dictionary属性执行此操作的:

     private Dictionary<Domain.Identity.IIdentity, LEST.Client.ApiClient> lests;
    

    然后我初始化。

     foreach (Domain.Identity.IIdentity identity in this.identities)
     {
         LEST.Client.ApiClient lest = new LEST.Client.ApiClient();
         lest.configure(identity.ClientId, identity.Username, identity.Password);
    
         this.lests.Add(identity, lest);
     }
    

    所以,当我需要根据身份获得具体的ApiClient对象时:

    public List<Domain.Channel> getChannels(Domain.Identity.IIdentity identity)
    {
        LEST.Client.ApiClient current_lest;
        this.lests.TryGetValue(identity, out current_lest);
    
        if (current_lest != null)
        {
            ...        
        }
    

    我想使用NInject来做。我创建了一个模块,以便创建自己的绑定。问题是我不知道如何创建这样的自定义上下文。

    我很感激你的帮助。

    谢谢大家。

    修改

    我抽象了一个基本的核心课程,总结了我的解释。

    public class Core
    {
        private IList<IIdentity> identities;
        private Ninject.IKernel kernel;
    
        public Core()
        {
            this.identities = new List<IIdentity>();
        }
    
        public void initialize()
        {
            this.kernel = new Ninject.StandardKernel(new LestModule());
        }
    
        public void openSession(IIdentity identity)
        {
            ApiClient client = this.kernel.Get<ApiClient>();
        }
    
        public void doSomething(IIdentity identity)
        {
            ApiClient client = this.kernel.Get<ApiClient>();
            client...
        }
    
        public void closeSession(IIdentity identity)
        {
            ApiClient client = this.kernel.Get<ApiClient>();
            this.kernel.Release(client);
        }
    }
    

    模块是:

    public class LestModule : Ninject.Modules.NinjectModule
    {
        public override void Load()
        {
            this.Bind<ApiClient>().ToProvider(new CustomProvider());
            this.Bind<IIdentity>().To<Identity>();
        }
    }
    
    public class CustomProvider : IProvider<ApiClient>
    {
    
        public object Create(IContext context)
        {
            //context data???
            // How to obtain current IIdentity on here?
            object d = context;
            return new ApiClient("", "", "");
        }
    
        public System.Type Type
        {
            get { return typeof(ApiClient); }
        }
    }
    

    ApiClient的抽象

    public class ApiClient
    {
    
        private string username;
    
        public string Username
        {
            get { return username; }
            set { username = value; }
        }
    
        private string client_id;
    
        public string ClientId
        {
            get { return client_id; }
            set { client_id = value; }
        }
    
        private string passwd;
    
        public string Passwd
        {
            get { return passwd; }
            set { passwd = value; }
        }
    
    
        public ApiClient(string client_id, string username, string password)
        {
            this.client_id = client_id;
            this.username = username;
            this.passwd = password;
        }
    }
    

    所以,我喜欢这样:

    1. 在openSession中:创建一个ApiClient实例(this.kernel.Get),根据此方法的IIdentity参数初始化实例。
    2. 在doSomething中:根据此方法的IIdentity参数获取ApiClient实例。
    3. 在closeSession中:根据此方法的IIdentity参数实现实例。

1 个答案:

答案 0 :(得分:0)

我已经实现了这个。

现在正在运作。

public class LestModule : Ninject.Modules.NinjectModule
{

    public override void Load()
    {
        this.Bind<LEST.Client.ApiClient>().ToProvider<LEST.Client.ApiClient>(new ApiClientProvider());
    }

}

public class ApiClientProvider : Ninject.Activation.IProvider<LEST.Client.ApiClient>
{

    private System.Collections.Generic.Dictionary<Domain.Identity.LESTIdentity, LEST.Client.ApiClient> lests;

    public ApiClientProvider()
    {
        this.lests = new System.Collections.Generic.Dictionary<Domain.Identity.LESTIdentity, LEST.Client.ApiClient>();
    }

    public object Create(Ninject.Activation.IContext context)
    {
        Ninject.Parameters.IParameter identity_parameter = context.Parameters.FirstOrDefault(p => p.Name == "identity");
        Domain.Identity.LESTIdentity identity = (Domain.Identity.LESTIdentity)identity_parameter.GetValue(context, null);

        LEST.Client.ApiClient current_client = null;
        this.lests.TryGetValue(identity, out current_client);
        if (current_client == null)
        {
            current_client = new LEST.Client.ApiClient();
            current_client.configure(...);

            this.lests.Add(identity, current_client);
        }

        return current_client;
    }

    ...
}

为了根据身份获取实例:

public void createChannel(Domain.Identity.ClientIdentity client_identity, Domain.Identity.UserIdentity user_identity, Domain.Channel channel)
    {
        LEST.Client.ApiClient client = NinjectServiceLocator.Instance.Kernel.Get<LEST.Client.ApiClient>(new Ninject.Parameters.Parameter("identity", new Domain.Identity.LESTIdentity(user_identity.Id, user_identity.Name, client_identity.Id, client_identity.Name), true));
        client.ChannelsApiP.Create(AutoMapper.Mapper.Map<Domain.Channel, LEST.Model.ChannelDTO>(channel));
    }

你有什么关系?