2011-02-16 8 views
1

私のアプリケーションでは、次のようなロジックの分離があります.SimpleControllerというクラスがあります。これは、SimpleEntitiesのインポートと検索に使用します。ビジネスロジックはどこにありますか?

私はすべてのSimpleEntitiesをメモリリストに読み込みました。なぜなら、これらのエンティティを頻繁に検索し、エンティティを検索するたびにデータベースから読み込むほうが高速です。

SimpleControllerクラスの代わりにSimpleEntitiesをメモリに読み込んでSimpleLogicクラスに格納する方がよいでしょうか?

public class SimpleEntity 
{ 
    public int SimpleId { get; set; } 
    public string SimpleName { get; set; } 
} 

public class SimpleDAL 
{ 
    public ICollection<SimpleEntity> GetAllSimpleEntities() 
    { 
     //Retrieve SimpleEntities from Database 
    } 
    public void InsertSimpleEntity(SimpleEntity simpleEntity) 
    { 
     //Insert simple Entity into Database 
    } 
} 

public class SimpleLogic 
{ 
    private readonly SimpleDAL simpleDAL = new SimpleDAL(); 

    public ICollection<SimpleEntity> GetAllSimpleEntities() 
    { 
     return simpleDAL.GetAllSimpleEntities(); 
    } 
    public void InsertSimpleEntity(SimpleEntity simpleEntity) 
    { 
     //Validate simpleEntity before adding to database 
     if (simpleEntity.SimpleId <= 0) 
      throw new Exception("Invalid SimpleEntity Id: " + simpleEntity.SimpleId); 

     if (String.IsNullOrEmpty(simpleEntity.SimpleName)) 
      throw new Exception("SimpleEntity Name cannot be empty or null"); 

     simpleDAL.InsertSimpleEntity(simpleEntity); 
    } 
} 

public class SimpleController 
{ 
    private readonly SimpleLogic simpleLogic = new SimpleLogic(); 
    private List<SimpleEntity> simpleEntities; 

    public SimpleController() 
    { 
     simpleEntities = simpleLogic.GetAllSimpleEntities().ToList(); 
    } 

    public int FindSimpleIndex(int simpleId) 
    { 
     return simpleEntities.FindIndex(p=> p.SimpleId == simpleId); 
    } 

    public void ImportOtherSimpleEntity(OtherSimpleEntity otherSimpleEntity) 
    { 
     if (otherSimpleEntity.Operation == "Update") 
     { 
      int index = FindSimpleIndex(otherSimpleEntity.OtherSimpleId); 
      //If entity was found update SimpleEntity 
      if (index > -1) 
      { 
       //Call SimpleLogic.UpdateSimpleEntity(Pass in SimpleEntity); 
      } 
     } 
    } 
} 

答えて

0

Iは、おそらくコントローラ内から参照可能SimpleEntityManagerを実装するだろう、そして必要に応じて外で動作することができます。したがって、コントローラはシステムのMVCの側面を処理し、SimpleEntityManagerSimpleEntitiesを管理します。

脇に、InsertSimpleEntity()は、フィールドで検証を実行しているようです。私は通常、SimpleEntityはその検証を自身(ほとんどの場合、建設中)実行します。

+0

[OK]を、私はSimpleEntityManagerの側面に同意します。 Entity Validation Logicをエンティティ自体に移動することについても考えていますが、Logicクラスでは実際に使用されていないようですが、DALを呼び出すだけで無駄になります。 – Jethro

関連する問題