.NET 學習

.NET 學習生活感想... 万事成蹉跎..... 贵在坚持 及时整理自己做过和学过的东西

博客园 首页 新随笔 联系 订阅 管理

Domain Components Basics(领域组件基础)

eXpressApp Framework > Concepts > Business Model Design > Domain Components > Domain Components Basics

Domain Components technology is designed to simplify data management and reusability by providing a much more flexible and abstracted approach to business object design. You can compose an application from reusable blocks, abstracted away from a particular persistence layer.
领域模型组件技术用于设计简单的数据管理和通过提供一个更灵活和抽象的方法复用业务对象设计。你可以制作一个用于程序从可重用块,一个特定的持久层抽象方法。

With Domain Components you define interfaces instead of regular business objects inherited from XPO classes. These interfaces will declare required properties or data fields. The way this data is to be processed ( Domain Logic) is then defined by creating special classes that determine how interface members behave when an object is constructed, when properties are changed, etc. Actual business classes are automatically implemented by XAF at runtime based on the provided logic and interfaces. You can package interfaces and domain logic in an assembly and use it as a domain library. If you then create a new XAF application, you can reference the domain library and reuse the domain components. Since interfaces support multiple inheritance, the required business objects can be combined into new domain components. With interfaces, you can make your domain model independent of implementation.

用领域组件定义接口代替从XPO类继承的常规业务对象。这些接口定义所需的属性或数据字段。这种方式处理(域逻辑)数据,然后建立特殊类定义以决定接口成员的如何行为当构建一个对象,一个属性更改,等等。实际业务类自动实现通过XAF在运行时基于提供的逻辑和接口。你可以打包接口和域逻辑在一个程序集中,用它作为一个域库。如果接下来你创建一个新的XAF应用程序,你可以引用域库,重用域组件。由于接口支持多继承,你可以实现自己独立域模型。

Benefits of the Domain Components Technology

域组件技术的好处

Domain Components Technology is not by any means a replacement for the regular business classes approach to defining domain entities. The main advantage of the technology is the ability to create reusable domain libraries containing components that can be combined in any way. If you do not need the ability to create reusable components and combine them, most probably you will not need to use Domain Components. The following list describes the benefits of the Domain Components technology and should help you decide whether or not the approach is right for your task.
域组件技术无论如何都不能定义为常规业务类方法定义域实体的替代品。该技术的优势是能建立重用以任何方式组合的包含组件的域库。如果你不需要能建立可重用和组合他们,最有可能你不需要用域组件。下面描述域组件技术的好处,帮助你决定你的任务是否使用该技术。

·         You can create reusable domain libraries.(可以建立可重用的域库)

Most probably, each new XAF application you develop is not unique. The most common objects, such as, Person, Phone, Address, etc. are always used. The Business Class Library provides you with a set of classes that you will need most frequently. But, implementing your own reusable library is not a simple task. With Domain Components you can easily package an assembly and reference it in your applications.
通常,开发的每个新的XAF应用程序都是独特的。常见的对象,如人员,电话号码,地址等等。业务类库提供一组经常使用的类。但是实现你自己可重用的库是不那么简单的任务。用域组件技术你可以很容易包装一个程序集并在应用程序中引用它。

·         You can use multiple inheritance.(你可以用多继承)

Since Domain Components are represented by interfaces and not by classes, you can use multiple inheritance to combine several previously defined components into a new one. The new Domain Component will expose the properties of all its ancestors and utilize their Domain Logic. Of course you can add new properties and apply additional logic.
由于域组件是通过接口展现而不是通过类,你可以用多继承来组合几个之前定义的组件成一个新的组件。新的领域构件公开所有祖先和可用领域逻辑的属性。当然,可以添加新的属性和额外应用逻辑。

·         You do not need to inherit from Base Persistent Classes when implementing Domain Components.(实现一个域组件不用从基持久类继承)

Each regular business class is derived from one of the Base Persistent Classes in XAF. The behavior of these classes differs. For instance, a base class can either expose the auto-generated primary key property of an integer or Guid type or not.. The choice of the base class is made once a class is implemented. With the Domain Components, you can use a specific base class in each new scenario (see the ITypesInfo.AddEntityToGenerate method overload that takes the baseClass parameter). However, attributes applicable to regular business classes and their properties can also be used with Domain Components. And of course, the object implemented via Domain Components does not differ from the object implemented in a standard way from the end-user or application administrator point of view.

XAF中,每个常规业务类是基于一个基持久类。这些类的行为不同。例如,一个基类也能公开自动生成的整数或Guid或不类型的属性。选择的基类一次实现一个类。用领域构件,在每一个方案(ITypesInfo.AddEntityToGenerate方法覆写用baseClass参数)中你可以用一个指定的基类。然而,特性适用于日常业务类和他们的属性,也适用于领域构件。当然,通过领域构件实现对象不同于从终端用户或应用程序管理员视点用一个标准方法实现对象。

 

