DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

原文:DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

这个系列通过一些简单例子循序渐进,介绍DevExpress ASP.NET控件的使用。先来介绍一下XPO的使用,安装的DevExpress版本为DXperienceUniversal-12.2.4,使用Visual Studio 2012+Sql Server2005。

什么是XPO?

XPO 是 eXpress Persistent Objects的缩写,它是DevExpress公司推出的一个运行在.NETFramwork平台上的ORM工具。Persistent Objects翻译过来时“持久化对象”的意思,所谓的持久化,也就是将数据存储下来,比如存在数据库、文件等这样的形式“永久的”保存下来。XPO是一个ORM工具,它在应用程序代码和数据库之间扮演了一个中间层的角色,起到中间桥梁这样一个作用,简单而言,就是将面向对象编程所建立的对象在数据库中做一个映射,使之和数据库中的表建立一一对应的关系。我们在面向对象编程的时候,只需要关心程序中的“对象”就可以了,XPO会自动的把我们对对象的操作反应到数据库中。

安装过程在此略过。。。

安装成功后,VS2012新建项目,就会有已安装DevExpress类别选项,如下图所示:

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建                                                                                                             (图一) 新建解决方案

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                               (图二)添加类库项目

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                           (图三) 建立XPO的ORM模型向导

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                (图四)选择映射到现有数据库

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

(图五)选择数据库,输入UserName,Password,此处勾选导和存储过程,后续步骤将出现存储过程选择界面

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                        (图六) 确认选择的表

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                    (图七)确认选择的存储过程

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                              (图八)数据库表、字段前缀、后缀等设置

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                              (图九)完成

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                           (图十) 新建模型未保存界面

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

                                                  (图十一)新建模型保存后,生成XXXCode目录

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

          (图十二) 生成代码目录结构

自动生成的代码:

ConnectionHelper.cs

using System;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
namespace XPOModel.DemoDB
{
    public static class ConnectionHelper
    {
        public const string ConnectionString = @"XpoProvider=MSSqlServer;data source=.;user id=demo;password=demo;initial catalog=DemoDB;Persist Security Info=true";
        public static void Connect(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption)
        {
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(ConnectionString, autoCreateOption);
            XpoDefault.Session = null;
        }
        public static DevExpress.Xpo.DB.IDataStore GetConnectionProvider(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption)
        {
            return XpoDefault.GetConnectionProvider(ConnectionString, autoCreateOption);
        }
        public static DevExpress.Xpo.DB.IDataStore GetConnectionProvider(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption, out IDisposable[] objectsToDisposeOnDisconnect)
        {
            return XpoDefault.GetConnectionProvider(ConnectionString, autoCreateOption, out objectsToDisposeOnDisconnect);
        }
        public static IDataLayer GetDataLayer(DevExpress.Xpo.DB.AutoCreateOption autoCreateOption)
        {
            return XpoDefault.GetDataLayer(ConnectionString, autoCreateOption);
        }
    }

}

 StoredProcHelper.cs

