Chapter Schema

Schema是XF的核心,每一个读写方法都有一个相关联的Schema。方法首先解析Schema,然后再根据Schema的配置的执行。

那Schema是什么呢?Schema是一个XElement,它的构成如下:

DatabaseSchema = Database Structure + NameMap + DatabaseConfig

PrimarySchema(一个) = DatabaseSchema + PrimaryConfig

NamedSchema(多个)= PrimarySchema + NamedConfig(多个)

Schema = PrimarySchema 或 NamedSchema + modifying(可选)

以上+号,代表Modify,+号右边的用来修改左边的,除了Database Structure与NameMap,其余均为XElement。

首先XF从数据库获得数据库结构(Database Structure),然后根据 NameMap,指定TableName、ColumnName与SetName 、ElementName、FieldName的映射。最后用DatabaseConfig修饰(Modify),得到DatabaseSchema。举例如下:

注:修饰(Modify)类似文件夹Copy,目标文件夹中存在的文件覆盖,不存在的添加。

如在数据库中有

Table:Users

PrimaryKey:Id

ColumnName

DataType

AllowDBNull

Id

int

N

UserName

nvarchar(50)

N

Password

nvarchar(50)

N

Description

nvarchar(254)

Y

对应的XML,有一种形式如下(丢掉了nvarchar的长度)

<Users PrimaryKey="Id">

<Id AllowDBNull="False" SqlDbType="int"/>

<UserName AllowDBNull="False" SqlDbType="nvarchar"/>

<Password AllowDBNull="False" SqlDbType="nvarchar"/>

<Description AllowDBNull="True" SqlDbType="nvarchar"/>

</Users>

给上述XML变个形,加点料,看上去像个User 类

<User Table="Users" Set="Users" PrimaryKey="Id">

<Id Column="Id" DataType="System.Int32" AllowDBNull="False" SqlDbType="int"/>

<UserName Column="UserName" DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Password Column="Password" DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Description Column="Description" DataType="System.String" AllowDBNull="True" SqlDbType="nvarchar"/>

</User>

XF把整个<User>称之为ElementSchema,User称为ElementName。<Id>、<UserName>、<Password>和<Description>称之为FieldSchema,同样Id、UserName、Password和Description称为FieldName。<User>还有一个重要的属性Set,这里Set意为集合,而不是设置,同理把Users称之为SetName(集合名)。

当ColumnName与FieldName一样的时候,可以省略Column属性,因此,User类的XML,可以简化为:

<User Table="Users" Set="Users" PrimaryKey="Id">

<Id DataType="System.Int32" AllowDBNull="False" SqlDbType="int"/>

<UserName DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Password DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Description DataType="System.String" AllowDBNull="True" SqlDbType="nvarchar"/>

</User>

XF内置了PrefixSuffixNameMap和SingularPluralNameMap二个类,用来把数据库中的每个表(和视图)生成类XML。(还有Foreign key生成Relationship,后述)。这二个类都继承自NameMap类。

上述例子中,用到的就是SingularPluralNameMap。其构造函数为:

public SingularPluralNameMap(XElement nameMapConfig)

当以复数形式命名数据库表时,请使用改类。参数nameMapConfig用来修饰(按单复数规律)生成后的XML。

譬如某些名词单复数一样或者你喜欢取一个自己喜欢的名称。如有以下nameMapConfig:

<UserElement Table="Users" Set="UserSet">

<Descr Column="Description" />

</UserElement>

则最终User类XML如下:

<UserElement Table="Users" Set="UserSet" PrimaryKey="Id">

<Id DataType="System.Int32" AllowDBNull="False" SqlDbType="int"/>

<UserName DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Password DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Descr Column="Description" DataType="System.String" AllowDBNull="True" SqlDbType="nvarchar"/>

</UserElement>

PrefixSuffixNameMap 假定表名是单数,用来Prefix + TableName + Suffix来命名SetName,参数nameMapConfig见上。

它有如下二个构造函数:

public PrefixSuffixNameMap(XElement nameMapConfig, string prefix, string suffix)

