Independent project Injection of Dependencies Ninject

0

Hello! I'm using the DDD-type architecture to do a project, in it I have the CrossCutting layer, and on this layer I thought of adding a class library project with Ninject so it does not stay in the presentation part.

I made a few attempts but found nothing that solved the problem ... could you help me?

This was the closest I got to it:

public class CrossCuttingModule : NinjectModule
{

    public override void Load()
    {
        Bind(typeof(IAppService<>)).To(typeof(AppService<>));
        Bind<IClienteService>().To<ClienteService>();
        Bind<IProdutoService>().To<ProdutoService>();

        Bind(typeof(IRepository<>)).To(typeof(Repository<>));
        Bind<IProdutoRepository>().To<ProdutoRepository>();
        Bind<IClienteRepository>().To<ClienteRepository>();
   }

}
    
asked by anonymous 27.10.2014 / 19:44

3 answers

1

I am studying DDD and I also did a recent project following the example of Eduardo Pires that can be found in this LINK , in the test project when installing Ninject by nugget it creates a class in the App_Start folder of the project called NinjectWebCommon , in this class there is a static void method called RegisterServices where you register your modules, as an example below:

/// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            Bind(typeof(IAppService<>)).To(typeof(AppService<>));
            Bind<IClienteService>().To<ClienteService>();
            Bind<IProdutoService>().To<ProdutoService>();

            Bind(typeof(IRepository<>)).To(typeof(Repository<>));
            Bind<IProdutoRepository>().To<ProdutoRepository>();
            Bind<IClienteRepository>().To<ClienteRepository>();
        }      
    
28.10.2014 / 13:43
0

I managed to solve my problem, in the CrossCutting layer I installed Ninject, and realized the concrete class binds / interfaces, in the View layer (MVC / WebAPI) I also installed Ninject, and in the container I called my class from the crosscutting layer passing my Web class container as a parameter.

    
13.05.2015 / 13:18
0

In your CrossCutting project you use the CommonServiceLocator .

One project I use for this is NinjectAdapter.Unofficial (as the name already says, it is not official, but it has always met my needs). The installation command in Nuget is:

  

install-Package CommonServiceLocator.NinjectAdapter.Unofficial

And in this same project, you create a Classe Container , where you indicate your class CrossCuttingModule in StandardKernel , so it will contain a método to select your created module, thus this class:

 public class Container
    {
        public Container()
        {
            ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(GetModule()));
        }

        public StandardKernel GetModule()
        {
            //Chama a sua classe CrossCuttingModule
            return new StandardKernel(new CrossCuttingModule());
        }
    }

When you do this in your Class Library project, you only have to reference RegisterServices in your NinjectWebCommon in your MVC project, thus:

/// <summary>
        ///     Load your modules or register your services here!
        /// </summary>
        private static StandardKernel RegisterServices()
        {
            return new Container().GetModule();
        }

When you do this, you just need to make the Bind<> in your project in your class CrossCuttingModule normally.

    
05.05.2015 / 14:43