Independent Project Injection of Dependencies Ninject

Asked

Viewed 972 times

0

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

I’ve made some attempts but I haven’t found anything to solve the problem... you could help me?

That’s as close as I got:

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

}

3 answers

1

I’m studying DDD and also did a recent project following the example of Eduardo Pires that can be found in this LINK, in the test project when installing the Ninject for nugget it creates a class in the folder App_Start of the project called NinjectWebCommon, in this class there is a method static void 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>();
        }      
  • Yes, but in this case it is in the MVC project right? I would like in a separate project...

0

I managed to solve my problem, in the Crosscutting layer I installed Ninject, and realized bind’s concrete class/interfaces, in the View layer (MVC/Webapi) I also installed Ninject, and in the container I called my crosscutting layer class passing as parameter my web class container.

0

In your project CrossCutting you use the Commonservicelocator.

A project that I use for this is the Ninjectadapter.Unofficial(as the name says, is not official, but always met my needs). The installation command in Nuget is:

install-Package Commonservicelocator.NinjectAdapter.Unofficial

And in this very project, you create a Classe Container, where you indicate your class CrossCuttingModule in the StandardKernel, so it will contain a método to select your module created, thus leaving 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());
        }
    }

Done this in your project Class Library, just refer to the RegisterServices in his NinjectWebCommon in his MVC project, thus:

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

Done that, just do the Bind<> in your class project CrossCuttingModule normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.