Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

autofac - Does Ninject support Func (auto generated factory)?

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

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

Thanks.

Update:

Just found this post, seems the answer is no:

How do I handle classes with static methods with Ninject?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...