gpt4 book ai didi

nhibernate - NHibernate CaSTLe Facility的延迟加载

转载 作者:行者123 更新时间:2023-12-03 12:21:10 24 4
gpt4 key购买 nike

我是否必须关闭CaSTLe的NHibernate的ISession生成的ISessionManager?如何处理那些ISession的交易?我还是NHibernate的新手。

编辑:我想延迟加载,但收到以下消息:

Initializing[failed to lazily initialize a collection of role: , no session or session was closed"



这是我继承的通用存储库,以实现特定的实例。
[Transactional]
public class Repository<TKey, TModel>
: IRepository<TKey, TModel>
where TKey : IComparable
where TModel : class
{
private readonly ISessionManager _sessionManager;

protected ISession Session { get { return _sessionManager.OpenSession(); } }

public Repository(ISessionManager sessionManager)
{
_sessionManager = sessionManager;
}
#region IRepository<TKey,TModel> Members

public virtual TModel Select(TKey key)
{
using (var session = _sessionManager.OpenSession())
{
return session.Get<TModel>(key);
}
}

public virtual IList<TModel> SelectWhere(Func<TModel, bool> query)
{
using (var session = Session)
{
return session.Linq<TModel>().Where(query).ToList();
}
}

public virtual TModel Single(Func<TModel, bool> query)
{
using (var session = Session)
{
return session.Linq<TModel>().SingleOrDefault(query);
}
}

public virtual TModel First(Func<TModel, bool> query)
{
using (var session = Session)
{
return session.Linq<TModel>().FirstOrDefault(query);
}
}

public virtual IList<TModel> All()
{
using (var session = Session)
{
return session.Linq<TModel>().ToList();
}
}

[Transaction(TransactionMode.Requires)]
public virtual void Store(TModel entity)
{
using (var session = Session)
{
session.SaveOrUpdate(entity);
}
}

[Transaction(TransactionMode.Requires)]
public virtual void Store(IEnumerable<TModel> entities)
{
using (var session = Session)
{
foreach (TModel entity in entities)
session.SaveOrUpdate(entity);
}
}


[Transaction(TransactionMode.Requires)]
public virtual void Remove(TModel entity)
{
using (var session = Session)
{
session.Delete(entity);
}

}

public virtual void Remove(Func<TModel, bool> query)
{
IEnumerable<TModel> entities = SelectWhere(query);
Remove(entities);
}

[Transaction(TransactionMode.Requires)]
public virtual void Remove(IEnumerable<TModel> entities)
{
using (var session = Session)
{
foreach (TModel entity in entities)
session.Delete(entity);
}
}

#endregion
}

public class Repository<TModel>
: Repository<Guid, TModel>, IRepository<TModel>
where TModel : class
{
public Repository(ISessionManager sessionManager) : base(sessionManager) { }
}

public class Repository
: Repository<ulong, object>, IRepository
{
public Repository(ISessionManager sessionManager) : base(sessionManager) { }
}

这是该存储库的示例调用:
IUserRepository userRepository = new UserRepository(); // This is actually provided by my IoC

var users = userRepository.All();
foreach (var user in Users)
{
foreach (var picture in user.Pictures)
{
// I get exceptions when I do stuff like this.
}
}

最佳答案

是的,请始终处置ISession。参见the docs on ISessionManager usage

对于事务,请考虑使用Automatic Transaction Facility

SessionManager is ATM-aware因此,即使您显然已经处置了ISession,它也将在需要时巧妙地处置ISession,并将事务考虑在内。

使用ASP.NET MVC + CaSTLe自动事务处理功能+ NHibernate功能的Here's a quick & dirty sample app

关于nhibernate - NHibernate CaSTLe Facility的延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1117633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com