3

次のように datacontext をプロパティとして設定する際に潜在的な問題はありますか?

リポジトリ

public Repository()
{ 
    public DataContext dc {get;set;}

    public GetOrders(int id)
       { ...from dc.Orders...}
}

サービス層:

public GetNewOrders()
   {
       ....
       Repository rep=new Repository();
       using {DataContext dc=new DataContext())
        { 
           rep.dc=dc;
           rep.GetOrders(id);
        }
    }
4

2 に答える 2

3

DDD では、concret クラスを参照することで、全体像を見落としています。ベスト プラクティスでは、リポジトリと「サービス レイヤー」の間でインターフェイスしていません。リポジトリに DataContext を挿入する必要がある場合は、次のようにリファクタリングすることをお勧めします。

public interface IRepository
{
  IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
  private IDataContext _dataContext;
  public Repository(IDataContext dataContext)
  {
    _dataContext = dataContext;
  }
  public IList<Orders> GetNewOrders()
  {
    // perform your actions on _dataContext here
  }
}

より良い解決策は、リポジトリが DataContext を独自に処理できるようにすることです。基本的な要件をマスクすることで、コンサートの分離を有効に保ちます。

public interface IRepository
{
  IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
  private IDataContext _dataContext;
  public Repository(String connectionString)
  {
    _dataContext = new DataContext(connectionString);
  }
  public IList<Orders> GetNewOrders()
  {
    // perform your actions on _dataContext here
  }
} 

DataContext (または別のクラス) の制御を自分で保持する必要がある場合 (おそらく、静的な参照を維持したり、WebRequest に基づいて設定を変更したりしたい場合など)、「Factory」を使用する必要があります。

ファクトリは次のようになります。

public static class DataContextFactory
{
  public static IDataContext GetInstance()
  {
    // return either a static instance, 
    // or threaded instance, a GlobalContext instance
    // or whatever your preference is here
    // 
  }
}

そうすれば、DataContext のインスタンスを「サービス」レイヤーの外側で制御する方法を完全に制御できます。したがって、この DataContextFactory を次のように使用します。

public interface IRepository
{
  IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
  public IList<Orders> GetNewOrders()
  {
    using (var dataContext = DataContextFactory.GetInstance())
    {
      // dataContext is now your IDataContext to work with
    }
  }
} 

「IRepository へのアクセス方法は?」あなたは尋ねることができますか?

サービス層は次のようになります。

public void GetNewOrdersForServices()
{
  // Not recommended!
  //      IRepository repo = new Repository()
  //
  // The following is recommended instead; because, it removes the
  // the Concret reference from your Services layer completely!
  //
  IRepository repo = ServiceLocator.InstanceOf<IRepository>();
  IList myList = repo.GetNewOrders();

}

または、お気に入りの Inversion of Control コンテナーを次のように使用して、サービスのコンストラクターに挿入します。

public class OrderService
{
  private IRepository _repo;

  public OrderService(IRepository repo)
  {
    _repo = repo;
  }

  public void GetNewOrdersForServices()
  {
    IList myList = _repo.GetNewOrders();

  }

サービスロケーターの概念に慣れていない場合は、ほぼすべてのニーズをカプセル化するキャッスルウィンザーをチェックしてください。

于 2009-03-05T18:58:06.343 に答える