2011-07-26 9 views
1

私はデータアクセスオブジェクトのライブラリを持っており、ロジックレイヤの設計を担当しています。このレイヤーでは、レンダリングのためにUIに渡すことができるProfileオブジェクトを作成するために必要なコアデータモデルにアクセスできます。カプセル化されたオブジェクトの種類に応じて派生クラスを初期化します。

Profileオブジェクトを必要とするデータモデル内のすべてのオブジェクトは、タイプPageから派生しています。理想的には、Pageをパラメータとして受け取り、Profileを返すメソッドを書く必要があります。ただし、オブジェクトはAppsのグループに分割されているため、これほど簡単ではありません。ユーザーが有効にすることができます。

は、私は、様々な異なるアプローチを試してみました(と全体の多くを削除して、再度起動し続ける!)が、ここで私は、現時点ではしようとしているソリューションですしました:

public interface IApp 
{ 
    //should be static, but interfaces cannot define static properties 
    //so this instance property will return a private static field 
    Dictionary<Type, IProfileCreator> Profiles { get; } 

    //other things the App contains that isn't relevant to the profiles 
} 

public interface IProfile 
{ 
    Page page { get; set; } 
} 

public interface IProfileCreator 
{ 
    IProfile Create(Page page); 
} 

public class ProfileCreator<TProfile> where TProfile : IProfile, new() 
{ 
    IProfile IProfileCreator.Create(Page page) 
    { 
     return Create(page); 
    } 

    public TProfile Create(Page page) 
    { 
     //constructor will have to be blank because of the new() constraint 
     TProfile profile = new TProfile(); 
     profile.Page = page; 
     return profile; 
    } 
} 

は、私はかなり24を作成する必要があります大きなページのためのクラスProfileがあるので、私はコーディングを開始する前に私ができる最良の方法をしていることを確認したいと思います。このデザインにはいくつかの欠陥がありますが、これを行うにはより良い方法がありますか?誰もが前に似たようなことを試みたことがありますか(この状況はそれほど稀ではありません)?

答えて

2

このFactoyパターン(source)を見て:次に、2つのアプリケーションは、オブジェクトの作成の実装は一度だけ行われます、オブジェクトの同じ種類を持っているかもしれません

abstract class ProfileFactory 
    { 
     public abstract IProfile GetProfile(Page p); //Factory Method Declaration 
    } 

class concreteFactoryforProfile1 : ProfileFactory 
    { 
    public override IProfile GetProfile(Page p) //Factory Method Implementation 
      { 
       //data access stuff... 
       return new Profile() { Page = p }; 
      } 
    } 

class concreteFactoryforProfile2 : ProfileFactory 
    { 
    public override IProfile GetProfile(Page p) //Factory Method Implementation 
      { 
       //other data access stuff... 
       return new Profile() { Page = p }; 
      } 
    } 


interface IProfile 
    { 
     Page Page { get; set; } 
     //other properties can come here 
    } 

class Profile : IProfile 
    { 
     public Page Page { get; set; } 
     //other properties can come here 
    } 


public class Test 
{ 
    void Main() 
    { 

     ProfileFactory[] objFactories = new ProfileFactory[2]; 
     objFactories[0] = new concreteFactoryforProfile1(); 
     objFactories[1] = new concreteFactoryforProfile2(); 
     foreach (ProfileFactory objFactory in objFactories) 
     { 
      IProfile objProfile = objFactory.GetProfile(this.Page); 
      Page p = objProfile.Page; 
     } 
    } 
} 

詳細が必要な場合は、お尋ねください。

+0

お返事ありがとうございます。私はこれでかなり混乱している。 'Main()'メソッドであなたの使用例をもう少し拡張することができますか? – Connell

+0

私は何を追加するかわからない、それはあなたのアプリがプロファイルを使用している方法にのみ依存します。特定のクラスのインスタンシエーションが複雑すぎるため、ここでのことは工場を使用することです。 – Arthis

+0

それはちょうどつまらない。なぜ私はこれを3時間前に見なかったのですか?実際にはなぜ私は最初の場所でこれを考えなかったのですか?ありがとうございました – Connell

関連する問題