疎結合構造を設計しています。String で表されるコードを介して、さまざまなアセンブリ/名前空間からクラスを呼び出したいと考えています。私の設計では、クライアントのビジネス ルールはそれぞれ異なるアセンブリにあり、相互に依存していません (1 つのクライアントは 1 つの DLL に対する比率です)。そのため、1 つのクライアントのビジネス ルールを更新しても、他のクライアントには影響しません。私の注意は、Factory Design の使用とActivator.CreateInstance()メソッドの使用にあります。
これはプロジェクトのセットアップです (2+n DLL)
namespace Foundation; // where the interfaces/abstract resides
namespace Factory; // has dependency on Foundation assembly
namespace Client1; // client1's DLL, no dependency
namespace Client2; // client2's DLL, no dependency
The UI // only referenced to the Foundation and Factory not the Clients
実際のコード
namespace Foundation
{
public interface IBusinessRules
{
string GetBusinessRule();
}
}
namespace Client1 //DLL for client 1
{
public class BusinessRules : Foundation.IBusinessRules
{
public string GetBusinessRule()
{
return "Client1 Business Rule";
}
}
}
namespace Client2 //DLL for client 2
{
public class BusinessRules : Foundation.IBusinessRules
{
public string GetBusinessRule()
{
return "Client2 Business Rule";
}
}
}
namespace Factory
{
public static class Invoker<T> where T: Foundation.IBusinessRules
{
public static T FetchInstance(string clientCode)
{
return (T)Activator.CreateInstance(Type.GetType(clientCode));
}
}
}
//sample implementation that generates unhandled Exception
using Factory;
using Foundation;
static void Main(string[] args)
{
//the parameter is maintained in the database
IBusinessRules objClient1 = Invoker<IBusinessRules>.FetchInstance("Client1");
//should call Client1.BusinessRules method
Console.WriteLine(objClient.GetBusinessRule());
Console.Read();
objClient = Invoker<IBusinessRules>.FetchInstance("Client2");
//should call Client2.BusinessRules method
Console.WriteLine(objClient.GetBusinessRule());
Console.Read();
}
私のサンプルが機能しない理由は何ですか? また、デザインを改善するための提案はありますか? 前もって感謝します。
使ってみてはどうですか
式.ラムダ
誰でも?