2011-09-19 73 views
1
public class Service : IService 
{ 
    internal Configuration configuration; 
    public Response response; 

    public Service() 
    { 
     configuration = new Configuration(); 
     configuration.Fetch(); 
    } 

    public Response Execute(Request request) 
    { 
     switch case request.processtype 
     { 
      case ProcessType.Import: 
       Import import = new Import(); 
       import.Configuration = configuration; 
       Response response = import.Execute(request) 

      case ProcessType.Export: 
       Export export = new Export(); 
       export.Configuration = configuration; 
       Response response = export.Execute(request) 
     } 
    } 


    public class Import 
    { 
     public Configuration configuration; 
     public Response response; 

     public Response Execute(Request request) 
     { 
      response.AddMessage("doing something"); 
      //some code 

      ImportSomething something = new ImportSomething(); 
      something.Configuration = configuration; 
      something.Response = response; 

      response.AddMessage("doing more thing"); 
      //more code 

      ImportSomethingElse somethingelse = new ImportSomethingElse(); 
      somethingelse.Configuration = configuration; 
      somethingelse.Response = response; 

      return response; 
     } 

     public class ImportSomething 
     { 
      public Configuration configuration; 
      public Response response; 

      public Response Execute(Request request) 
      { 
       response.AddMessage("doing something"); 
       //some code 
       response.AddMessage("doing more thing"); 
       //more code 
      } 
     } 

     public class ImportSomethingElse 
     { 
      public Configuration configuration; 
      public Response response; 

      public Response Execute(Request request) 
      { 
       response.AddMessage("doing something"); 
       //some code 
       response.AddMessage("doing more thing"); 
       //more code 
      } 
     } 
    } 

    public class Export 
    { 
     public Configuration configuration; 

     public Response Execute(Request request) 
     { 

     }  
    } 

    public class Configuration 
    { 
     List<NameValue> Items; 

     public void Fetch() 
     { 
      //fetch from database 
      Items.Add("data"); 
     } 
    } 

    public class Response 
    { 
     List<Message> Messages; 
     Public string Data; 

     public void AddMessage() 
     { 

     } 
    } 
} 

你會注意到我將配置和響應對象傳遞給我打電話的類。我想知道這是否正確。特別是我傳遞給每個類的響應對象是因爲最終響應需要包含來自所有對象的消息。c#類的設計問題

回答

1

你的實現沒有什麼真正的錯誤。它可以combesome將這些對象傳遞給每個構造函數。如果你想要走這條路線,創建一個工廠,傳入需要的配置和響應對象,然後從工廠創建所有其他對象,依靠它來獲取,這是工廠設計的一個更容易的事情。這些對象傳入正確的地方。

+0

我從來沒有聽說過工廠。我正在研究它。如果你能指出我正確的方向,我將不勝感激。謝謝 – gangt

0

這可以用IoC容器做的很漂亮。但因爲它是純C#的問題...

我定義類似語境和使用它的實例(僅一次初始化),而不是所有這些分離設置:

public class CommandContext 
{ 
    public Configuration Configuration { get; protected set; } 
    public Request Request { get; protected set; } 

    public CommandContext(Configuration configuiration, ...) { ... } 
} 

...

public Response Execute(Request request) 
{ 
    var context = new CommandContext(configuration, request); 
    switch case request.processtype 
    { 
     case ProcessType.Import: return new Import().Execute(context); 
     case ProcessType.Export: return new Export().Execute(context); 
    } 
} 

此外,工廠模式可以幫助構建物體,確實可以減少更多的線數。

+0

既然你提到了工廠模式,我會先研究它。如果這看起來不適合我,我會走這條路。謝謝 – gangt