Definition of the Domain Components Interfaces (定义域组件接口)

The following snippet illustrates a typical Domain Component definition.

下面代码片段说明了一个典型的领域构件定义。

C#

 

VB

 

[DomainComponent]

public interface IPerson {

    string LastName { get; set; }

    string FirstName { get; set; }

    string FullName { get; }

    void Copy(IPerson target);

}

<DomainComponent> _

Public Interface IPerson

    Property LastName() As String

    Property FirstName() As String

    ReadOnly Property FullName() As String

    Sub Copy(ByVal target As IPerson)

End Interface

 

As you can see in the code above, the interface decorated with the DomainComponentAttribute is considered to be a Domain Component. This interface must expose properties of the business class to be automatically generated when the application runs. You can use attributes applicable to regular business classes and their properties. For instance, you can decorate the LastName property with the RuleRequiredFieldAttribute and the class itself with the NavigationItemAttribute. Note the read-only FullName property and the Copy method. As these member values should be calculated, it is required to implement the Domain Logic, describing how to get these members' values.

正如你从上面代码看到,领域构件用特性DomainComponentAttribute修饰。当应用程序运行时这个接口公开了自动生成业务类的属性。你可以用使用与日常业务类和他们属性的特性。例如,你用特性RuleRequiredFieldAttribute修饰LastName属性,用特性NavigationItemAttribute修饰所在类。注意只读FullName属性和复制方法。这些成员的值是计算得到,它需要实现领域逻辑,描述如何获取这些成员的值。

Note (备注)

You can use the Domain Component v.10.2 template to simplify the definition of the Domain Component and its logic.
你可以用领域构件V10.2模板简化领域构件定义和他的逻辑。

Implementation of the Domain Logic Classes(实现域逻辑类)

