0

セッションとトランザクションに DAOFactory と NHibernate Helper を使用するサービスを実装しました。次のコードは非常に単純化されています。

public interface IService
{
    IList<Disease> getDiseases();
}

public class Service : IService
{
    private INHibernateHelper NHibernateHelper;
    private IDAOFactory DAOFactory;

    public Service(INHibernateHelper NHibernateHelper, IDAOFactory DAOFactory)
    {
        this.NHibernateHelper = NHibernateHelper;
        this.DAOFactory = DAOFactory;
    }

    public IList<Disease> getDiseases()
    {
        return DAOFactory.getDiseaseDAO().FindAll();
    }
}

public class NHibernateHelper : INHibernateHelper
{
    private static ISessionFactory sessionFactory;

    /// <summary>
    /// SessionFactory is static because it is expensive to create and is therefore at application scope.
    /// The property exists to provide 'instantiate on first use' behaviour.
    /// </summary>
    private static ISessionFactory SessionFactory
    {
        get
        {
            if (sessionFactory == null)
            {
                try
                {
                    sessionFactory = new Configuration().Configure().AddAssembly("Bla").BuildSessionFactory();
                }
                catch (Exception e)
                {
                    throw new Exception("NHibernate initialization failed.", e);
                }
            }
            return sessionFactory;
        }
    }

    public static ISession GetCurrentSession()
    {
        if (!CurrentSessionContext.HasBind(SessionFactory))
        {
            CurrentSessionContext.Bind(SessionFactory.OpenSession());
        }
        return SessionFactory.GetCurrentSession();
    }

    public static void DisposeSession()
    {
        var session = GetCurrentSession();
        session.Close();
        session.Dispose();
    }

    public static void BeginTransaction()
    {
        GetCurrentSession().BeginTransaction();
    }

    public static void CommitTransaction()
    {
        var session = GetCurrentSession();
        if (session.Transaction.IsActive)
            session.Transaction.Commit();
    }

    public static void RollbackTransaction()
    {
        var session = GetCurrentSession();
        if (session.Transaction.IsActive)
            session.Transaction.Rollback();
    }
}

結局のところ、IService を ASP.NET MVC/コンソール アプリケーション/Winform に公開したいだけです。コンソール アプリケーションで既にサービスを使用できますが、まずそれを改善したいと考えています。最初の改善は、Castle を介して INHibernateHelper と IDAOFactory のインターフェイスを挿入することだと思います。しかし、問題は、NHibernateHelper が「要求ごとの Nhibernate セッション」パターンに従って実行されるべき asp.net コンテキストで問題を引き起こす可能性があることだと思います。私が持っている 1 つの質問は、このパターンが nhibernate 構成セクション (current_session_context_class = web の設定) によって決定されるのか、それとも城を介してこれを制御できるのかということです。

これが理にかなっていることを願っています。最終的な目的は、THE IService を公開することです。

ありがとう。

キリスト教徒

4

1 に答える 1

0

2 つの選択肢があります..

1) WCF でホストします。これにより、任意のソースからアクセスできます。

2) コードの使用方法に固有のすべてを抽象化します。たとえば、私たちのシステムでは、コードが実行されている場所に基づいて異なる方法で格納される独自の Unit Of Work 実装を使用しています。小さな例は、現在のスレッドと比較して、WCF 呼び出しコンテキストを使用して何かを保存することです。

于 2010-03-04T20:36:07.940 に答える