ASP.NET MVC 3 & Unity.MVC3(5)
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// e.g. container.RegisterType<ITestService, TestService>();
container.LoadConfiguration("default");
container.RegisterControllers();
return container;
}
}
这里我把依赖关系都放到配置文件里了:
[html] print?<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
...
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IRepository" type="GenericRepository.IRepository`1, GenericRepository" />
<alias alias="DbContextRepository" type="GenericRepository.DbContextRepository`1, GenericRepository" />
<alias alias="User" type="MvcWithUnityTest.Models.User, MvcWithUnityTest.Models" />
<alias alias="DbEntities" type="MvcWithUnityTest.Models.DbEntities, MvcWithUnityTest.Models" />
<container name="default">
<register type="IRepository[User]" mapTo="DbContextRepository[User]">
<lifetime type="HierarchicalLifetimeManager" />
<constructor>
<param name="context" dependencyType="DbEntities">
</param>
</constructor>
</register>
</container>
</unity>
</configuration>
相关新闻>>
- 发表评论
-
- 最新评论 更多>>