This topic describes how to define the business model and the business logic for WinForms and ASP.NET applications. The applications' business model contains two logical parts that are implemented via different ORM tools:

本主题描述如何定义WinForms和ASP.net的业务模型和业务逻辑。asp.net应用程序的业务模型包含两个逻辑部分,通过不同的ORM工具实

Logical part ORM tool Classes
Marketing Entity Framework (EF) Customer and Testimonial
Planning eXpress Persistent Objects (XPO) Project and Task

#Add the Customer and Testimonial entities (EF)

添加客户和证明实体(EF

  1. In the Solution Explorer, right-click the SimpleProjectManager.Module\BusinessObjects folder and select Add | Class… from the context menu. Set the name to "Marketing" and click Add.

  2. Replace the auto-generated file content with the following code:

  在解决方案资源管理器中,右键单击SimpleProjectManager。模块\BusinessObjects文件夹,并从上下文菜单中选择添加|类…。将名称设置为“Marketing”,然后单击Add。
  用以下代码替换自动生成的文件内容:

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.CompilerServices;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base; namespace SimpleProjectManager.Module.BusinessObjects.Marketing {
[NavigationItem("Marketing")]
public class Customer : INotifyPropertyChanged {
public Customer() {
testimonials = new List<Testimonial>();
}
int id;
[Browsable(false)]
public int Id {
get { return id; }
protected set { SetProperty(ref id, value); }
}
string firstName;
public string FirstName {
get { return firstName; }
set { SetProperty(ref firstName, value); }
}
string lastName;
public string LastName {
get { return lastName; }
set { SetProperty(ref lastName, value); }
}
string email;
public string Email {
get { return email; }
set { SetProperty(ref email, value); }
}
string company;
public string Company {
get { return company; }
set { SetProperty(ref company, value); }
}
string occupation;
public string Occupation {
get { return occupation; }
set { SetProperty(ref occupation, value); }
}
List<Testimonial> testimonials;
[Aggregated]
public virtual List<Testimonial> Testimonials {
get { return testimonials; }
set { SetProperty(ref testimonials, value); }
}
[NotMapped]
public string FullName {
get {
string namePart = string.Format("{0} {1}", FirstName, LastName);
return Company != null ? string.Format("{0} ({1})", namePart, Company) : namePart;
}
}
byte[] photo;
[ImageEditor(ListViewImageEditorCustomHeight = , DetailViewImageEditorFixedHeight = )]
public byte[] Photo {
get { return photo; }
set { SetProperty(ref photo, value); }
}
protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null) {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
OnPropertyChanged(propertyName);
}
}
#region the INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}

 
Show the API description
  1. Open the SimpleProjectManagerDbContext.cs (SimpleProjectManagerDbContext.vb) file within the SimpleProjectManager.Module\BusinessObjects folder. Add the Customer and Testimonial properties to the SimpleProjectManagerDbContext class to register Customer and Testimonial entities within the DbContext as shown below:

    • C#
    • 显示API描述
      在SimpleProjectManager中打开SimpleProjectManagerDbContext.cs (SimpleProjectManagerDbContext.vb)文件。模块\ BusinessObjects文件夹。将客户和证明属性添加到SimpleProjectManagerDbContext类中,以便在DbContext中注册客户和证明实体,如下所示:

      c#

    
    
    using SimpleProjectManager.Module.BusinessObjects.Marketing;
    
    namespace  SimpleProjectManager.Module.BusinessObjects {
    public class SimpleProjectManagerDbContext : DbContext {
    //...
    public DbSet<Customer> Customer { get; set; }
    public DbSet<Testimonial> Testimonial { get; set; }
    }
    }
    
    
    
    
  2. In the Solution Explorer, navigate to the Module.cs (Module.vb) file and press the F7 key to open the file in the code editor. Uncomment the #if and #endif directives and code between them as shown below to specify the database initializer for EF. With this uncommented code, the database is recreated each time you change your entities.

    • C#
    • 在解决方案资源管理器中,导航到Module.cs (Module.vb)文件,并在代码编辑器中按F7键打开文件。取消注释#if和#endif指令和它们之间的代码,如下所示,以指定EF的数据库初始化器。使用这个未注释的代码,每次更改实体时都会重新创建数据库。
      c#
      public sealed partial class SimpleProjectManagerModule : ModuleBase {
      static SimpleProjectManagerModule() {
      // ...
      // Uncomment this code to delete and recreate the database each time the data model has changed.
      // Do not use this code in a production environment to avoid data loss.
      #if DEBUG
      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SimpleProjectManagerDbContext>());
      #endif
      }
      // ...
      }
     

