Sam*_*tar 1 asp.net-mvc dependency-injection asp.net-mvc-4
我正在使用MVC4开始一个新项目,并希望使用依赖注入.我过去没有使用它,但现在似乎是使用它的好时机.
任何人都可以告诉我哪些DI工具现在支持MVC4,现在提供最简单的设置.由于项目很小,我的需求相当简单.对我来说最重要的是一个易于学习和配置的系统.
我在MVC 4 Beta项目中成功使用了Ninject.MVC3 NuGet包.
// Global.asax.cs
public static void Configure(HttpConfiguration config)
{
var kernel = new StandardKernel();
RegisterServices(kernel);
// For ApiControllers
config.ServiceResolver.SetResolver(
t => kernel.TryGet(t),
t => kernel.GetAll(t));
// For Controllers
DependencyResolver.SetResolver(
t => kernel.TryGet(t),
t => kernel.GetAll(t));
}
public static void RegisterServices(IKernel kernel)
{
kernel.Bind<IRepository>().To<Repository>();
}
protected void Application_Start()
{
...
Configure(GlobalConfiguration.Configuration);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1957 次 |
| 最近记录: |