0

さて、私は自分のアプリケーション構造に行き詰まっているようです。これが私がやりたいことです:

  • UI レイヤー: ASP.NET Web フォーム Web サイト。
  • BLL: DAL 上のリポジトリを呼び出すビジネス ロジック層。
  • DAL: .EDMX ファイル (エンティティ モデル) と、各エンティティの CRUD 操作を抽象化するリポジトリ クラスを含む ObjectContext。
  • エンティティ: POCO エンティティ。持続性無知。Microsoft の ADO.Net POCO Entity Generator によって生成されます。

リポジトリで HttpContext ごとに obejctcontext を作成して、パフォーマンス/スレッドの安全性の問題を回避したいと考えています。理想的には、次のようになります。

public MyDBEntities ctx
{
    get
    {
        string ocKey = "ctx_" + HttpContext.Current.GetHashCode().ToString("x");
        if (!HttpContext.Current.Items.Contains(ocKey))
            HttpContext.Current.Items.Add(ocKey, new MyDBEntities ());
        return HttpContext.Current.Items[ocKey] as MyDBEntities ;
    }
}  

問題は、DAL (リポジトリが配置されている場所) で HttpContext にアクセスしたくないことです。しかし、どうにかして HttpContext を DAL に渡す必要があります。ここでの私の質問への回答に基づいて、IoC パターンを使用する必要があります。理想的には、多層アーキテクチャで このようなことを達成したいと考えています。

私は Autofac をチェックアウトしましたが、非常に有望なようです。しかし、多層アーキテクチャでこれをどのように達成できるかわかりません (Httpcontext を渡して、HttpContext ごとに 1 つの ObjectContext がインスタンス化されるようにする)。これを達成する方法について、誰かが私にいくつかの実例を教えてもらえますか? DAL の HttpContext に直接アクセスせずに、DAL の HttpContext を認識するにはどうすればよいですか? 多層ソリューションの設計に少し戸惑っているように感じます。

4

1 に答える 1

3

私は WebForms で IoC コンテナーを使用したことがないので、これを高レベルのソリューションとして入手してください。これはおそらくさらに改善されるはずです。

IoC プロバイダーをシングルトンとして作成してみることができます。

public class IoCProvider
{
  private static IoCProvider _instance = new IoCProvider();

  private IWindsorContainer _container;

  public IWindsorContainer
  {
    get
    {
      return _container;
    }
  }

  public static IoCProvider GetInstance()
  {
    return _instance;
  }

  private IoCProvider()
  {
    _container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
  }
}

次のようなセクションを含める必要があります (構成は以前の投稿web.configに基づいています):

<configuration>
  <configSections>    
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>

  <castle>
    <components>
      <component id="DalLayer"
                 service="MyDal.IDalLayer, MyDal"
                 type="MyDal.MyDalLayer, MyDal"
                 lifestyle="PerWebRequest">
        <!-- 
             Here we define that lifestyle of DalLayer is PerWebRequest so each
             time the container resolves IDalLayer interface in the same Web request
             processing, it returns same instance of DalLayer class
          -->
        <parameters>
          <connectionString>...</connectionString>
        </parameters>
      </component>
      <component id="BusinessLayer"
                 service="MyBll.IBusinessLayer, MyBll"
                 type="MyBll.BusinessLayer, MyBll" />
      <!-- 
           Just example where BusinessLayer receives IDalLayer as
           constructor's parameter.
        -->
    </components>
  </castle>  

  <system.Web>
    ...
  </system.Web>
</configuration>

これらのインターフェイスとクラスの実装は次のようになります。

public IDalLayer
{
  IRepository<T> GetRepository<T>();  // Simplified solution with generic repository
  Commint(); // Unit of work
}

// DalLayer holds Object context. Bacause of PerWebRequest lifestyle you can 
// resolve this class several time during request processing and you will still
// get same instance = single ObjectContext.
public class DalLayer : IDalLayer, IDisposable
{
  private ObjectContext _context; // use context when creating repositories

  public DalLayer(string connectionString) { ... }

  ...
}

public interface IBusinessLayer
{
  // Each service implementation will receive necessary 
  // repositories from constructor. 
  // BusinessLayer will pass them when creating service
  // instance

  // Some business service exposing methods for UI layer
  ISomeService SomeService { get; } 
}

public class BusinessLayer : IBusinessLayer
{
  private IDalLayer _dalLayer;

  public BusinessLayer(IDalLayer dalLayer) { ... }

  ...
}

ページの基本クラスを定義してビジネスレイヤーを公開するよりも(解決できる他のクラスでも同じことができます):

public abstract class MyBaseForm : Page
{
  private IBusinessLayer _businessLayer = null;
  protected IBusinessLayer BusinessLayer
  {
    get 
    { 
      if (_businessLayer == null)
      {
        _businessLayer = IoCProvider.GetInstance().Container.Resolve<IBusinessLayer>(); 
      }

      return _businessLayer;         
  }

  ...
}

PageHandlerFactoryカスタムを使用してページを直接解決し、依存関係を挿入する複雑なソリューション。このようなソリューションを使用する場合は、Spring.NETフレームワーク (IoC コンテナーを備えた別の API) を確認してください。

于 2011-01-06T22:22:38.733 に答える