エンティティマネージャー:3層/レイヤーアプリケーションの質問
-
26-10-2019 - |
質問
私はシンプルな3層プロジェクトを作成しようとしていますが、ビジネスロジックレイヤーの実装を順調に進んでいます。これは私のBLがどのように見えるかです
//The Entity/BO
public Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public UserAccount UserAccount { get; set; }
public List<Subscription> Subscriptions { get; set; }
}
//The BO Manager
public class CustomerManager
{
private CustomerDAL _dal = new CustomerDAL();
private Customer _customer;
public void Load(int customerID)
{
_customer = GetCustomerByID(customerID);
}
public Customer GetCustomer()
{
return _customer;
}
public Customer GetCustomerByID(int customerID)
{
return _dal.GetCustomerByID(customerID);
}
public Customer GetCustomer()
{
return _customer;
}
public UserAccount GetUsersAccount()
{
return _dal.GetUsersAccount(_customer.customerID);
}
public List<Subscription> GetSubscriptions()
{
// I load the subscriptions in the contained customer object, is this ok??
_customer.Subscriptions = _customer.Subscriptions ?? _dal.GetCustomerSubscriptions(_customer.CustomerID);
return _customer.Subscriptions;
}
ご存知かもしれませんが、私のオブジェクトマネージャーは本当に私の実際のオブジェクト(顧客)の単なるコンテナであり、これが私のビジネスロジック、ビジネスエンティティをビジネスロジックに切り離す方法を置く場所です。これが私が通常使用する方法です
int customerID1 = 1;
int customerID2 = 2;
customerManager.Load(1);
//get customer1 object
var customer = customerManager.GetCustomer();
//get customer1 subscriptions
var customerSubscriptions = customerManager.GetSubscriptions();
//or this way
//customerManager.GetSubscriptions();
//var customerSubscriptions = customer.Subscriptions;
customerManager.Load(2);
//get customer2
var newCustomer = customerManager.GetCustomer();
//get customer2 subscriptions
var customerSubscriptions = customerManager.GetSubscriptions();
ご覧のとおり、一度に1つのオブジェクトしか保持していません。顧客のリストを管理する必要がある場合は、おそらく別のマネージャーを作成する必要があります。 CustomerListManager
私の質問は、これが3層/レイヤーデザインのBLを実装する正しい方法ですか?または、どのように実装するかについての提案。ありがとう。
解決
他の人が前に述べたように、あなたは リポジトリ パターン。また、チェックアウトすることもお勧めします 仕事の単位 パターンだけでなく ドメイン駆動型デザイン 一般的なアプリケーションのアーキテクチャ用。
また、オブジェクトリレーショナルマッピングを調べることもできます(orm).NETのようなフレームワーク エンティティフレームワーク また nhibernate それがあなたのためのオプションである場合。
ここにヘッドスタートを提供するために、適切な道路に行くための素晴らしいリソースがあります。
本
- ドメイン駆動型のデザインとパターンを適用します ジミー・ニルソン
- ドメイン駆動型デザイン エリック・エヴァンス
- ドメイン駆動型のデザイン (無料の電子ブック)
オンライン参照
- データの持続性のための設計パターン ジェレミー・ミラー
- リポジトリは新しいシングルトンです Ayende Rahienによる
- リポジトリパターン (MSDN)
- 仕事のパターンと永続性の無知の単位 ジェレミー・ミラー
それが役立つことを願っています。
他のヒント
リポジトリパターンを検索してみてください。あなたのニーズに応えると思います。
所属していません StackOverflow