私はDDDを初めて使用するので、初心者です。だから、それは非常に簡単だとしましょう:D私はasp.net mvc 2、ddd、nhibernateを使用してアプリケーションを開発しました。クラスライブラリにドメインモデルがあり、別のクラスライブラリにリポジトリがあり、asp.netmvc2アプリケーションがあります。私のリポジトリ基本クラスには、注入する構造と依存関係(global.asaxで開始された独自のISessionFactoryオブジェクト)があります。コードは次のとおりです。
public class Repository<T> : IRepository<T>
where T : Entidade
{
protected ISessionFactory SessionFactory { get; private set; }
protected ISession Session
{
get { return SessionFactory.GetCurrentSession(); }
}
protected Repository(ISessionFactory sessionFactory)
{
SessionFactory = sessionFactory;
}
public void Save(T entity)
{
Session.SaveOrUpdate(entity);
}
public void Delete(T entity)
{
Session.Delete(entity);
}
public T Get(long key)
{
return Session.Get<T>(key);
}
public IList<T> FindAll()
{
return Session.CreateCriteria(typeof(T)).SetCacheable(true).List<T>();
}
}
そして、私がこのような特別なリポジトリを持った後:
public class DocumentRepository : Repository<Domain.Document>, IDocumentRepository
{
// constructor
public DocumentRepository (ISessionFactory sessionFactory) : base(sessionFactory)
{ }
public IList<Domain.Document> GetByType(int idType)
{
var result = Session.CreateQuery("from Document d where d.Type.Id = :IdType")
.SetParameter("IdType", idType)
.List<Domain.Document>();
return result;
}
}
このコードにはトランザクションの制御がなく、正常に機能していますが、asp.net mvcのコントローラーでこのリポジトリを制御するために、次のような単純なものを作成したいと思います。
using (var tx = /* what can I put here ? */) {
try
{
_repositoryA.Save(objA);
_repositoryB.Save(objB);
_repositotyC.Delete(objC);
/* ... others tasks ... */
tx.Commit();
}
catch
{
tx.RollBack();
}
}
NHibernateUnitOfWorkについて聞いたことがありますが、わかりません:(、リポジトリで動作するようにNHibernateUnitOfWorkを構成するにはどうすればよいですか?単純なリポジトリを変更する必要がありますか?推測は大歓迎です!
だから、誰かがここまで読んでくれたらありがとう!私を助けることができれば、私は感謝します!
PS:私の英語でごめんなさい!
さようなら=D