4

リポジトリパターンの例をたくさん見つけました。それらはすべて、リポジトリが独自の接続ライフサイクルを管理していることを示しています。複数のリポジトリ間で単一の接続を共有したい場合、人々はどのように対処するのだろうかと思いました。

私が尋ねている主な理由は、TransactionScopeを使用してトランザクションを作成するときに、DTCトランザクションにエスカレートしたくないからです。セットアップは簡単ですが、少し重いようです。

私が考えていたのは、接続のライフサイクルを管理するTransactionScopeに似たものを使用することでした。ただし、データアクセスをビジネスプロセスに少しリンクしているようです。私が言っていることの例は次のとおりです。

//As DataScope will handle connections, then repositories will be created from them
//in order to share the connection.
using(DataScope scope = new DataScope())
{
   scope.GetRepository<CustomerRepository>.FindById(10)
}
4

2 に答える 2

5

リポジトリを作成するとき、リポジトリのコンストラクターで、使用したい接続/ unitofwork / ISession(nhibernate)を渡します。

リポジトリは、それが使用されるコンテキストを認識していないため、接続/ISession自体の作成を担当するべきではありません。CastleやSpring.NETなどの制御の反転を使用することもできます。

于 2009-07-09T08:49:06.783 に答える
1

I have created session factory class for each database connection that I need.

So consider if you have 2 databases: Backoffice database and Checkout database, My session factory would look like below:

public class BackOfficeSessionFactoryCreator : ISessionFactoryCreator
{
    public ISessionFactory CreateSessionFactory()
    {
        var sessionFactory =Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager
        .AppSettings["FluentNHibernateConnectionForBackOffice"]))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>())
        .ExposeConfiguration(c => c.SetProperty("command_timeout",ConfigurationManager
        .AppSettings["FluentNHibernateCommandTimeout"]));

        return sessionFactory.BuildSessionFactory();
    }
}



public class CheckoutSessionFactoryCreator : ISessionFactoryCreator
{
    public ISessionFactory CreateSessionFactory()
    {
        var sessionFactory =Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager
        .AppSettings["FluentNHibernateConnectionForCheckout"]))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<CustomerCheckOut>())
        .ExposeConfiguration(c => c.SetProperty("command_timeout",ConfigurationManager
        .AppSettings["FluentNHibernateCommandTimeout"]));

        return sessionFactory.BuildSessionFactory();
    }
}


public interface ISessionFactoryCreator
{
    ISessionFactory CreateSessionFactory();
}
于 2011-10-12T15:45:18.590 に答える