繁体   English   中英

使用Ninject进行.NET MVC依赖注入

[英].NET MVC Dependency Injection with Ninject

我刚开始用.NET编程,我在实现dependency injection (using Ninject)方面遇到了一些问题dependency injection (using Ninject)

我正在创建某种餐饮应用程序,用户可以浏览城镇,城镇浏览餐馆和餐馆浏览食物。

我正在使用UnitOfWork和存储库模式,例如我通过id访问城镇,如下所示:

_unitOfWork.TownRepository.GetByID(id);

现在我开始在应用程序中实现服务,我遇到了dependency injection

我创建了ITownServiceIRestaurantServiceIFoodService (因为我在UnitOfWorkTownRepositoryRestaurantRepositoryFoodRepository )。

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

我还没有实现FoodServiceRestaurantService ,但它们应该是类似的,当然有些附加方法可以解决这个问题。 例如在RestaurantService我可能有public Restaurant GetRestaurantsInTown(Town town){}或类似的东西。

我希望你有一点应用的感觉。 现在回到Ninject

在我的TownController我会有这样的事情:

 public class TownController : Controller
    {

        private ITownService _townService;

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

类似于RestaurantControllerFoodController当然只是构造函数注入。

在这样的例子中如何使用Ninject 我是否需要一些全局的IService而不是我在TownServiceRestaurantServiceFoodServiceTownService ITownServiceIRestaurantServiceIFoodService ,还是可以这样吗?

绑定时我需要绑定什么?

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

像这样的东西?

简而言之 - 我需要为Ninject添加依赖注入?

我真的遇到了这个问题,需要帮助。

非常感谢前进。

从包管理器控制台运行此命令:

Install-package Ninject.MVC3

这将为App_Start/NinjectWebCommon.cs添加一个类

如果你看到底部附近有一个RegisterServices方法。

你只需在那里添加你问题的代码,即

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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