我已经为此研究了数周。我目前正在使用 n 层(3 层)方法和工厂设计方法设计松耦合架构设计。我的目标是将每个客户的业务逻辑(ClientA.DLL、ClientB.DLL)放在单独的命名空间中,以便项目横向扩展,这意味着我可以修改/删除/添加特定客户的业务逻辑而不影响其他客户,因为它们是彼此不依赖。然后,我通过 Factory 命名空间使用客户端的唯一标识符(数据库中维护的字符串值)调用客户端的命名空间/类。 Factory.DLL 还隐藏每个客户端的逻辑,而 BusinessAbstract.DLL 用作每个客户端的布局或模板类将使用。
这里是项目解决方案:
这里是实际的代码:
BusinessAbstract.DLL
namespace BusinessAbstract
{
// the entity / data transfer object
public class MemberDTO
{
public string MemberID { get; set; }
public string MemberName { get; set; }
}
// the interface
public interface IMaintainable
{
void Add();
void Edit();
void Delete();
}
// the base abstract class, implements the Entity and the Interface
public abstract class Member : MemberDTO, IMaintainable
{
// Implement IMaintanable but change it to abstract
public abstract void Add();
public abstract void Edit();
public abstract void Delete();
// a method with Database access, get from DAL
public virtual MemberDTO GetMemberDetails(params object[] args)
{
return DAL.MemberDAL.FetchMemberDetails(args);
}
public virtual string GetClientBLL()
{
return "base's method";
}
}
}
ClientA 实现 AbstractBusinessRule
ClientA.DLL
namespace ClientA
{
public class _Member : BusinessAbstract.Member
{
public override void Add()
{
throw new NotImplementedException();
}
public override void Edit()
{
throw new NotImplementedException();
}
public override void Delete()
{
throw new NotImplementedException();
}
public override string GetClientBLL()
{
return "ClientA Method";
}
}
}
工厂
工厂.DLL
public static class Invoker
{
public static T GetMemberInstance<T>(string clientCode)
where T : Member, IMaintainable
{
Type objType = Type.GetType(clientCode + "._Member," + clientCode);
return (T)Activator.CreateInstance(objType);
}
}
表示层的示例实现
网站
protected void Page_Load(object sender, EventArgs e)
{
// invoke Member class using String hardcode
Member obj = Invoker.GetMemberInstance<Member>("ClientA");
Response.Write(obj.GetClientBLL()); //prints clientA method
obj = Invoker.GetMemberInstance<Member>("ClientB");
Response.Write(obj.GetClientBLL()); //prints clientB method
}
您还会注意到我在每个客户端 DLL 以及 AbstractBusinessRule DLL 中都有一个 DAL 文件夹,因为我还想缩放 DAL 层并使用层结构“UI-BLL-DAL”。
欢迎对此设计提出任何意见/建议。我希望就如何改进此结构提供意见。提前致谢。
最佳答案
我唯一看到的,我只是在看你的帖子时错过了这一点,但我没有看到 DAL 接口(interface)定义或抽象层将它与你的 BL 分开,就像你的 BL 从你的 BL 中抽象出来一样演示文稿。
这很重要,因为它让您在未来可以灵活地使用相同的数据创建新的业务层,而无需重写 DAL,或者用单元测试中的平面 CSV 文件/模拟/维护的第 3 方替换您的数据库soap web 服务响应,或者其他任何将来可能成为更好的数据存储机制。
关于c# - 创建松散耦合/可扩展的软件架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3737848/