Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC Dependency Injection with Ninject

I've just started programming in .NET and I'm having some problems with implementing dependency injection (using Ninject).

I'm creating some sort of catering application where user can browse towns, in towns browse restaurants and in restaurants browse food.

I'm using UnitOfWork and repository pattern where for example I access town by id like this:

_unitOfWork.TownRepository.GetByID(id);

Now I started implementing services into application and I have encountered need for dependency injection.

I have created ITownService, IRestaurantService and IFoodService (since I've TownRepository, RestaurantRepository and FoodRepository in my UnitOfWork).

Sample look of TownService:

public class TownService : ITownService
    {
        // initialize UnitOfWork
        private IUnitOfWork _unitOfWork;

        public TownService()
            : this(new UnitOfWork())
        {
        }

        public TownService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public Town GetByID(object id)
        {
            return _unitOfWork.TownRepository.GetByID(id);
        }

        public IEnumerable<Town> GetAll()
        {
            return _unitOfWork.TownRepository.Get();
        }

        public bool Insert(Town town)
        {
            // validation logic
            if (!ValidateTown(town))
                return false;

            try
            {
                _unitOfWork.TownRepository.Insert(town);
                _unitOfWork.Save();
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool Delete(object id)
        {
            try
            {
                _unitOfWork.TownRepository.Delete(id);
                _unitOfWork.Save();
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool Update(Town townToUpdate)
        {
            // validation logic
            if (!ValidateTown(townToUpdate))
                return false;

            try
            {
                _unitOfWork.TownRepository.Update(townToUpdate);
                _unitOfWork.Save();
            }
            catch
            {
                return false;
            }

            return true;
        } 
    }

I haven't implemented FoodService and RestaurantService yet, but they should be similar with of course some additinal methods exepct this that I have. For example in RestaurantService I might have public Restaurant GetRestaurantsInTown(Town town){} or something like that.

I hope that you got the feel of application a bit. Now lets back to Ninject.

In my TownController I would have something like this:

 public class TownController : Controller
    {

        private ITownService _townService;

        public TownController(ITownService townService)
        {
            _townService = townService;
        }
    }

Similar would be for RestaurantController and FoodController of course just constructor injecting.

How do I use Ninject in such example? Do I need some global IService and not ITownService, IRestaurantService and IFoodService which I'd inherid in TownService, RestaurantService and FoodService or is it okay like this?

When binding what do I need to bind?

kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
kernel.Bind<ITownService>().To<TownService>();
kernel.Bind<IRestaurantService>().To<RestaurantService>();
kernel.Bind<IFoodService>().To<TownService>();

Something like this?

In short - what I need for adding dependency injection with Ninject?

I'm really having problems with this and would need help.

Thanks a lot in forward.

like image 739
Želja Huber Avatar asked Feb 03 '14 10:02

Želja Huber


People also ask

What is ninject dependency injection?

NInject is a popular IOC container that can be used to inject dependencies in your WebAPI controllers easily. IDG. Dependency injection is a software design pattern that helps you to build pluggable implementations in your application using loosely coupled, testable components.

Does ninject support .NET Core?

Ninject. Web. AspNetCore is built in a way that makes it easy to integrate with all standard ASP.NET Core examples and templates.

Does MVC support dependency injection?

The Dependency Injection (DI) Design PatternThe Dependency Resolver in ASP.NET MVC can allow you to register your dependency logic somewhere else (e.g. a container or a bag of clubs). The advantages of using Dependency Injection pattern and Inversion of Control are the following: Reduces class coupling.


1 Answers

From the package manager console run this command:

Install-package Ninject.MVC3

This will add a class to App_Start/NinjectWebCommon.cs

If you look near the bottom there is a RegisterServices method.

You simply add the code from your question there i.e.

    private static void RegisterServices(IKernel kernel)
    {
      kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
      kernel.Bind<ITownService>().To<TownService>();
      kernel.Bind<IRestaurantService>().To<RestaurantService>();
      kernel.Bind<IFoodService>().To<TownService>();
    }
like image 162
hutchonoid Avatar answered Oct 04 '22 10:10

hutchonoid