简体   繁体   中英

.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.

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 ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM