asp.net-mvc – 在Asp.Net MVC应用程序中使用Structuremap将ISession注入我的存储
|
我的存储库都在构造函数中使用ISession: protected Repository(ISession session)
{
this.session = session;
}
private readonly ISession session;
在使用StructureMap的Asp.Net MVC应用程序中,我如何在StructureMap注册表中设置ISession?我还需要将SessionFactory添加到容器中吗? FluentNHibernate改变了什么吗? 解决方法您应该使用工厂方法注册ISession.另一种选择(并不总是最好,但易于使用)是: 实现ISession和ISessionFactory接口(SessionProxy和SessionFactoryProxy). public class SessionAggregator : ISession {
protected ISession session;
public SessionAggregator(ISessionFactory theFactory) {
if (theFactory == null)
throw new ArgumentNullException("theFactory","theFactory is null.");
Initialise(theFactory);
}
protected virtual void Initialise(ISessionFactory factory) {
session = factory.OpenSession();
}
// the ISession implementation - proxy calls to the underlying session
}
public class SessionFactoryAggregator : ISessionFactory {
protected static ISessionFactory factory;
private static locker = new object();
public SessionFactoryAggregator() {
if (factory == null) {
lock(locker) {
if (factory == null)
factory = BuildFactory();
}
}
}
// Implement the ISessionFactory and proxy calls to the factory
}
这样您就可以注册ISession(由SessionAggregator实现)和ISessionFactory(SessionFactoryAggreagator),任何DI框架都可以轻松解析ISession. 如果您的DI不支持工厂方法(我不知道结构图是否支持),这很好. 我已将这些实现添加到我的Commons程序集中,所以我不应该每次都重新实现它. 编辑:现在,在Web应用程序中使用ISession: >在结构图中注册SessionFactoryAggregator(生命周期可以是单例). 代码可能如下所示: // The Registry in StructureMap
ForRequestedType<ISessionFactory>()
.CacheBy(InstanceScope.Singleton)
.TheDefaultIsConcreteType<SessionFactoryAggregator>();
ForRequestedType<ISession>()
.CacheBy(InstanceScope.Hybryd)
.TheDefaultIsConcreteType<SessionAggregator>();
// Then in EndRequest call
HttpContextBuildPolicy.DisposeAndClearAll() (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 为什么我的动作方法不会超时?
- asp.net – ASP计数器 – 不同计数器“桶”中类似命名的计数
- asp.net-mvc – ASP.NET MVC项目架构
- ASP.NET网站攻击:如何回应?
- asp.net-mvc – TDD:在ASP.NET MVC 3中测试DataAnnotation
- asp.net webapi UseOAuthBearerAuthentication vs UseJwtBe
- asp.net – 没有owin.Environment项目在上下文中找到
- 如何在ASP.NET MVC 4 Beta中禁用Javascript/CSS缩小
- 如何在ASP.NET中以程序方式模拟HTTP POST?
- 在使用ASP.NET会话时是否可以强制请求并发?
- asp.net – 在Web.config帮助中定义tagPrefixes
- asp.net-mvc-3 – 如何让User.Identity在控制器外
- asp.net-mvc – IIS显示服务器错误而不是自定义错
- asp.net-mvc – ASP.Net MVC捆绑和分类
- asp.net-mvc – 在Umbraco 6.1.1 MVC 4中,如何使
- 详解ASP.NET Core 中的框架级依赖注入
- asp.net-mvc – 如何在asp.net mvc中的url中添加
- asp.net – 如何将下拉列表添加为gridview项
- asp.net – 为什么aspnet_users使用guid来代替id
- asp.net – IIS7中的SQL Server和Windows身份验证