// 相当 public PrefixSuffixNameMap(XElement nameMapConfig, string prefix="SetOf", string suffix=string.Empty)

public PrefixSuffixNameMap(XElement nameMapConfig)

使用该类,则最终User类XML如下:

<Users Table="Users" Set="SetOfUsers" PrimaryKey="Id">

<Id DataType="System.Int32" AllowDBNull="False" SqlDbType="int"/>

<UserName DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Password DataType="System.String" AllowDBNull="False" SqlDbType="nvarchar"/>

<Description DataType="System.String" AllowDBNull="True" SqlDbType="nvarchar"/>

</Users>

数据库中不单有表(和视图),还有“关系”,二个表之间的关系,依赖Foreign key。同样,XF会把Foreign key以XML描述,称之为Relationship。

Relationship用到的名称均为ElementName和FieldName,而不是TableName和ColumnName。下图所示表间Foreign key:

会生成:

<Relationship From="Employee" To="Job" Type="ManyToOne" Content="Employee(JobId),Job(Id)"/>

<Relationship From="User" To="Employee" Type="ManyToOne" Content="User(EmployeeId),Employee(Id)"/>

<Relationship From="UserRole" To="Role" Type="ManyToOne" Content="UserRole(RoleId),Role(Id)"/>

<Relationship From="UserRole" To="User" Type="ManyToOne" Content="UserRole(UserId),User(Id)"/>

注:如果,你不喜欢在数据库中设置Foreign key,上面的那些Relationship,就需要通过DatabaseConfig或PrimaryConfig修饰(添加)到DatabaseSchema或PrimarySchema。

注:在生成PrimarySchema时,会自动(推导出)加上下列多对多的Relationship:

<Relationship From="Role" To="User" Type="ManyToMany" Content="Role(Id),UserRole(RoleId);UserRole(UserId),User(Id)"/>

注:在DatabaseConfig或PrimaryConfig(在PrimaryConfig更合适),还可以添加如下<ReferencePath>:

<ReferencePath Name=” User- Job” From="User" To="Job" Content="User(EmployeeId),Employee(Id);Employee(JobId),Job(Id)">

XF把二个及以上ManyToOne或OneToOne按ToOne方向串联起来称之为ReferencePath,一般地ReferencePath有一个Name属性,以方便引用(如ReferencePath.Name=” User- Job”)。ReferencePath提供二个表之间间接的关联。

至此DatabaseSchema初稿已经生成,如果还有要补充的内容,可通过DatabaseConfig(也可以通过PrimaryConfig)修饰。

DatabaseConfig典型应用就是为表(Table)中的一列(Column,通常为主键)指定序列(Sequence)。

如在DatabaseConfig中有如下片段:

……

<Employee>

<Id>

<Sequence>

<SequenceName>Sequence-Employees</SequenceName>

</Sequence>

</Id>

……

</Employee>

……

<User>

<Id>

<Sequence>

<SequenceName>Sequence-Users</SequenceName>

</Sequence>

</Id>

……

</User>

……

修饰后DatabaseSchema 相应片段为

……

<Employee Table="Employees" PrimaryKey="Id" Set="Employees" TableType="Table">

<Id DataType="System.Int32" AllowDBNull="False" Unique="True" ReadOnly="False" SqlDbType="int">

<Sequence>

<SequenceName>Sequence-Employees</SequenceName>

</Sequence>

</Id>

……

</Employee>

……

<User Table="Users" PrimaryKey="Id" Set="Users" TableType="Table">

<Id DataType="System.Int32" AllowDBNull="False" Unique="True" ReadOnly="False" SqlDbType="int">

<Sequence>

<SequenceName>Sequence-Users</SequenceName>

</Sequence>

</Id>

……

</User>

……

XF把作为子节点插入的Element称之为Attribute。实际也对应一个继承自Attribute的类。

以<Sequence>为例,对应:

public class SequenceAttribute : Attribute

{

public string SequenceName { get; private set; }

public SequenceAttribute(string sequenceName)

{

SequenceName = sequenceName;

}

}

