【发布时间】:2011-05-26 01:21:51
【问题描述】:
我正在使用:
- EF 4.1
- MVC 3
- 忍者
- Ninject.Extensions.Conventions
- Ninject.Web.Mvc
应用程序使用存储库模式。 我的存储库可以这样注入:
kernel.Bind<ICategoryRepository>().To<CategoryRepository>().InRequestScope();
一切正常:-)
但我一直在尝试进一步从我的 global.asax.cs 中这样的程序集动态注入
private static void LoadFromAssemblies(IKernel kernel)
{
Uri uri = new Uri(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) +
@"\Extensions");
DirectoryInfo directoryInfo = new DirectoryInfo(uri.LocalPath);
var scanner = new AssemblyScanner();
scanner.FromAssembliesInPath(directoryInfo.FullName);
scanner.BindWith<DefaultBindingGenerator>();
kernel.Scan(scanner);
//var foo = kernel.Get<ICategoryRepository>();
}
在运行时,存储库确实会被注入,但由于某种原因,实体永远不会被保存 - 可能是因为存储库无法判断是否有更改?还是整个请求中没有维护工作单元?
我的问题是:从程序集中动态加载时如何实现“InRequestScope”?我必须以某种方式注入内核吗?
【问题讨论】:
-
好的,所以我找到了这篇文章:stackoverflow.com/questions/4019585/…,所以这接近了答案并解决了我的问题。 kernel.Scan(a => { a.FromAssembliesInPath(directoryInfo.FullName); a.AutoLoadModules(); a.BindWithDefaultConventions(); a.**InRequestScope**(); });
标签: c# repository-pattern ninject-2