创建一个松散耦合的

创建一个松散耦合的

本文介绍了创建一个松散耦合的/可扩展的软件架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了几个星期有关这个..我目前正在设计一个松耦合使用N层架构设计(3层)的方法和工厂的设计方法。我的目标是把每一个客户的业务逻辑(ClientA.DLL,ClientB.DLL)在不同的命名空间,使该项目扩展出来的意思,我可以修改/删除/添加特定客户的业务逻辑,而不会影响其他人,因为它不依赖于每个其他。然后,我用客户端的唯一标识符通过工厂命名空间(即保持在数据库中的字符串值)调用客户端的命名空间/班。在 Factory.DLL 也的隐藏的每个客户端的逻辑。虽然在 BusinessAbstract.DLL 作为布局或者说,每个客户端的课程将使用的模板。

下面是该项目的解决方案。

和这里是实际code:

BusinessAbstract.DLL

 命名空间BusinessAbstract
{
   //实体/数据传输对象
   公共类MemberDTO
   {
      公共字符串会员id {获得;组; }
      公共字符串成员名{获得;组; }
    }

   //接口
   公共接口IMaintainable
   {
      无效的Add();
      无效编辑();
      无效删除();
   }

  //基抽象类,实现了实体和接口
  公共抽象类会员:MemberDTO,IMaintainable
  {
    //实现IMaintanable,但将其更改为抽象
    公共抽象无效添加();
    公共抽象无效编辑();
    公共抽象无效删除();

    //数据库访问的方法,从DAL获取
    公共虚拟MemberDTO GetMemberDetails(params对象[]参数)
    {
        返回DAL.MemberDAL.FetchMemberDetails(参数);
    }

    公共虚拟字符串GetClientBLL()
    {
        返回基地法;
    }
   }
 }
 

客户端A执行AbstractBusinessRule的

ClientA.DLL

 命名空间的客户端A
 {
    公共类_Member:BusinessAbstract.Member
   {
       公众覆盖无效添加()
      {
        抛出新的NotImplementedException();
      }

      公众覆盖无效编辑()
      {
        抛出新的NotImplementedException();
      }

      公众覆盖无效删除()
      {
        抛出新的NotImplementedException();
      }

      公众覆盖字符串GetClientBLL()
      {
        返回客户端A法;
      }
    }
 }
 

出厂

Factory.DLL

 公共静态类祈求
 {
     公共静态牛逼GetMemberInstance< T>(字符串,客户端code)
        其中T:会员,IMaintainable
      {
        输入OBJTYPE = Type.GetType(客户端code +._Member,+端code);
        返程(T)Activator.CreateInstance(OBJTYPE);
      }
  }
 

在presentation层样品实施

网站

 保护无效的Page_Load(对象发件人,EventArgs的)
 {

    //使用字符串硬code调用成员类
    会员OBJ = Invoker.GetMemberInstance<会员>(客户端A);
    回复于(obj.GetClientBLL()); //输出clientA的方法

    OBJ = Invoker.GetMemberInstance<会员>(ClientB);
    回复于(obj.GetClientBLL()); //输出clientB方法

 }
 

任何意见/建议,这个设计。我需要我怎么能改善这个结构中的输入家伙。先谢谢了。

解决方案

我看到了,我只是雾气会丢失这看您的文章,但我没有看到一个DAL接口定义或抽象层的唯一的事情,从你的BL seperates它在你的BL是从presentation抽象的方式。

这是重要的,因为它可以让你在今后使用相同的数据来创建新的业务层的灵活性,而无需重写DAL,或平CSV文件替换您的数据库/嘲弄在单元测试/第三方维护SOAP的Web服务响应,或任何其他可能在未来更好的数据存储机制。

I been researching for weeks about this.. I'm currently designing a loosely-coupled architecture design using n-tier(3 layered) method and factory design approach. My goal is to put each Client's business logic (ClientA.DLL, ClientB.DLL) in separate namespaces so that the project scales out meaning I can modify/remove/add a specific clients business logic without affecting the others because its not dependent to each other. Then I Invoke the client's namespaces/class using the client's unique identifier (a string value that is maintained in the database) via the Factory namespace. The Factory.DLL also hides the per-client logic. While the BusinessAbstract.DLL serve as the Layout or the Template that the per-client's classes will be using.

Here is the project solution.

and here is the actual code:

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 implementation of the 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";
      }
    }
 }

The Factory

Factory.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);
      }
  }

Sample implementation on Presentation Tier

the Website

 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

 }

Any comments/suggestion about this design. I need your input guys on how I can improve this structure. Thanks in advance.

解决方案

The only thing I see, and I mist just be missing this in looking at your post, but I don't see a DAL interface definition or abstraction layer that seperates it from your BL in the way your BL is abstracted from your presentation.

This is important because it gives you the flexibility in the future to create a new business layer using the same data without having to rewrite the DAL, or replacing your database with flat CSV files/mocks in unit testing/a 3rd party maintained soap web service response, or whatever else might be a better data storage mechanism in the future.

这篇关于创建一个松散耦合的/可扩展的软件架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:04