注意与XAttribute的区别,如下XML片段,Table、PrimaryKey、Set、TableType都是XAttribute(属于User的),同样DataType、AllowDBNull、Unique、ReadOnly、SqlDbType也是(属于Id的)

<User Table="Users" PrimaryKey="Id" Set="Users" TableType="Table">

<Id DataType="System.Int32" AllowDBNull="False" Unique="True" ReadOnly="False" SqlDbType="int">

这些XAttribute,属于不可配置的数据库属性。方法解析Schema时,只会解析Attribute,而不会理会XAttribute。

XF另外还定义了一个PrimeAttribute类(标记类,无参构造函数,无属性),用于<Relationship>和<ReferencePath>标注首选项。

除了XF自己定义的二个Attribute类,还支持下列.NET Framework Attribute类:

(也支持System.ComponentModel.DataAnnotations.ValidationAttribute的子类,详见Chapter Data Validation,在此处不列出)

System.ComponentModel.DataAnnotations.KeyAttribute

System.ComponentModel.DataAnnotations.TimestampAttribute

System.ComponentModel.DataAnnotations.ConcurrencyCheckAttribute

System.ComponentModel.DefaultValueAttribute

System.ComponentModel.DisplayNameAttribute

System.ComponentModel.DataAnnotations.DisplayAttribute

一个真实的DatabaseSchema片段:

<User Table="Users" PrimaryKey="Id" Set="Users" TableType="Table">

<Id DataType="System.Int32" AllowDBNull="False" Unique="True" ReadOnly="False" SqlDbType="int">

<Key/>

<Sequence>

<SequenceName>Sequence-Users</SequenceName>

</Sequence>

</Id>

<EmployeeId DataType="System.Int32" AllowDBNull="False" Unique="False" ReadOnly="False" SqlDbType="int">

<Required/>

</EmployeeId>

<UserName DataType="System.String" AllowDBNull="False" Unique="True" ReadOnly="False" SqlDbType="nvarchar">

<Required/>

</UserName>

<Password DataType="System.String" AllowDBNull="False" Unique="False" ReadOnly="False" SqlDbType="nvarchar">

<Required/>

<DefaultValue>

<Value>a1b2c3</Value>

</DefaultValue>

</Password>

<Description DataType="System.String" AllowDBNull="True" Unique="False" ReadOnly="False" SqlDbType="nvarchar"/>

</User>

至此DatabaseSchema已完全生成,再经由PrimaryConfig修饰(Modify),就得到PrimarySchema。

如上所述,DatabaseConfig的内容,全都可以放在PrimaryConfig,DatabaseConfig甚至可以没有。DatabaseConfig的存在,是为了层次更清晰。除了这些PrimaryConfig还可以有哪些内容呢?PrimaryConfig的主要作用就是横向扩展Field。如有以下PrimaryConfig片段:

<User>

<Employee.Name Element="Employee" Field="Name" Relationship.Content="User(EmployeeId),Employee(Id)"/>

<Job.Name Element="Job" Field="Name" ReferencePath.Name=" User- Job"/>

<Job.Descr Element="Job" Field="Descr" ReferencePath.Name=" User- Job"/>

</User>

这里明确指出了Relationship或ReferencePath。在未指定的情况:

1.在所有包括<Prime>的Relationship和ReferencePath中,按 非多对多Relationship、多对多Relationship、ReferencePath查找;

2.在不包括<Prime>的Relationship和ReferencePath中,也按 非多对多Relationship、多对多Relationship、ReferencePath查找;

3.试图推导ReferencePath,(只用于ElementQuerier,见Chapter Querying Data)

修饰(Modify)后,会添加到<User>。这里不再列出修饰(Modify)后的XML。

至此,最主要的PrimarySchema已经生成(DatabaseSchema是个过渡,主要用来生成PrimarySchema)。

针对不同的应用场景,可能需要特定应用场景的PrimarySchema变体。XF通过NamedConfig

