【问题标题】:Does Ninject support Func (auto generated factory)?Ninject 是否支持 Func(自动生成工厂)?
【发布时间】:2011-06-17 22:20:09
【问题描述】:

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

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

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

谢谢。

更新

刚找到这个帖子,好像答案是否定的:

How do I handle classes with static methods with Ninject?

【问题讨论】:

    标签: ninject autofac func


    【解决方案1】:

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


    编辑:注意在 Ninject 2.3 中有一个 Bind&lt;T&gt;().ToFactory() 实现(这不是完全测试支持的版本,而是 is available from the CodeBetter server

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

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

    【讨论】:

    • 感谢代码!期待下一个版本。
    • 感谢您的辛勤工作雷莫。是否可以扩展代码以使用 Func>?
    • 当然可以。更改 FunctionFactory.Create 方法,检查 IEnumerable 并返回 GetAll。
    • 反应超快! :D 但它抱怨它没有找到 Func> 的绑定(其中 T 是我的接口).. 确切的错误消息 ... Func{IEnumerable{IWorker}} 2)注入依赖 Func{IEnumerable {IWorker}} 放入 WorkflowManager 类型构造函数的参数 workerFactory
    • 啊,我刚刚注意到它取决于VerifyFactoryFunction,我必须更新它才能工作..谢谢伙计!
    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2015-12-23
    • 2016-05-23
    相关资源
    最近更新 更多