Я проектирую слабосвязанную структуру. Я хочу вызывать классы из разных сборок / пространств имен через код, который представлен строкой . Мой дизайн таков, что каждое из бизнес-правил клиента находится на разных сборках и не зависит друг от друга (отношение ОДИН клиент к ОДНОЙ DLL), поэтому, когда я обновляю бизнес-правила 1 клиента, это не повлияет на остальные. Мое внимание сейчас сосредоточено на использовании Factory Design и использовании Activator.CreateInstance () Method.
Это настройка проекта (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();
}
Есть идеи, почему мой образец не работает? И есть ли предложения по улучшению дизайна?
Заранее спасибо.
Как насчет использования
Expression.Lambda
любой