《.NET分布式应用程序开》读书笔记 第二章:.NET组件

一:什么是组件,它和程序集有何区别?

组件,可能是一个类或控件,或一组类,现实一个特定的功能,提供一些特定的信息。

程序集是物理的文件,是部署时的单元。可以说程序集包括组件。

二:分布式应用中组件分为两种

  • 服务提供程度(Service-Provider),它通常是无状类的,只封闭特定的功能。如Data access object,Business object,Web service object。。。这些对象通常会被远程激活,所以它们一般继承于:
    • System.MarshalByRefObject,这样的对象才能被.NET Remoting激活
    • System.Web.Services.WebService,作为ASP.NET中的一个服务程序
    • System.EnterpriseServices.ServicedComponent,COM+对象,拥有COM+所提供的一切功能。如:分布式事务,对象池,JIT。
  • 信息的载体(Information Container),就是我们通常说的实体类,VO。贫血模型。因为它会被在不同的进程中传递,所以在标记为可序列化。在C#中,最简单的方法是加上[Serializable]属性。

三:组件示例

最常见的组件可能就是数据访问对象了。下例中,我们要为Customer建立数据访问的组件。

namespace Net.Distributed.App
{
    ///Customer table data access components (Service Provider)
    public class CustomerDb : System.ComponentModel.Component
    {
        public void AddCustomer(CustomerDetail customer)
        {
            throw new System.NotImplementedException();
        }

        public void UpdateCustomer(CustomerDetail customer)
        {
            throw new System.NotImplementedException();
        }

        public CustomerDetail GetCustomerById(int customerId)
        {
            throw new System.NotImplementedException();
        }
    }
}

namespace Net.Distributed.App
{  
    ///Customer Information Container classs
    public class CustomerDetail:System.ComponentModel.Component
    {
        public int CustomerId
        {
            get;
            set;
        }

        public string CustomerName
        {
            get;
            set;
        }

        public string Email
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }
    }
}

注意:你不必继承自Component类,这里这样做,主要是让它们可以在VS中显示在IDE中。方便设计时使用。

4

原文地址:https://www.cnblogs.com/rockniu/p/1546813.html