学习011-03-01 Business Classes vs Database Tables(业务类与数据库表)

Business Classes vs Database Tables(业务类与数据库表)

The XAF is based on an object-based data handling approach. In this topic, we discuss the reasons why this approach, as opposed to the relational model of data handling, was chosen. The methods that allow utilization of this approach are also defined in this document.
XAF基于基于对象的数据处理方法。在本主题中,我们将讨论选择这种方法而不是数据处理的关系模型的原因。本文档还定义了允许使用这种方法的方法。

In an application where you have to deal with information, you most likely have database tables, database systems, SQL queries, records and columns. Using relational models is a traditional way to work with databases. Today, the most common approach to database access is probably ADO.NET technology. It has the DataSet type that grants a local copy of multiple interrelated database tables. Via the DataSet object, a user can locally execute various operations, with the content of a database being physically disconnected from the DBMS.
在您必须处理信息的应用程序中,您很可能拥有数据库表、数据库系统、SQL查询、记录和列。使用关系模型是处理数据库的传统方式。今天,最常见的数据库访问方法可能是ADO.NET技术。它具有DataSet类型,可以授予多个相互关联的数据库表的本地副本。通过DataSet对象,用户可以在本地执行各种操作,数据库的内容与DBMS物理断开连接。

The XAF concentrates its efforts on another flow of today’s development: object-oriented data handling. The need to store object-oriented data appeared when object-oriented programming became pervasive. Currently, most modern, nontrivial applications use an object-oriented paradigm to model application domains. This means that you can abstract from any persistence details, and have a clean, simple, object-oriented API to perform data storage. You do not need to handle persistence details and internal data representation in data stores be they relational, object-based, or something else. Why should you deal with low-level constructs of the data-store model, such as rows and columns, and constantly translate them back and forth? Instead, concentrate on complex applications you are required to deliver.
XAF将精力集中在当今开发的另一个流程上:面向对象的数据处理。当面向对象编程变得普遍时,存储面向对象数据的需求就出现了。目前,大多数现代、重要的应用程序都使用面向对象的范式来建模应用程序域。这意味着您可以从任何持久性细节中抽象出来,并拥有一个干净、简单、面向对象的API来执行数据存储。您不需要处理数据存储中的持久性细节和内部数据表示,无论是关系的、基于对象的还是其他什么。为什么要处理数据存储模型的低级结构,例如行和列,并不断来回转换它们?相反,专注于需要交付的复杂应用程序。

The ability to use object-based data handling, instead of direct interaction with databases, leads to a layer that provides mapping of objects into database tables (Object-Relational Mapping tool). In the XAF, such a layer is provided by the eXpress Persistent Objects (XPO). The XPO provides reliability and flexibility in storing the information, proven by years as a standalone product. Its simplicity comes from the fact that you do not need to learn the details of how the data is stored.
使用基于对象的数据处理而不是与数据库直接交互的能力导致了一个层,该层提供对象到数据库表的映射(对象-关系映射工具)。在XAF中,这样一个层由eXpress持久对象(XPO)提供。XPO在存储信息方面提供了可靠性和灵活性,多年来作为独立产品得到了证明。它的简单性来自于这样一个事实,即您不需要了解数据如何存储的细节。

With the XPO layer, to persist a class (to provide its mapping) all that you need to do is to inherit it from the XPObject class. This class has the XPObject.Oid property that uniquely specifies an object. In the XAF, the BaseObject class is used as a persistent class. It is inherited from the XPCustomObject class to satisfy the Framework’s needs. XPO reads the content of a persistent class, and creates a database table for its read-write public fields and properties. By default, XPO gives the database table the same name as the class, and stores data in fields with the same name as the properties. You can easily override this behavior by using the PersistentAttribute. The Persistent attribute can also be used to make XPO persist a private field, if required. If there is a property, class or field that you do not want to persist for any reason (for instance, a property’s value is calculated in code), use the NonPersistentAttribute. For more information on XPO, refer to the eXpress Persistent Objects Online Documentation.
使用XPO层,要持久化一个类(以提供其映射),您需要做的就是从XPObject类继承它。该类具有唯一指定对象的XPObject. Oid属性。在XAF中,BaseObject类用作持久化类。它从XPCustomObject类继承以满足框架的需求。XPO读取持久化类的内容,并为其读写公共字段和属性创建数据库表。默认情况下,XPO为数据库表提供与类相同的名称,并将数据存储在与属性相同名称的字段中。您可以通过使用Persis entAtoral轻松覆盖此行为。如果需要,Persis ent属性也可用于使XPO持久化为私有字段。如果有一个属性、类或字段由于任何原因而不希望持久化(例如,属性的值是在代码中计算的),请使用NonPersistentAttribute。有关XPO的更多信息,请参阅eXpress持久对象在线文档。

