Ninject 是否支持 Func(自动生成工厂)? [英] Does Ninject support Func (auto generated factory)?

查看:35
本文介绍了Ninject 是否支持 Func(自动生成工厂)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Autofac 自动为 Func 生成工厂;我什至可以传递参数.

Autofac automatically generates factories for Func<T>; I can even pass parameters.

public class MyClass
{
    public MyClass(Func<A> a, Func<int, B> b)
    {
        var _a = a();
        var _b = b(1);
    }
}

我可以用 Ninject 做同样的事情吗?如果没有,我可以应用什么解决方法?

Can I do the same with Ninject? If not, what workaround can I apply?

谢谢.

更新:

刚刚找到这个帖子,似乎答案是否定的:

Just found this post, seems the answer is no:

我如何处理类使用 Ninject 的静态方法?

推荐答案

NB Ninject 3.0 及更高版本使用 Ninject.Extensions.Factory 包完全支持此方案,请参阅 wiki:- https://github.com/ninject/ninject.extensions.factory/wiki

NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https://github.com/ninject/ninject.extensions.factory/wiki

注意 Ninject 2.3 中有一个 Bind<T>().ToFactory() 实现(这不是一个完全测试支持的版本,而是 可从 CodeBetter 服务器获得)

NB there is a Bind<T>().ToFactory() implementation in Ninject 2.3 (which is not a fully tests supported release but is available from the CodeBetter server)

Ninject 目前本身不支持此功能.我们计划将其添加到下一个版本中.但是可以通过配置适当的绑定轻松添加支持.只需加载下面的模块即可享受.

Ninject does not support this natively at the moment. We planned to add this to the next version. But support can be added easily by configuring the appropriate binding. Just load the module below and enjoy.

public class FuncModule : NinjectModule
{
    public override void Load()
    {
        this.Kernel.Bind(typeof(Func<>)).ToMethod(CreateFunc).When(VerifyFactoryFunction);
    }

    private static bool VerifyFactoryFunction(IRequest request)
    {
        var genericArguments = request.Service.GetGenericArguments();
        if (genericArguments.Count() != 1)
        {
            return false;
        }

        var instanceType = genericArguments.Single();
        return request.ParentContext.Kernel.CanResolve(new Request(genericArguments[0], null, new IParameter[0], null, false, true)) ||
               TypeIsSelfBindable(instanceType);
    }

    private static object CreateFunc(IContext ctx)
    {
        var functionFactoryType = typeof(FunctionFactory<>).MakeGenericType(ctx.GenericArguments);
        var ctor = functionFactoryType.GetConstructors().Single();
        var functionFactory = ctor.Invoke(new object[] { ctx.Kernel });
        return functionFactoryType.GetMethod("Create").Invoke(functionFactory, new object[0]);
    }

    private static bool TypeIsSelfBindable(Type service)
    {
        return !service.IsInterface
               && !service.IsAbstract
               && !service.IsValueType
               && service != typeof(string)
               && !service.ContainsGenericParameters;
    }

    public class FunctionFactory<T>
    {
        private readonly IKernel kernel;

        public FunctionFactory(IKernel kernel)
        {
            this.kernel = kernel;
        }

        public Func<T> Create()
        {
            return () => this.kernel.Get<T>();
        }
    }
}

这篇关于Ninject 是否支持 Func(自动生成工厂)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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