6

Webアプリケーション内でAutofacを使用してNHibernateトランザクションを管理するための最良のアプローチは何ですか?

セッションへの私のアプローチは

builder.Register(c => c.Resolve<ISessionFactory>().OpenSession())
       .ContainerScoped();

については、Google Code でITransactionを見つけましたが、ロールバックするかどうかを決定する際に依存しています。HttpContext.Current.Error

より良い解決策はありますか?また、NHibernate トランザクションにはどのようなスコープが必要ですか?

4

3 に答える 3

4

私は少し前にこれを投稿しました:

http://groups.google.com/group/autofac/browse_thread/thread/f10badba5fe0d546/e64f2e757df94e61?lnk=gst&q=transaction#e64f2e757df94e61

インターセプターにロギング機能があり、[トランザクション]属性もクラスで使用できるように変更されました。

[global::System.AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class TransactionAttribute : Attribute
{
}


public class ServicesInterceptor : Castle.Core.Interceptor.IInterceptor
{
    private readonly ISession db;
    private ITransaction transaction = null;

    public ServicesInterceptor(ISession db)
    {
        this.db = db;
    }

    public void Intercept(IInvocation invocation)
    {
        ILog log = LogManager.GetLogger(string.Format("{0}.{1}", invocation.Method.DeclaringType.FullName, invocation.Method.Name));

        bool isTransactional = IsTransactional(invocation.Method);
        bool iAmTheFirst = false;

        if (transaction == null && isTransactional)
        {
            transaction = db.BeginTransaction();
            iAmTheFirst = true;
        }

        try
        {
            invocation.Proceed();

            if (iAmTheFirst)
            {
                iAmTheFirst = false;

                transaction.Commit();
                transaction = null;
            }
        }
        catch (Exception ex)
        {
            if (iAmTheFirst)
            {
                iAmTheFirst = false;

                transaction.Rollback();
                db.Clear();
                transaction = null;
            }

            log.Error(ex);
            throw ex;
        }
    }

    private bool IsTransactional(MethodInfo mi)
    {
        var atrClass = mi.DeclaringType.GetCustomAttributes(false);

        foreach (var a in atrClass)
            if (a is TransactionAttribute)
                return true;

        var atrMethod = mi.GetCustomAttributes(false);

        foreach (var a in atrMethod)
            if (a is TransactionAttribute)
                return true;

        return false;
    }
}
于 2009-11-01T17:13:23.743 に答える
4

autofac を使用する場合、同じコンテナー スコープのメソッドを使用しますが、同じセッションをリポジトリ/DAO オブジェクトに渡す代わりに、コンテナー スコープの UnitOfWork を渡します。Unit of work のコンストラクターにはこれがあります。

    private readonly ISession _session;
    private ITransaction _transaction;

    public UnitOfWork(ISession session)
    {
        _session = session;
        _transaction = session.BeginTransaction();
    }

そして処分は次のとおりです。

    public void Dispose()
    {
        try
        {
            if (_transaction != null &&
                            !_transaction.WasCommitted &&
                            !_transaction.WasRolledBack)
                _transaction.Commit();
            _transaction = null;
        }
        catch (Exception)
        {
            Rollback();
            throw;
        }

    }

これを管理するために、autofac で決定論的処理を (ab) 使用しています。

もう 1 つは、基本的に ASPNet 環境のみを対象としており、トランザクションが Web 要求に関連付けられているという意識的な決定を下したことです。したがって、Web 要求パターンごとのトランザクション。

そのため、IHttpModule で次のエラー処理コードを実行できます。

    void context_Error(object sender, System.EventArgs e)
    {
        _containerProvider.RequestContainer.Resolve<IUnitOfWork>().Rollback();
    }

私は NHibernate.Burrow をあまり詳しく見ていませんが、これのほとんどを行う何かがあると確信しています。

于 2009-11-04T15:25:50.647 に答える
-1

私は通常、自分でトランザクションを管理します..

public ActionResult Edit(Question q){
try {
 using (var t = repo.BeginTransaction()){
  repo.Save(q);
  t.Commit();
  return View();
 }
 catch (Exception e){
  ...
 }
}
于 2009-10-30T21:49:39.557 に答える