using System;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
namespace XPOModel.DemoDB
{
    public static class SprocHelper
    {
        public static DevExpress.Xpo.DB.SelectedData ExecAspNetPager(Session session, int pageSize, int curPage, string viewName, string fieldName, string orderField, string orderType, string where1)
        {
            return session.ExecuteSproc("", new OperandValue(pageSize), new OperandValue(curPage), new OperandValue(viewName), new OperandValue(fieldName), new OperandValue(orderField), new OperandValue(orderType), new OperandValue(where1));
        }
        public static DevExpress.Xpo.DB.SelectedData ExecSelectjqGridUsers(Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows)
        {
            return session.ExecuteSproc("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows));
        }
        public static System.Collections.Generic.ICollection<SelectjqGridUsersResult> ExecSelectjqGridUsersIntoObjects(Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows)
        {
            return session.GetObjectsFromSproc<SelectjqGridUsersResult>("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows));
        }
        public static XPDataView ExecSelectjqGridUsersIntoDataView(Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows)
        {
            DevExpress.Xpo.DB.SelectedData sprocData = session.ExecuteSproc("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows));
            return new XPDataView(session.Dictionary, session.GetClassInfo(typeof(SelectjqGridUsersResult)), sprocData);
        }
        public static void ExecSelectjqGridUsersIntoDataView(XPDataView dataView, Session session, int PageIndex, string SortColumnName, string SortOrderBy, int NumberOfRows)
        {
            DevExpress.Xpo.DB.SelectedData sprocData = session.ExecuteSproc("SelectjqGridUsers", new OperandValue(PageIndex), new OperandValue(SortColumnName), new OperandValue(SortOrderBy), new OperandValue(NumberOfRows));
            dataView.PopulateProperties(session.GetClassInfo(typeof(SelectjqGridUsersResult)));
            dataView.LoadData(sprocData);
        }
    }
}

Users.cs

using System;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
namespace XPOModel.DemoDB
{

    public partial class Users
    {
        public Users(Session session) : base(session) { }
        public override void AfterConstruction() { base.AfterConstruction(); }
    }

}

 Users.Designer.cs

using System;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
namespace XPOModel.DemoDB
{

    public partial class Users : XPLiteObject
    {
        int fUserID;
        [Key(true)]
        public int UserID
        {
            get { return fUserID; }
            set { SetPropertyValue<int>("UserID", ref fUserID, value); }
        }
        string fUserName;
        [Size(50)]
        public string UserName
        {
            get { return fUserName; }
            set { SetPropertyValue<string>("UserName", ref fUserName, value); }
        }
        string fFirstName;
        [Size(50)]
        public string FirstName
        {
            get { return fFirstName; }
            set { SetPropertyValue<string>("FirstName", ref fFirstName, value); }
        }
        string fLastName;
        [Size(50)]
        public string LastName
        {
            get { return fLastName; }
            set { SetPropertyValue<string>("LastName", ref fLastName, value); }
        }
        string fMiddleName;
        [Size(50)]
        public string MiddleName
        {
            get { return fMiddleName; }
            set { SetPropertyValue<string>("MiddleName", ref fMiddleName, value); }
        }
        string fEmailID;
        [Size(50)]
        public string EmailID
        {
            get { return fEmailID; }
            set { SetPropertyValue<string>("EmailID", ref fEmailID, value); }
        }
    }

}

至此,已经完成了从数据库到XPO模型的创建,接下来开始使用XPO对象,看看它给我们带来了什么?

下一节中,我们继续使用XPO,结合XpoDataSource与ASPxGridView,以极少的代码,完成单表CRUD操作。

DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

-------------------------------------------------------------------------------------------------------

附示例数据库的建库脚本,使用SQL Server2005 数据库,SQL语句如下:

CREATE DATABASE [DemoDB] ON  PRIMARY 
( NAME = N'DemoDB', FILENAME = N'C:\DemoDB.mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'DemoDB_log', FILENAME = N'C:\DemoDB_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'DemoDB', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [DemoDB].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [DemoDB] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [DemoDB] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [DemoDB] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [DemoDB] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [DemoDB] SET ARITHABORT OFF 
GO
ALTER DATABASE [DemoDB] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [DemoDB] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [DemoDB] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [DemoDB] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [DemoDB] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [DemoDB] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [DemoDB] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [DemoDB] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [DemoDB] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [DemoDB] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [DemoDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [DemoDB] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [DemoDB] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [DemoDB] SET  READ_WRITE 
GO
ALTER DATABASE [DemoDB] SET RECOVERY FULL 
GO
ALTER DATABASE [DemoDB] SET  MULTI_USER 
GO
ALTER DATABASE [DemoDB] SET PAGE_VERIFY CHECKSUM  
GO
USE [DemoDB]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [DemoDB] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO


USE [DemoDB]
GO

/****** Object:  Table [dbo].[Users]    Script Date: 2014/11/27 13:36:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Users](
	[UserID] [int] IDENTITY(1,1) NOT NULL,
	[UserName] [nvarchar](50) NULL,
	[FirstName] [nvarchar](50) NULL,
	[LastName] [nvarchar](50) NULL,
	[MiddleName] [nvarchar](50) NULL,
	[EmailID] [nvarchar](50) NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
	[UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

 

上一篇:ASP.Net简单页面方法加载顺序


下一篇:Hexo-NexT 设置近期文章