EPR类企业管理系统

在我们现有系统基础上或全新开发,提供定制服务
为您的企业高效快速实施ERP,WMS,MES,CRM管理系统
全面管控物料仓库、销售业务、采购业务、仓库业务
生产过程、质量检验、组织架构、业务报表


定制
QQ:460-3528

开发
QQ群:3360-90194

源码
微信:136-3650-3721

业务类与数据库表

eXpressApp框架是基于一个基于对象的数据处理方法。在本主题中,我们讨论选择此方法(与数据处理的关系模型相对)的原因。本文档中还定义了允许使用此方法的方法。

在必须处理信息的应用程序中,您很可能具有数据库表,数据库系统,SQL查询,记录和列。使用关系模型是使用数据库的传统方式。如今,最常见的数据库访问方法可能是ADO.NET技术。它具有DataSet类型,该类型授予多个相互关联的数据库表的本地副本。通过DataSet对象,用户可以在本地执行各种操作,而数据库的内容则与DBMS物理断开连接。

eXpressApp框架将精力集中在当今发展的另一个流程上:面向对象的数据处理。当面向对象的编程变得普遍时,出现了存储面向对象的数据的需求。当前,大多数现代的,非平凡的应用程序都使用面向对象的范例来对应用程序域进行建模。这意味着您可以从任何持久性细节中抽象出来,并拥有一个干净,简单,面向对象的API来执行数据存储。您不需要在数据存储中处理持久性详细信息和内部数据表示,无论它们是关系型,基于对象的还是其他类型。为什么要处理数据存储模型的低级构造(例如行和列),并不断地来回转换它们?相反,请专注于您需要交付的复杂应用程序。

使用基于对象的数据处理(而不是与数据库直接交互)的能力导致提供了将对象映射到数据库表的层(对象关系映射工具)。在eXpressApp框架中,此类层由eXpress持久对象(XPO)提供。的XPO提供存储信息,通过年作为独立产品验证的可靠性和灵活性。它的简单性来自您无需了解如何存储数据的细节这一事实。

对于XPO层,要持久化一个类(以提供其映射),您需要做的就是从XPObject类继承它。此类具有XPObject.Oid属性,该属性唯一地指定一个对象。在eXpressApp Framework中BaseObject类用作持久类。它是从XPCustomObject类继承的,以满足Framework的需求。XPO读取持久类的内容,并为其可读写的公共字段和属性创建数据库表。默认情况下,XPO为数据库表提供与类相同的名称,并将数据存储在与属性具有相同名称的字段中。您可以使用PersistentAttribute轻松覆盖此行为。该永久属性还可以用来做XPO坚持一个私有字段,如果需要的话。如果存在由于某种原因不想保留的属性,类或字段(例如,属性的值是在代码中计算的),请使用NonPersistentAttribute。有关XPO的更多信息,请参考eXpress持久对象在线文档

以下代码演示了如何声明持久对象。

[DefaultClassOptions]
public class Contact : BaseObject {
   public Contact(Session session) : base(session) { }
   private string firstName;
   private string lastName;
   public string FirstName {
      get {
         return firstName;
      }
      set {
         SetPropertyValue(nameof(FirstName), ref firstName, value);
      }
   }
   public string LastName { 
      get {
         return lastName;
      } 
      set {
         SetPropertyValue(nameof(LastName), ref lastName, value);
      } 
   }
}

Applying the DefaultClassOptions (see DefaultClassOptionsAttribute) attribute to a class creates the following capabilities:

To apply each of these options separately, use the NavigationItem, CreateableItem, VisibleInReports and VisibleInDashboards attributes, respectively (see NavigationItemAttribute, CreatableItemAttribute, VisibleInReportsAttribute and VisibleInDashboardsAttribute).

Using the SetPropertyValue is required by XPO. For details, refer to the Simplified Property Syntax topic.

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:

注意

If you have CodeRush installed, you can use Code Templates when implementing business classes. Using Code Templates decreases code creation time, because it helps avoid having to type the entire code manually and allows you to create regular code sections with only a few keystrokes. To learn about the built-in Code Templates for eXpress Persistent Objects, refer to the XPO and XAF Templates topic.

ContactTable

Then, you can modify the implemented persistent classes, and add new ones. The database will be changed respectively, when running the application.

注意

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.

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):

Contact_DetailView

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:

ContactTableWithRecord

A new persistent object can be created and saved to the database in code. For details, refer to the Supply Initial Data (XPO) topic. Generally, all operations on persistent objects are supposed to be performed via an Object Space.

注意

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:

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 links for details:

The eXpressApp Framework 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 eXpressApp Framework 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.

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.

相关文章

转载保留此链接,注明出处