Each Domain Component can have one or more Domain Logics. Each Domain Logic is represented by a class. Generally, this class should be decorated with the DomainLogicAttribute, which indicates the Domain Component interface to which the target class belongs (the Domain Component interface type is defined by the attribute's DomainLogicAttribute.InterfaceType parameter). The Domain Logic class should expose methods named according to the following naming conventions.

每个领域构件有一个或多个域逻辑。每个域逻辑代表一个类。通常,这个类用特性DomainLogicAttribute修饰,指出这个领域构件接口上的目标类归属(领域构件接口通过特性的DomainLogicAttribute.InterfaceType参数定义)。这些领域逻辑公开以下命名约定的命名方法。

Method Name(方法名称)

Description(描述)

Get_PropertyName

Executed when getting a target property value. A target property should be read-only. You can use this method to implement calculated properties.

执行时得到一个目标属性的值。目标属性只读。可以用这个方法实现计算属性。

Init_PropertyName

Executed when a target property is initialized. A target property should not be read-only. You can use this method to specify the initial value of a property.

执行时目标属性被初始化。目标属性不能只读。可以用着方法初始化一个属性的值。

AfterChange_PropertyName

Executed after a target property is changed. A target property should not be read-only. You can use this method to implement dependent properties.

执行后改变一个目标属性。目标属性不能只读。可以用这个方法属性实现依赖属性。

MethodName

Executed when the target method is called. You can use this method to define the target method body.

执行时目标方法被调用。你可以用这个方法定义一个目标方法体。

AfterConstruction

Executed after an object is constructed. You can use this method instead of the previous one to initialize several properties at once.

执行后构建一个对象。你可以用这个方法一次初始化几个属性而不是之前那个方法。

Note (备注)

These methods should be public and should accept the object of the target interface type as the first parameter. PropertyName and MethodName should be substituted with the target members' names.

这些方式是公共的,接受目标接口类型对象作为第一个参数。PropertyName MethodName应该用目标成员的名称替代。

As you can see from the table above, method names can consist of the target property name and one of the following prefixes: AfterChange_, Get_ or Init_. Additionally, there is the AfterConstruction predefined method name. You can define additional methods that can be called from methods implementing the Domain Logic.

正如你从上面表看到的,方法名称有目标属性名称和AfterChange_, Get_ 或者 Init_前缀组成。另外,AfterConstruction是预定义方法名称。你可以定义其他能被实现领域逻辑方法调用的方法。

The following snippet illustrates Domain Logic class implementation.

下面代码片段说明实现领域逻辑

C#

 

VB

 

[DomainLogic(typeof(IPerson))]

public class PersonLogic {

    public static string Get_FullName(IPerson person) {

        return string.Format("{0} {1}", person.FirstName, person.LastName);

    }

    public static void Copy(IPerson person, IPerson target) {

        if(target != null) {

            target.FirstName = person.FirstName;

            target.LastName = person.LastName;

        }

    }

}

<DomainLogic(GetType(IPerson))> _

Public Class PersonLogic

    Public Shared Function Get_FullName(ByVal person As IPerson) As String

        Return String.Format("{0} {1}", person.FirstName, person.LastName)

    End Function

    Public Shared Sub Copy(ByVal person As IPerson, ByVal target As IPerson)

        If target IsNot Nothing Then

            target.FirstName = person.FirstName

            target.LastName = person.LastName

        End If

    End Sub

End Class

 

You can also define a method, in a Domain Component, that will create Domain Component instances. For this purpose, the method must be parameterless, have the required return type and be decorated with the CreateInstanceAttribute.
你也可以在领域构件内定义一个方法,将创建领域构件实例。为此,这个方法必须无参数,具备必要的返回类并用CreateInstanceAttribute修饰。

C#

 

VB

 

[DomainComponent]

public interface IAddressable {

    string PrimaryAddress { get; set; }

    string SecondaryAddress { get; set; }

}

[DomainComponent]

public interface IPerson {

    IAddressable Address { get; set; }

    [CreateInstance]

    IAddressable CreateAddress();

}

[DomainLogic(typeof(IPerson))]

public class PersonLogic {

    public static void AfterConstruction(IPerson person) {

        if(person.Address == null) {

            person.Address = person.CreateAddress();

        }

    }

}

<DomainComponent> _

Public Interface IAddressable

    Property PrimaryAddress() As String

    Property SecondaryAddress() As String

End Interface

<DomainComponent> _

Public Interface IPerson

    Property Address() As IAddressable

    <CreateInstance> _

    Function CreateAddress() As IAddressable

End Interface

<DomainLogic(GetType(IPerson))> _

Public Class PersonLogic

    Public Shared Sub AfterConstruction(ByVal person As IPerson)

        If person.Address Is Nothing Then

            person.Address = person.CreateAddress()

        End If

    End Sub

End Class

 

 

Note (备注)

If you have no access to the Domain Logic class sources, you can use the ITypesInfo.RegisterDomainLogic and ITypesInfo.UnregisterDomainLogic methods to manipulate the Domain Logic assignment.
如果你不访问领域逻辑类资源,你可以用ITypesInfo.RegisterDomainLogic ITypesInfo.UnregisterDomainLogic方法使领域自己工作。

Registering the Domain Components in the XAF Application(XAF应用程序中注册域组件)

To specify what classes should be generated, register required domain components with the application. Override the ModuleBase.Setup method and invoke the ITypesInfo.AddEntityToGenerate method in it:

要指定类生成什么,需要在应用程序中指出所需的领域构件。覆写ModuleBase.Setup方法并调用ITypesInfo.AddEntityToGenerate方法。

C#

 

VB

 

using DevExpress.Persistent.BaseImpl;

// ...

public override void Setup(XafApplication application) {

    if (!XafTypesInfo.IsInitialized) {

        XafTypesInfo.Instance.AddEntityToGenerate("Person", typeof(IPerson), typeof(BaseObject));

        // ...

    }

    base.Setup(application);

}

Imports DevExpress.Persistent.BaseImpl

' ...

Public Overrides Sub Setup(ByVal application As XafApplication)

    If (Not XafTypesInfo.IsInitialized) Then

        XafTypesInfo.Instance.AddEntityToGenerate("Person", GetType(IPerson), GetType(BaseObject))

        ' ...

    End If

    MyBase.Setup(application)

End Sub

 

With the code above, the Person class derived from the BaseObject class will be generated. The generated class will expose properties and utilize the Domain Logic of the IPerson Domain Component.
有以上代码,将生成派生于BaseObject类的Person。这个生成的类利用IPerson领域构件的领域逻辑公开属性。

Current Limitations(当前限制)

As the Domain Components technology is released as a preview in XAF v2010 vol 1, there are several issues and limitations. Domain Components cannot be used:

领域构件技术作为预览版发布在XAF v2010 vol1,有一些问题和局限。不能用领域构件:

However, these issues will be fixed as soon as possible. (不过,这些问题可能不久将被解决)

To see a full example on how to define Domain Components, specify the Domain Logic and register them in the XAF application; refer to the How to: Implement Domain Components topic.

看完整的如何调用领域构件的例子,在XAF应用程序中指定领域构件和注册他们;请参考如何实现领域构件主题

Note (备注)

The XCRM demo installed with XAF is a customer relationship management application that uses the Domain Components technology and is composed completely of reusable blocks, abstracted away from a particular persistence layer. The application is located in the C:\Users\Public\Documents\DevExpress 2010.2 Demos\eXpressApp Framework\XCRM folder by default. To see how various tasks can be implemented via Domain Components, take a look at the source code of this application.

XCRM演示同XAF一起安装,是一个自定义关系管理应用程序,用了领域构件技术,完全可重用的模块组成,从一个特定的持久层抽象出来。默认,这个应用程序在本地C:\Users\Public\Documents\DevExpress 2010.2 Demos\eXpressApp Framework\XCRM文件夹。要了解实现各自任务通过领域构件,请看应用程序源代码。

欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/

posted on 2011-01-15 15:50  Tonyyang  阅读(1072)  评论(0编辑  收藏  举报
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/