具有Lightinject的通用FactoryMethod实例提供程序

时间:2019-04-21 17:17:17

标签: c# inversion-of-control ninject light-inject

在之前的项目中,我使用Ninject实现动作的自动生成。我有一个通用的ActionsFactory接口,如下所示:

public interface IActionsFactory
{
    TAction GetAction<TAction>();
}

Ninject配置为将此接口绑定到工厂:

kernel.Bind<IActionsFactory>().ToFactory(() => new GenericFactoryMethodInstanceProvider());

public class GenericFactoryMethodInstanceProvider : StandardInstanceProvider
{
    protected override string GetName(MethodInfo methodInfo, object[] arguments)
    {
        return null;
    }
}

有了这个,我只能在控制器中引用IActionFactory,而无需引用所有操作,例如:

public class SomeController : ApiController
{
    private readonly IActionsFactory 

    public SomeController(IActionsFactory actionsFactory)
    {
        this.actionsFactory = actionsFactory;
    }

    [HttpGet]
    public async Task<Foo> GetFoo()
    {
        retutn await actionsFactory.GetAction<IFooAction>().GetSomeFoo();
    }
}

问题是:如何使用Lightinject实现类似的目标?

0 个答案:

没有答案