エンティティマネージャー:3層/レイヤーアプリケーションの質問

StackOverflow https://stackoverflow.com/questions/6318462

  •  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 それがあなたのためのオプションである場合。

ここにヘッドスタートを提供するために、適切な道路に行くための素晴らしいリソースがあります。

オンライン参照

それが役立つことを願っています。

他のヒント

リポジトリパターンを検索してみてください。あなたのニーズに応えると思います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top