2010-01-21 62 views
0

我有幾個數據庫 - 大象,長頸鹿,大猩猩等 - 每個數據庫都有一個名爲ElephantInputs,ElephantResults,GiraffeInputs,GiraffeResults,GorillaInputs,GorillaResults的輸入和結果表。我無法控制表的命名。LINQ to SQL協方差 - 這是正確的方法嗎?

我使用LINQ to SQL來自動生成ElephantInputs,ElephantResults,GiraffeInputs,GiraffeResults等類。

我想編寫一個類Processor(),它可以從任何這些數據庫讀取輸入並處理它。我有一個Factory方法根據用戶的選擇實例化一個Processor。

我做的第一件事是爲Input和Result對象創建一個接口,併爲每個數據庫創建一個分部類,以便其輸入和結果對象實現該接口。這給了我每個動物的通用界面。我還爲每個數據庫創建了一個存儲庫類,它有一些方法可以從數據庫返回項目。

public interface IBaseInput 
{ 
    int ID { get; set; } 
    string InputA { get; set; } 
    int InputB { get; set; } 
    double InputC { get; set; } 
} 

public interface IBaseResult 
{ 
    int ID { get; set; } 
    string ResultA { get; set; } 
    int ResultB { get; set; } 
    double ResultC { get; set; } 
} 

public interface IRepository<I, R> 
    where I : IBaseInput 
    where R : IBaseResult 
{ 
    IQueryable<I> GetInputs(); 
    IQueryable<R> GetResults(); 
} 

public partial class GorillaInput : IBaseInput 
{ 
} 

public partial class GorillaResult : IBaseResult 
{ 
} 

    // A concrete repository for Gorillas 
    public class GorillaRepository : IRepository<GorillaInput, GorillaResult> 
    { 

    GorillaDataContext db = new GorillaDataContext(GetConnectionString("Gorillas")); 

    public IQueryable<GorillaInput> GetInputs() 
    { 
     return from p in db.GorillaInputs select p; 
    } 

    public IQueryable<GorillaResult> GetResults() 
    { 
     return from r in db.GorillaResults select r; 
    } 
} 

接下來,我創建了一個接口,用於我的處理器

public interface IProcessor 
{ 
    void Process(); 
} 

這裏有一個具體的處理器,並創建它的工廠。

public class Processor<T, I, R> : IProcessor 
    where T : class, IRepository<I, R>, new() 
    where P : IBaseInput 
    where R : IBaseResult 
{ 

    public void Process() 
    { 
     // Do some stuff ... 

     T repository = new T(); // Get results from the rater tables 
     IEnumerable<I> inputs = repository.GetInputs(); 
     IEnumerable<R> results = repository.GetResults(); 

     // Do some stuff with inputs and outputs... 
    } 
} 

public static class ProcessorFactory 
{ 
    public static IProcessor GetProcessor(string animal) 
    { 
     switch (animal) { 
      case "Elephant": 
       return new Processor<ElephantRepository, ElephantInput, ElephantResult>(); 
      case "Giraffe": 
       return new Processor<GiraffeRepository, GiraffeInput, GiraffeResult>(); 
      case "Gorilla": 
       return new Processor<GorillaRepository, GorillaInput, GorillaResult>(); 
      default: 
       return null; 
     } 
    } 
} 

最後,這裏的調用處理器的程序:

class Program 
{ 
    public static void Main() 
    { 
     IProcessor processor = ProcessorFactory.GetProcessor("Gorilla"); 
     processor.Process(); 
    } 
} 

我這樣做對嗎?有沒有更復雜的方法? 謝謝

回答

2

隨着所有具體類型實現接口,爲什麼要打擾泛型呢?

public class Processor 
{ 

    public void Process(IRepository repository) 
    { 
     // Do some stuff ... 

     IEnumerable<IBaseInput> inputs = repository.GetInputs(); 
     IEnumerable<IBaseResult> results = repository.GetResults(); 

     // Do some stuff with inputs and outputs... 
    } 
} 
相關問題