asp.net mvc 2应用程序中的IoC和dataContext配置
我有 Global.asax
就像下面的代码:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
// ....
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(typeof(IOCControllerFactory));
}
}
public class IOCControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public IOCControllerFactory()
{
kernel = new StandardKernel(new NanocrmContainer());
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return base.GetControllerInstance(requestContext, controllerType);
var controller = kernel.TryGet(controllerType) as IController;
if (controller == null)
return base.GetControllerInstance(requestContext, controllerType);
var standartController = controller as Controller;
if (standartController is IIoCController)
((IIoCController)standartController).SetIoc(kernel);
return standartController;
}
class NanocrmContainer : Ninject.Modules.NinjectModule
{
public override void Load()
{
// ...
Bind<DomainModel.Entities.db>().ToSelf().InRequestScope().WithConstructorArgument("connection", "Data Source=lims;Initial Catalog=nanocrm;Persist Security Info=True;User ID=***;Password=***");
}
}
}
在这种情况下,如果在某个地方它是类,定义如下:
public class UserRepository : IUserRepository
{
private db dataContext;
private IUserGroupRepository userGroupRepository;
public UserRepository(db dataContext, IUserGroupRepository userGroupRepository)
{
this.dataContext = dataContext;
this.userGroupRepository = userGroupRepository;
}
}
然后创建 dataContext
实例(如果没有创建)在此请求范围内)由 Ninject 提供。
所以现在的麻烦是 - 在哪里调用 dataContext
方法 .Dispose()
?
UPD:
所以我听从了KeeperOfTheSoul的建议并以这种方式解决了这个问题:
public override void ReleaseController(IController controller)
{
base.ReleaseController(controller);
var db = kernel.Get<DomainModel.Entities.db>();
db.Dispose();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理此问题的好地方是 IControllerFactory.ReleaseController,例如
在 NInject 中,这可以通过使用 激活块,在创建控制器时请求开始时您可以将激活块存储在HttpContext的当前项中,在ReleaseController期间您可以检索之前创建的激活块并将其丢弃。
您还可以考虑使用
InScope
并让自定义范围实现INotifyWhenDispose
。之后的用法与激活块相同,只不过现在您将范围存储在 HttpContext 的当前项中。A good place to handle this is in IControllerFactory.ReleaseController, eg
In NInject this could be handled by scoping using an activation block, at the start of the request when creating the controller you can store the activation block in the HttpContext's current items, during ReleaseController you can retrieve the previously created activation block and dispose it.
You could also consider using
InScope
and having the custom scope implementINotifyWhenDisposed
. After that the usage is the same as with an activation block, except now you store the scope in the HttpContext's current items.有时用于处置数据库连接的一种模式是从终结器调用
Dispose
。A pattern that is sometimes used to dispose db connections is to call
Dispose
from the finaliser.您可以将其挂接到 Application_EndRequest 中。
You could hook it into Application_EndRequest.