を定義domain model
したら、残りの作業を行う方法を理解したいと思います。
データ アクセス層
UnitOfWork
私は以前に、独自の実装をコーディングする必要がないことを読んでいISession
ました (それを行う方法について多くの情報を見つけました)。だから私はかなり混乱しています..私は次のようなリポジトリインターフェースを持っています:
public interface IRepository<T> where T: AbstractEntity<T>, IAggregateRoot
{
T Get(Guid id);
IQueryable<T> Get(Expression<Func<T, Boolean>> predicate);
IQueryable<T> Get();
T Load(Guid id);
void Add(T entity);
void Remove(T entity);
void Remove(Guid id);
void Update(T entity);
void Update(Guid id);
}
具体的な実装には、次の 2 つのオプションがあります。
オプションA
ISessionFactory
コンストラクターを介して注入し、次のようなものを用意します。
public class Repository<T> : IRepository<T> where T : AbstractEntity<T>, IAggregateRoot
{
private ISessionFactory sessionFactory;
public Repository(ISessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public T Get(Guid id)
{
using(var session = sessionFactory.OpenSession())
{
return session.Get<T>(id);
}
}
}
オプション B
NHibernateHelper
クラスを使用することです
using(var session = NHibernateHelper.GetCurrentSession())
{
return session.Get<T>(id);
}
どこNHibernateHelper
ですか
internal sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if(currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if(currentSession == null)
{
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if(sessionFactory != null)
{
sessionFactory.Close();
}
}
}
どのオプションが優先されますか?
なぜ(注射以外に)?
オプションを使用する場合A
、構成はどこに配置しますISessionFactory
か?
ASP.NET MVC
プロジェクトのどこかに配置する必要がありますか?どのように?
モンスターの質問を読んでくれてありがとう!ご指導よろしくお願いします!