The following example demonstrates how to declare a persistent class:
以下示例演示如何声明持久类:

C# (EF Core)
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions]
public class Contact : BaseObject {
    public virtual string FirstName { get; set; }
}


// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
C# (XPO)
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
// ...
[DefaultClassOptions]
public class Contact : BaseObject {
    // BaseObject contains an auto-generated Guid key, you cannot add a custom key
    public Contact(Session session) : base(session) { }
    string fFirstName;
    public string FirstName {
        get { return fFirstName; }
        set { SetPropertyValue(nameof(FirstName), ref fFirstName, value); }
    }
}

The following help topic lists base classes that you can use in XPO-based applications: Base Persistent Classes.
以下帮助主题列出了可在基于XPO的应用程序中使用的基类:基本持久类。

Applying the DefaultClassOptions (see DefaultClassOptionsAttribute) attribute to a class creates the following capabilities:
将DefaultClassOptions(参见DefaultClassOptionsAttribute)属性应用于类会创建以下功能:

  • The Contact item is added to the main form’s navigation control. When clicking this item, a list of the objects of the Contact type will be displayed.
    联系人项被添加到主窗体的导航控件中。单击此项时,将显示联系人类型的对象列表。
  • The Contact item is added to the submenu of the New toolbar button when objects of another type are displayed. Click this item to invoke a Contact detail form and create a new Contact object.
    当显示另一种类型的对象时,联系人项将添加到新建工具栏按钮的子菜单中。单击此项可调用联系人详细信息表单并创建新的联系人对象。
  • The Contact objects is provided as a data source to generate reports (see Create and Preview a Report).
    联系人对象作为数据源提供以生成报告(请参阅创建和预览报告)。
  • The Contact item is added to the types list in the Dashboards Data Source Wizard. This allows end-users to create dashboards for objects of this class (see Create, View and Modify Dashboards in a WinForms Application and Create, View and Modify Dashboards in an ASP.NET Web Forms Application).
    联系人项被添加到仪表板数据源向导中的类型列表中。这允许最终用户为此类对象创建仪表板(请参阅在WinForms应用程序中创建、查看和修改仪表板以及在ASP.NETWeb窗体应用程序中创建、查看和修改仪表板)。

To apply each of these options separately, use the NavigationItem, CreateableItem, VisibleInReports and VisibleInDashboards attributes, respectively (see NavigationItemAttribute, CreatableItemAttribute, VisibleInReportsAttribute and VisibleInDashboardsAttribute).
要分别应用这些选项,请分别使用NavigationItem、CreateableItem、VisibleInReports和VisibleInDashboard属性(参见NavigationItemAttribute、CreatableItemAttribute、VisibleInReportsAttribute和VisibleInDashboardsAttribute)。

Using the SetPropertyValue is required by XPO. For details, refer to the Creating a Persistent Object topic.
XPO需要使用SetProperty tyValue。有关详细信息,请参阅创建持久对象主题。

When running the application for the first time, a database is created, and tables for all persistent classes found are created. The following image shows the Contact table corresponding to the Contact class defined above:
第一次运行应用程序时,会创建一个数据库,并为找到的所有持久类创建表。下图显示了与上面定义的联系人类对应的联系人表:

Note
CodeRush includes a number of Code Templates that help generate business classes or their parts with a few keystrokes. To learn about the Code Templates for eXpress Persistent Objects, refer to the following help topic: XPO and XAF Templates.
CodeRush包含许多代码模板,这些模板有助于通过几次击键生成业务类或其部分。要了解eXpress持久性对象的代码模板,请参阅以下帮助主题:XPO和XAF模板。
在这里插入图片描述

Then, you can modify the implemented persistent classes, and add new ones. The database will be changed respectively, when running the application.
然后,您可以修改实现的持久类,并添加新的。运行应用程序时,数据库将分别更改。

