私は、永続エンティティをNHibernateセッションの同じライフタイムスコープに入れたいと思いますか?NHibernate永続エンティティをセッションの同じライフタイムスコープに入れることは可能ですか?
public class ViewModel
{
readonly Func<Owned<ISession>> _sessionFactory;
public ViewModel(Func<Owned<ISession>> sessionFactory)
{
_sessionFactory = sessionFactory;
}
public void DoSomething()
{
using (var session = _sessionFactory())
{
using (var trans = session.Value.BeginTransaction())
{
session.Value.Get<Model>(1).DoSomething();
trans.Commit();
}
}
}
}
public class Model
{
readonly ILifetimeScope _lifetimeScope;
public Model(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
public void DoSomething()
{
ISession session = _lifetimeScope.Resolve<ISession>();
Model model = session.Get<Model>(2);
model.Text = "test";
}
public string Text { get; set; }
}
詳細な返信とリンクをありがとう!私は私のプロジェクトでそれを試してみると、私はプロジェクトに大きなリファクタリングをしているので、1〜3日かかるかもしれません。 –
多くの感謝!私はこのアプローチを自分のプロジェクトでテストしました。 –