#Add the Project and ProjectTask persistent classes (XPO)

#添加项目和ProjectTask持久化类(XPO)

  1. In the Solution Explorer, right-click the SimpleProjectManager.Module\BusinessObjects folder and select Add | Class… from the context menu. Set the name to "Planning" and click Add.

  2. Replace the auto-generated file content with the following code:

    • ProjectTask (C#)
    • Project (C#)
    • 在解决方案资源管理器中,右键单击SimpleProjectManager。模块\BusinessObjects文件夹,并从上下文菜单中选择添加|类…。将名称设置为“Planning”,点击Add。
      用以下代码替换自动生成的文件内容:
    • ProjectTask (c#)
      项目(c#)
    
    
    using System;
    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.BaseImpl;
    using DevExpress.Xpo; namespace SimpleProjectManager.Module.BusinessObjects.Planning {
    [NavigationItem("Planning")]
    public class ProjectTask : BaseObject {
    public ProjectTask(Session session) : base(session) { }
    string subject;
    [Size()]
    public string Subject {
    get { return subject; }
    set { SetPropertyValue(nameof(Subject), ref subject, value); }
    }
    ProjectTaskStatus status;
    public ProjectTaskStatus Status {
    get { return status; }
    set { SetPropertyValue(nameof(Status), ref status, value); }
    }
    Person assignedTo;
    public Person AssignedTo {
    get { return assignedTo; }
    set { SetPropertyValue(nameof(AssignedTo), ref assignedTo, value); }
    }
    DateTime startDate;
    public DateTime StartDate {
    get { return startDate; }
    set { SetPropertyValue(nameof(startDate), ref startDate, value); }
    }
    DateTime endDate;
    public DateTime EndDate {
    get { return endDate; }
    set { SetPropertyValue(nameof(endDate), ref endDate, value); }
    }
    string notes;
    [Size(SizeAttribute.Unlimited)]
    public string Notes {
    get { return notes; }
    set { SetPropertyValue(nameof(Notes), ref notes, value); }
    }
    Project project;
    [Association]
    public Project Project {
    get { return project; }
    set { SetPropertyValue(nameof(Project), ref project, value); }
    }
    }
    }

#Populate the Database with Initial Data

用初始数据填充数据库

Open the Updater.cs (Updater.vb) file from the SimpleProjectManager.Module project's Database Update folder and override the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method as shown below:

从SimpleProjectManager中打开update .cs (update .vb)文件。模块项目的数据库更新文件夹并覆盖ModuleUpdater。updatedabaseafterupdateschema方法如下:

using DevExpress.Persistent.BaseImpl;
using SimpleProjectManager.Module.BusinessObjects.Marketing;
using SimpleProjectManager.Module.BusinessObjects.Planning;
// ...
public class Updater : ModuleUpdater {
//...
public override void UpdateDatabaseAfterUpdateSchema() {
base.UpdateDatabaseAfterUpdateSchema();
if (ObjectSpace.CanInstantiate(typeof(Person))) {
Person person = ObjectSpace.FindObject<Person>(
CriteriaOperator.Parse("FirstName == ? && LastName == ?", "John", "Nilsen"));
if (person == null) {
person = ObjectSpace.CreateObject<Person>();
person.FirstName = "John";
person.LastName = "Nilsen";
}
}
if (ObjectSpace.CanInstantiate(typeof(ProjectTask))) {
ProjectTask task = ObjectSpace.FindObject<ProjectTask>(
new BinaryOperator("Subject", "TODO: Conditional UI Customization"));
if (task == null) {
task = ObjectSpace.CreateObject<ProjectTask>();
task.Subject = "TODO: Conditional UI Customization";
task.Status = ProjectTaskStatus.InProgress;
task.AssignedTo = ObjectSpace.FindObject<Person>(
CriteriaOperator.Parse("FirstName == ? && LastName == ?", "John", "Nilsen"));
task.StartDate = new DateTime(, , );
task.Notes = "OVERVIEW: http://www.devexpress.com/Products/NET/Application_Framework/features_appearance.xml";
}
}
if (ObjectSpace.CanInstantiate(typeof(Project))) {
Project project = ObjectSpace.FindObject<Project>(
new BinaryOperator("Name", "DevExpress XAF Features Overview"));
if (project == null) {
project = ObjectSpace.CreateObject<Project>();
project.Name = "DevExpress XAF Features Overview";
project.Manager = ObjectSpace.FindObject<Person>(
CriteriaOperator.Parse("FirstName == ? && LastName == ?", "John", "Nilsen"));
project.Tasks.Add(ObjectSpace.FindObject<ProjectTask>(
new BinaryOperator("Subject", "TODO: Conditional UI Customization")));
}
}
if (ObjectSpace.CanInstantiate(typeof(Customer))) {
Customer customer = ObjectSpace.FindObject<Customer>(
CriteriaOperator.Parse("FirstName == ? && LastName == ?", "Ann", "Devon"));
if (customer == null) {
customer = ObjectSpace.CreateObject<Customer>();
customer.FirstName = "Ann";
customer.LastName = "Devon";
customer.Company = "Eastern Connection";
}
}
ObjectSpace.CommitChanges();
}
//...
}
In the code above, the Object Space is used to create initial data. This is one of the main framework abstractions that allows you to perform CRUD (create-read-update-delete) operations. You can find more information on the ObjectSpace in the next topic (the Define Custom Logic and UI Elements section).

在上面的代码中,对象空间用于创建初始数据。这是允许您执行CRUD(创建-读取-更新-删除)操作的主要框架抽象之一。您可以在下一个主题(定义自定义逻辑和UI元素部分)中找到关于ObjectSpace的更多信息。

Note

You can refer to Supply Initial Data (XPO/EF) for more information on how to initially populate the database.

请注意
有关如何初始填充数据库的更多信息,可以参考Supply Initial Data (XPO/EF)

#Run the Applications

#运行应用程序

  • #The WinForms Application

    The WinForms project is set as the default startup project. Press Start Debugging or the F5 key to run the application.

    The following image shows the auto-created UI of this application:

  • # WinForms应用程序

  • WinForms项目被设置为默认的启动项目。按下Start Debugging或F5键运行应用程序。
    下图显示了这个应用程序自动创建的UI:

  • #The ASP.NET Application

    Right-click the SimpleProjectManager.Web project in the Solution Explorer and select the Set as StartUp Project item from the context menu. Press Start Debugging or the F5 key.

    The following image shows the auto-created UI of this application:

  • # ASP.Net应用程序
    右键单击SimpleProjectManager。在解决方案资源管理器中,并从上下文菜单中选择Set作为启动项目项。按下开始调试或F5键。
    下图显示了这个应用程序自动创建的UI:

XAF generates this UI for List and Detail Views with available CRUD operations and other capabilities (search, filter, print, etc.). The Detail View contains editors (text box, memo, drop-down box, image and date picker, etc.) that display different business class properties.

XAF为具有可用CRUD操作和其他功能(搜索、筛选、打印等)的列表和详细视图生成此UI。Detail视图包含显示不同业务类属性的编辑器(文本框、备忘录、下拉框、图像和日期选择器等)。

Lookup and collection editors display properties that are parts of an association. For example, the Project and ProjectTask classes participate in a One-To-Many relationship. The editor for the "Many" part (the Project's Tasks property) allows users to add, edit, remove, and export tasks.

查找和集合编辑器显示属于关联一部分的属性。例如,Project和ProjectTask类参与一对多关系。“许多”部分的编辑器(项目的任务属性)允许用户添加、编辑、删除和导出任务。

To display reference properties (the ProjectTask's AssignedTo property), XAF generates a drop-down list of persons in the UI and creates a foreign key that references the Person table in the database. This drop-down list displays person's full names because the FullName property is the default property of the Person class (see Default Property of a Business Class).

为了显示引用属性(ProjectTask的AssignedTo属性),XAF在UI中生成人员的下拉列表,并创建一个外键来引用数据库中的Person表。此下拉列表显示person的全名,因为FullName属性是person类的默认属性(请参阅业务类的默认属性)。

Note

You can find more information on UI generation in the List View Column Generation and View Items Layout Generation topics.

请注意
您可以在列表视图列生成和视图项布局生成主题中找到更多关于UI生成的信息。

#Auto-Created Database

# Auto-Created数据库

The database is automatically created based on your data model. The database columns are generated based on the persistence settings specified in the data model (such as field size set via an attribute). The images below show the Object Explorer window from the SQL Server Management Studio.

数据库是根据您的数据模型自动创建的。数据库列是根据数据模型中指定的持久性设置(例如通过属性设置的字段大小)生成的。下面的图像显示了来自SQL Server Management Studio的对象资源管理器窗口。

The applications' navigation control contains items for each database table. These navigation items allow a user to navigate to a List View with records and open their Detail Views.

Next topic: Customize the Application UI and Behavior

应用程序的导航控件包含每个数据库表的项。这些导航项允许用户导航到带有记录的列表视图并打开它们的详细信息视图。

Define the Data Model and Set the Initial Data 定义数据模型并设置初始数据的更多相关文章

  1. HBase 数据模型(Data Model)

    HBase Data Model--HBase 数据模型(翻译) 在HBase中,数据是存储在有行有列的表格中.这是与关系型数据库重复的术语,并不是有用的类比.相反,HBase可以被认为是一个多维度的 ...

  2. How to: Create a Business Model in the XPO Data Model Designer

    How to: Create a Business Model in the XPO Data Model Designer This topic provides step-by-step inst ...

  3. How to: Supply Initial Data for the Entity Framework Data Model 如何:为EF数据模型提供初始数据

    After you have introduced a data model, you may need to have the application populate the database w ...

  4. ExtJS笔记 Ext.data.Model

    A Model represents some object that your application manages. For example, one might define a Model ...

  5. EF,ADO.NET Entity Data Model简要的笔记

    1. 新建一个项目,添加一个ADO.NET Entity Data Model的文件,此文件会生成所有的数据对象模型,如果是用vs2012生的话,在.Designer.cs里会出现“// Defaul ...

  6. Create Entity Data Model

    http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx 官 ...

  7. ExtJs Ext.data.Model 学习笔记

    Using a Proxy Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'], proxy: ...

  8. [ExtJs] ExtJs4.2 数据模型Ext.data.Model学习

    Model代表应用程序管理的一些对象.例如,我们可能会为 我们想在系统中建模的现实世界中的一些物体像使用者.产品和汽车等定义一个Model.这些Model在 Ext.ModelManager中注册,被 ...

  9. [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...

随机推荐

  1. JAVA描述算法和数据结构(01):稀疏数组和二维数组转换

    本文源码:GitHub·点这里 || GitEE·点这里 一.基本简介 1.基础概念 在矩阵中,若数值为0的元素数目远远多于非0元素的数目,并且非0元素分布没有规律时,则称该矩阵为稀疏矩阵:与之相反, ...

  2. Mybatis的原理分析1(@Mapper是如何生效的)

    接着我们上次说的SpringBoot自动加载原理.我们大概明白了在maven中引入mybatis后,这个模块是如下加载的. 可能会有人问了,一般我们的dao层都是通过Mapper接口+Mapper.x ...

  3. [Abp vNext 源码分析] - 5. DDD 的领域层支持(仓储、实体、值对象)

    一.简要介绍 ABP vNext 框架本身就是围绕着 DDD 理念进行设计的,所以在 DDD 里面我们能够见到的实体.仓储.值对象.领域服务,ABP vNext 框架都为我们进行了实现,这些基础设施都 ...

  4. Selenium(十七):unittest单元测试框架(三) 脚本分析、编写Web用例

    1. 带unittest的脚本分析 也许你现在心里还有疑问,unittest框架与我们前面所编写的Web自动化测试之间有什么必然联系吗?当然有,既然unittest可以组织.运行测试用例,那么为什么不 ...

  5. java学习第一步-工欲善其事必先利其器

    Java SE 磨刀不误砍柴工,工欲善其事必先利其器,咱们先搞好硬件配置,才能顺利的搞好Java学习 阶段一 1.认识Java 2.java发展史及用户 3.配置Java环境 4.JDK8下载安装 5 ...

  6. google跟踪代码管理器gtm无法给相同class元素绑定click事件埋点解决

    Google 跟踪代码管理器是一个跟踪代码管理系统 (TMS),可以帮助您快速轻松地更新网站或移动应用上的跟踪代码及相关代码段(统称为“代码”).将一小段跟踪代码管理器代码添加到项目后,您可以通过网页 ...

  7. 11gr2 alert日志中报TNS-12535 TNS-00505原因及解决方法

    前面新装了11GR2 RAC,某天在做巡检的时候发现alert日志中存在如下报错Fatal NI connect error 12170. VERSION INFORMATION: TNS for L ...

  8. MySql 表结构修改、约束条件、表关系

    表结构修改(alter) 查看表的结构:desc 表名; 修改表名:alter table 表名 rename to 新表名; 修改字段名:alter table 表名 change 旧字段名 新字段 ...

  9. MySql 库/表级操作 及 数据类型

    数据库分类 关系型数据库(SQL):存储方式固定,安全 非关系型数据库(NoSQL):存储方式比较灵活,存储数据的效率比较高,不太安全 MySQL是一种关系型数据库管理系统(采用关系模型来组织管理数据 ...

  10. python selenium 处理时间日期控件

    # -*- coding: utf-8 -*- from selenium import webdriverfrom time import sleep driver = webdriver.Fire ...