Note
While XAF can create database schema objects, it cannot modify them later. For instance, column type and size cannot be changed automatically when you update your business class code.
虽然XAF可以创建数据库模式对象,但以后不能修改它们。例如,当您更新业务类代码时,列类型和大小不能自动更改。

A Contact object can be created by an end-user. The following picture demonstrates a populated Contact Detail View (the View is generated automatically, although you can customize it):
最终用户可以创建联系人对象。下图演示了填充的联系人详细信息视图(视图是自动生成的,但您可以对其进行自定义):
在这里插入图片描述

After saving changes in the Contact Detail View, the created object is mapped to the database. The following image demonstrates the Contact table with the record inserted:
在联系人详细信息视图中保存更改后,创建的对象将映射到数据库。下图演示了插入记录的联系人表:
在这里插入图片描述

A new persistent object can be created and saved to the database in code. For details, refer to the Supply Initial Data topic. Generally, all operations on persistent objects are supposed to be performed via an Object Space.
可以用代码创建新的持久对象并将其保存到数据库中。有关详细信息,请参阅供应初始数据主题。通常,对持久对象的所有操作都应该通过对象空间执行。

Note
XAF does not use or initialize the DevExpress.Xpo.XpoDefault settings storage. So, the XpoDefault.Session or Session.DefaultSession Sessions should not be used in XAF applications. If you need to access a valid Session that uses the correct connection string, you have the following options:
XAF不使用或初始化DevExpress. Xpo.XpoDefault设置存储。因此,XpoDefault.Session或Session.DefaultSession会话不应在XAF应用程序中使用。如果您需要访问使用正确连接字符串的有效会话,您有以下选项:

  • In the context of a Business Class - use the Session property, available in all the persistent types that inherit from the DevExpress.Xpo.PersistentBase class.
    在Business Class的上下文中-使用Session属性,该属性在继承自DevExpress. Xpo.PersitentBase类的所有持久类型中可用。
  • In the context of a Controller - you can access the XafApplication object via the Controller.Application property. The XafApplication object, in turn, has the XafApplication.CreateObjectSpace method. You can use this method to create a new Object Space and access its Session via the XPObjectSpace.Session property.
    在Controller的上下文中-您可以通过Controller. Application属性访问XafApplication对象。XafApplication对象又具有XafApplication.CreateObjectSpace方法。您可以使用此方法创建新的对象空间并通过XPObjectSpace.Session属性访问其Session。

As you see, you do not need to be concerned with a database, its tables and record queries. You deal only with classes and their properties.
如您所见,您不需要关心数据库、其表和记录查询。您只处理类及其属性。

You can set relationships between database tables by defining relationships between objects. Refer to the following link for details: Relationships Between Objects. The “One-to-Many” and “Many-to-Many” relationship types are defined via the AssociationAttribute. The “Many” relationship part is represented by the XPCollection type property.
您可以通过定义对象之间的关系来设置数据库表之间的关系。有关详细信息,请参阅以下链接:对象之间的关系。“ One-to-Many”和“ Many-to-Many”关系类型是通过AssociationAttribute定义的。“Many”关系部分由XPCollection类型属性表示。

The XAF not only spares you the difficulties of direct contact with database tables, it also allows you to use the ready-to-use business classes. The XAF has the Business Class Library - a collection of classes that describe entities frequently used in business applications - personal information, addresses, etc. This means that you do not have to build your data structure from scratch - we have already implemented the most frequently used templates for you. You may only need to extend them with custom attributes, and create appropriate relations. For details on the Business Class Library, refer to the Built-in Business Classes for Most Popular Scenarios topic.
XAF不仅免除了您直接接触数据库表的困难,还允许您使用现成的业务类。XAF拥有业务类库——描述业务应用程序中经常使用的实体的类集合——个人信息、地址等。这意味着您不必从头开始构建数据结构——我们已经为您实现了最常用的模板。您可能只需要使用自定义属性扩展它们,并创建适当的关系。有关业务类库的详细信息,请参阅最流行场景的内置业务类主题。

If you have a large database and want to use it in an XAF application, refer to the How to: Generate XPO Business Classes for Existing Data Tables topic.
如果您有一个大型数据库并希望在XAF应用程序中使用它,请参阅如何:为现有数据表生成XPO业务类主题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值