だから私はデータベースからいくつかのデータを取得するプロジェクトに取り組んでいます-このプロジェクトを素晴らしいものにする2つのデータがあります.1つは私がタイプを持っています(それらはイベントと呼ばれますが、基本的には私が作成した.NETタイプに変換されます)そして、XMLを使用して、オブジェクトを設計したので、オブジェクトは適切に逆シリアル化されます。これはすべて素晴らしいものであり、単体テストが行われ、すべてのクラスとメソッドは単一責任の原則に従います。
私のアーキテクチャスキルが曖昧になるのは、XMLから作成した.NETオブジェクトを処理するためのビジネスロジックを構築するためのファクトリを作成しているときです。
基本的にこれは私が持っているものです。
public class EventProcessorFactory : IEventProcessorFactory
{
private readonly List<IEventProcessor> _eventProcessors;
public EventProcessorFactory()
{
_eventProcessors = new List<IEventProcessor>();
}
public IEventProcessor GetProcessor(Type eventType)
{
var typeOfEventProcessor = GetProcessorFromEventType(eventType);
if (_eventProcessors.Any(x => x.GetType() == typeOfEventProcessor))
return _eventProcessors.Single(x => x.GetType() == typeOfEventProcessor);
var processor = BuildProcessorFromType(typeOfEventProcessor);
_eventProcessors.Add(processor);
return processor;
}
private static Type GetProcessorFromEventType(Type eventType)
{
if (eventType == typeof(EnrollmentEventType))
return typeof(EnrollmentEventProcessor);
if (eventType == typeof(ClaimantAccountInfoEventType))
return typeof(ClaimantAccountInfoEventProcessor);
if (eventType == typeof(PhoneUpdateEventType))
return typeof(PhoneUpdateEventProcessor);
if (eventType == typeof(AddressUpdateEventType))
return typeof(AddressUpdateEventProcessor);
if (eventType == typeof(ClientAccountInfoEventType))
return typeof(ClientAccountInfoEventProcessor);
return null;
}
private IEventProcessor BuildProcessorFromType(Type typeOfEventProcessor)
{
return ((IEventProcessor)Activator.CreateInstance(typeOfEventProcessor));
}
}
だからそれはうまくいくが、それはかなり不格好なようだ。工場の使用に関する記事をいくつか読んだことがありますが、正しいものを読んでいないか、理解できていません。上記のコードには2つの問題があります。
1)新しいイベントを追加して変更する必要がある場合は、後の開発者が「MyCoolNewEventType」と「MyCoolNewEventProcessor」を削除するだけで、イベントをプロセッサに一致させるメソッドを変更する必要がなくなります。
2)現在、インスタンスを作成すると、.CreateInstance();を呼び出すことができます。これは問題ありません。依存関係はありませんが、「イベントプロセッサ」は、少なくともデータベースに依存関係がある可能性があります。それを処理する方法が100%わからないので、Container.Resolve()をランダムに呼び出したくありません。
誰かが正しい方向を指すことができれば、それは途方もないことです。