ead*_*dam 22 asp.net-mvc ninject telerik-open-access
我试图第一次使用Ninject和OpenAccess.请帮我处理以下内容.这是我的项目看起来像......
public class ContentController : Controller
{
private ContentService contentSvc;
public ContentController(ContentService contentSvc)
{
this.contentSvc = contentSvc;
}
}
Run Code Online (Sandbox Code Playgroud)
以下类位于我的Web应用程序中的文件夹下.
public class ContentService
{
private IContentRepository contentRepository;
public ContentService(IContentRepository contentRepository)
{
this.contentRepository = contentRepository;
}
public void InsertContent(Content content)
{
contentRepository.InsertContent(content);
}
}
Run Code Online (Sandbox Code Playgroud)
以下存储库属于单独的程序集.
public class ContentRepository : IContentRepository
{
DBContext db;
public ContentRepository(DBContext _db)
{
db = _db;
}
public void InsertContent(Content content)
{
db.Add(content);
}
}
Run Code Online (Sandbox Code Playgroud)
这是Ninject绑定的样子..
kernel.Bind<ContentService>().To<ContentService>().InRequestScope();
kernel.Bind<IContentRepository>().To<ContentRepository>().InRequestScope().WithConstructorArgument("_db", new DBContext());
Run Code Online (Sandbox Code Playgroud)
如果我一次取一页,一切正常.我正在使用一个简单的工具'XENU'来同时获取多个页面.这是我通过一次获取多个页面而获得DBContext错误的时候.
我不确定Ninject是否在每个REQUEST中处理DBContext?我得到不同的错误,例如'对象引用未设置为对象的实例.'或者'ExecuteReader需要一个开放且可用的连接.连接的当前状态是打开的.
我在我的MVC网络应用程序的文件夹下有ContentService.ContentRepository是一个单独的程序集.我将在ContentService中添加业务逻辑,并仅将"ContentRepository"用于CRUD操作.另外,如果这个架构没问题,或者有更好的方法来创建服务和存储库,请告诉我.
Not*_*ple 29
这是我如何做你的Ninject绑定,
kernel.Bind<DBContext>().ToSelf().InRequestScope();
kernel.Bind<ContentService>().ToSelf().InRequestScope();
kernel.Bind<IContentRepository>().To<ContentRepository>().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,使用EF和Ninject,此模式应该可以正常工作.
| 归档时间: |
|
| 查看次数: |
16612 次 |
| 最近记录: |