针对不同的应用,可能需要特定应用的PrimarySchema变体。XF通过NamedConfig对PrimarySchema修饰(Modify)得到这个变体,称之为NamedSchema。

顾名思义,NamedSchema 必定有一个名称,这个名称就是Name XAttribute属性,显然,这个Name属性来自NamedConfig。

一般地,会把所有的NamedConfig放在同一个目录下,XF会根据这些NamedConfig,生成每一个NamedConfig对应的NamedSchema。

在调用方法时,通过指定SchemaName来取得对应的Schema(不指定、SchemaName为null或空字符串会取得PrimarySchema)。

如果调用方法需要更具个性化,还可以提供一个方法级别的config,即modifying。来修饰上述Schema,形成本次方法调用的Schema。

Chapter Schema的更多相关文章

  1. Chapter Configuration

    Chapter Configuration 在Web.config 或App.config的configuration节,插入如下配置: <configuration> …… <co ...

  2. Chapter Querying Data

    Chapter Querying Data XF获取数据的三种方法: 其中参数schema,参见 Chapter Schema. 下面列出一个后面将会用到的schema片段,称之为片段A: <U ...

  3. XF 文档 - Element Framework Doc

    配置篇 Chapter Configuration Schema篇 Chapter Schema 查询篇 Chapter Querying Data 数据更改及验证篇 Chapter Data Mod ...

  4. Chapter Data Modification & Chapter Data Validation

    Chapter Data Modification XF的数据提交,支持单行.集合和多层次的master-details结构的数据. Create 当提交如下数据 <Job> <Id ...

  5. MongoDB:The Definitive Guide CHAPTER 2 Getting Started

    MongoDB is very powerful, but it is still easy to get started with. In this chapter we’ll introduce ...

  6. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  7. 复杂和遗留的数据库schema

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50414652 In this chapter, we focus ...

  8. Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...

  9. Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity Framework Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity F ...

随机推荐

  1. C#执行DOS命令(CMD命令)

    在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了c#执行DOS命令,并返回结果.         //dosCommand Dos命令语句         publ ...

  2. C# & SQL Server大数据量插入方式对比

    以下内容大部分来自: http://blog.csdn.net/tjvictor/article/details/4360030 部分内容出自互联网,实验结果为亲测. 最近自己开发一个向数据库中插入大 ...

  3. Neutron分析(7)—— neutron-l3-agent HA solutions

    1. keepalived vrrp/conntrackd High availability features will be implemented as extensions or driver ...

  4. iMac一体机安装苹果和Win7双系统

    前几天,有个客户说有一苹果的一体机,想装苹果和Win7双系统.约好了时间,带上工具就过去了.去的路上,用手机上网查了一下苹果电脑装双系统的过程.虽然以前也有给苹果的电脑安装过双系统,但次数不多而且时间 ...

  5. elixir学习

    安装 brew install elixir atom配置 language-elixir atom-elixir elixir的shell iex :erlang.system_info(:otp_ ...

  6. cowboy使用restful的例子

    直接贴代码,一切尽在不言中 %% cowboy的restful的文档,一定要好好阅读http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest ...

  7. 使用express搭建第一个Web应用【Node.js初学】

    来源:http://jingyan.baidu.com/article/bad08e1ee501e009c8512106.html express是一个开源的node.js项目框架,初学者使用expr ...

  8. 获得Unix/Linux系统中的IP、MAC地址等信息

    获得Unix/Linux系统中的IP.MAC地址等信息 中高级  |  2010-07-13 16:03  |  分类:①C语言. Unix/Linux. 网络编程 ②手册  |  4,471 次阅读 ...

  9. libcurl上传文件,添加自定义头

    原文  http://www.cnblogs.com/meteoric_cry/p/4285881.html 主题 curl libcurl参数很多,一不小心就容易遇到问题.曾经就遇到过一个很蛋疼的问 ...

  10. Ubuntu防火墙 UFW 设置

    Ubuntu防火墙 UFW 设置 1.安装 sudo apt-get install ufw 2.启用 sudo ufw enable sudo ufw default deny 运行以上两条命令后, ...