hidden
hidden

Reputation: 3236

Ninject factory not working with conventions for me

I am trying to use the method of binding located here but having no luck https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface%3A-Referencing-Named-Bindings

Keep in mind I am not trying to do it this way:https://gist.github.com/akimboyko/4593576 Rather I am trying to use the convention GetMercedes() to mean

I am basically trying to achieve this:https://gist.github.com/akimboyko/4593576 with conventions specified in the above examples.

using Ninject;
using Ninject.Extensions.Factory;
using Ninject.Modules;
using Ninject.Parameters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Test.NinjectFactory
{
    public class Program
    {
        public static void Main()
        {
            using (var kernel = new StandardKernel(new CarModule()))
            {


               var factory = kernel.Get<ICarFactory>();
               var mercedes =factory.GetMercedes();
                int i = 1; 

            }
        }

        public interface ICar
        {
            void Drive();

        }

        public class Mercedes : ICar
        {
            readonly ICarFactory carFactory;
            public Mercedes(ICarFactory carFactory)
            {
                this.carFactory = carFactory;
            }

            public void Drive()
            {
                var Mercedes = this.carFactory.GetMercedes();
            }

        }


        public interface ICarFactory
        {
            ICar GetMercedes();
        }

        public class CarModule : NinjectModule
        {
            public override void  Load()
            {
                //https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface%3A-Referencing-Named-Bindings
                Kernel.Bind<ICarFactory>().ToFactory();
                Bind<ICar>().To<Mercedes>().NamedLikeFactoryMethod<ICarFactory>(x => x.GetMercedes());//doesnt work for me

            }
        }
    }
}

Upvotes: 1

Views: 404

Answers (2)

hidden
hidden

Reputation: 3236

I found the anwswer here: https://gist.github.com/akimboyko/5338320

It seems you need a function to take care of the binding

public class BaseTypeBindingGenerator<InterfaceType> : IBindingGenerator
{
    public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
    {
        if (type != null && !type.IsAbstract && type.IsClass && typeof(InterfaceType).IsAssignableFrom(type))
        {
            string.Format("Binds '{0}' to '{1}' as '{2}", type, type.Name, typeof(InterfaceType)).Dump();

            yield return bindingRoot.Bind(typeof(InterfaceType))
                                    .To(type)
                                    .Named(type.Name) as IBindingWhenInNamedWithOrOnSyntax<object>;
        }
    }
}

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65049

I'm posting this as an answer because it is most likely the cause.

The factory extensions use prefixed Get methods as a standard. You'll run into issues by calling any of your factory methods with prefixed Get and using NamedLikeFactoryMethod. For example, GetFord, GetMercedes, GetNissan. You'll notice that, in the example at the link you provided, the function is called CreateMercedes.

Change your function name to CreateMercedes or anything that doesn't start with Get and it should be fine.

Upvotes: 2

Related Questions