いくつかのデータベース (Elephants、Giraffes、Gorillas など) があり、それぞれに ElephantInputs、ElephantResults、GiraffeInputs、GiraffeResults、GorillaInputs、GorillaResults という名前の入力テーブルと結果テーブルがあります。テーブルの命名を制御できません。
LINQ to SQL を使用して、ElephantInputs、ElephantResults、GiraffeInputs、GiraffeResults などのクラスを自動的に生成しています。
これらのデータベースから入力を読み取って処理できる単一のクラス、Processor() を作成したいと考えています。ユーザーの選択に基づいて Processor をインスタンス化する Factory メソッドがあります。
最初に、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();
}
}
私はこれを正しく行っていますか?もう少し複雑な方法はありますか?ありがとう