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

问题描述

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

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()实现(这不是完全受测试支持的版本,但 )

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(自动生成的工厂)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:38