2013-05-29 25 views
1

我目前正在将一些工作Ninject代码迁移到Microsoft Unity,并且在这个特定转换中有点卡住。将Ninject转换为Unity

Bind<MyDbContext>().ToSelf().InRequestScope().WithConstructorArgument("connectionString", configurationService.MySqlConnectionString); 
Bind<IUnitOfWork>().ToMethod(ctx => ctx.Kernel.Get<MyDbContext>()); 
Bind<DbContext>().ToMethod(ctx => ctx.Kernel.Get<MyDbContext>()); 

任何帮助,将不胜感激,在提供此代码的Unity 3.0版本。

回答

3

假设你正在使用的Unity.MVC4的NuGet(这将注册自定义DependencyResolver你):

container.RegisterType<IUnitOfWork, MyDbContext>(
    new HierarchicalLifetimeManager(), 
    new InjectionFactory(
     c => new MyDbContext(configurationService.MySqlConnectionString) 
    ) 
); 
container.RegisterType<DbContext, MyDbContext>(
    new HierarchicalLifetimeManager() 
); 
+0

感谢达林,你真是个天才!哦,是的,我正在使用着名的NuGet软件包。 – Bern