NInject自定义上下文 [英] NInject Custom context

查看:88
本文介绍了NInject自定义上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将介绍我现在正在做的事情,以及以后要使用NInject做的事情.

I'll expose what I'm doing now, and later, what I'd like to do using NInject.

课程:

  1. 内核,
  2. ApiClient
  3. IIdentity

内核将几种方法公开为:

Kernel exposes several methods as:

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

内核包含一些已存储的身份,根据每个上下文,我需要做一件事或另一件事. 在这种情况下,我的上下文在方法内部,并且上下文关键字是标识参数值.根据这一点,我需要获取ApiClient的实例.

Kernel contains several identities stored, and according each context I need to do a thing or another. In this case, my context is inside the method and the context key is identity parameter value. According this one, I need to get an instance of ApiClient.

实际上,我是使用Kernel类的Dictionary属性来做到这一点的:

Actually, I'm doing this using a Dictionary property of Kernel class:

 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对象时:

So, when I need to get a concrete ApiClient object according an identity:

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做到这一点.我创建了一个模块,以创建与其自身的绑定.问题是我不知道如何创建这样的自定义上下文.

I'd like to do it using NInject. I've created a module in order to create the bindings to itselfs. The problem is that I've no idea how to create a custom context like this.

非常感谢您的帮助.

谢谢大家.

编辑

我抽象了一个基本的Core类,总结了我的解释.

I've abstracted a basic Core class that summarizes my explanation.

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的抽象

And the abstraction of 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中:根据此方法的IIdentity参数创建一个ApiClient实例(this.kernel.Get)初始化该实例.
  2. 在doSomething中:根据此方法的IIdentity参数获取ApiClient实例.
  3. 在closeSession中:根据此方法的IIdentity参数实现实例.

推荐答案

我已经实现了这一点.

现在正在工作.

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;
    }

    ...
}

为了根据身份获取实例:

In order to get the instance according to the identity:

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));
    }

你怎么看?

这篇关于NInject自定义上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