0

IA12 つのアプリケーション インターフェイスを介してアクセスする 2 つのドメインがありIA2、それぞれが異なるリポジトリR1と を使用しますR2。リポジトリは、NHibernate セッションを作成できるインターフェースを使用しますISimpleSessionFactory

リポジトリがデータベースを共有している場合、unity コンテナーを使用して、次のように両方をセットアップします。

var unity = new UnityContainer();

unity.RegisterType<ISimpleSessionFactory, NHibernateSessionFactory>(
                new ContainerControlledLifetimeManager(), new InjectionConstructor("ConnectionA"));

unity.RegisterType<IA1, A1>();
unity.RegisterType<R1>();

unity.RegisterType<IA2, A2>();
unity.RegisterType<R2>();

しかし、それらは接続文字列を共有していないので、次のようなことをしたいと思います:

var unity = new UnityContainer();

var child1 = unity.CreateChildContainer();

child1.RegisterType<ISimpleSessionFactory, NHibernateSessionFactory>(
                new ContainerControlledLifetimeManager(), new InjectionConstructor("ConnectionA"));

child1.RegisterType<IA1, A1>();
child1.RegisterType<R1>();

var child2 = unity.CreateChildContainer();

child2.RegisterType<ISimpleSessionFactory, NHibernateSessionFactory>(
                new ContainerControlledLifetimeManager(), new InjectionConstructor("ConnectionB"));

child2.RegisterType<IA2, A2>();
child2.RegisterType<R2>();

しかし、私の問題は、クラスから解決IA2したいということです。A1理想的には、セッション ファクトリ以外のすべてを親コンテナーに格納したいと考えています。それは単なるリポジトリでR1ありR2、異なるISimpleSessionFactorys が必要ですが、私が理解しているように、子でローカルに解決されない場合にのみ親にフォールバックするため、何かを親に移動するとセッションファクトリが見つかりません。

4

2 に答える 2

0

より良い答えが得られるまで、2 つのセッション ファクトリに別々のインターフェイスを定義して、両方を同じコンテナに登録できるようにしています。

interface IASessionFactory : ISimpleSessionFactory{}
interface IBSessionFactory : ISimpleSessionFactory{}

class ASessionFactory : NHibernateSessionFactory, IASessionFactory{...}
class BSessionFactory : NHibernateSessionFactory, IBSessionFactory{...}

var unity = new UnityContainer();

unity.RegisterType<IASessionFactory, ASessionFactory>(
                new ContainerControlledLifetimeManager(), new InjectionConstructor("ConnectionA"));

unity.RegisterType<IBSessionFactory, BSessionFactory>(
                new ContainerControlledLifetimeManager(), new InjectionConstructor("ConnectionB"));

unity.RegisterType<IA1, A1>();
unity.RegisterType<R1>();

unity.RegisterType<IA2, A2>();
unity.RegisterType<R2>();

R1andR2を使用する必要がIASessionFactoryありIBSessionFactoryますISimpleSessionFactory

于 2013-07-15T09:11:02.407 に答える