Ninject 从程序集中动态加载存储库
我正在使用:
- EF 4.1
- MVC 3
- Ninject
- 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”?我必须以某种方式注入内核吗?
I'm using:
- EF 4.1
- MVC 3
- Ninject
- Ninject.Extensions.Conventions
- Ninject.Web.Mvc
The app uses the repository pattern.
My Repositories can be injected like this:
kernel.Bind<ICategoryRepository>().To<CategoryRepository>().InRequestScope();
and it all works fine :-)
But i've neen attempting to go further with dynamically injecting from an asssembly like this in my 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>();
}
At run time the repository does get injected, but for some reason the entity never gets saved - perhaps because the repository can't tell if there are changes? or the unit of work is not maintained across the request?
My question is: How do i implement a "InRequestScope" when dynamically loading from assemblies? Do i have to somehow inject the kernel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种方法(标记为
***
)回答并解决了问题(复制自 @John Barrett 的评论):This approach (marked
***
) answers and solves the problem (copied from @John Barrett's comment):