SMARTPERSISTENCELAYER 2.0 foundation settings
Now I have to talk about how the SPL should be applied in the system. Through this article, you will learn how to configure SPL to your system.
SPL operation principle
As a data access platform, SPL is finally reflected in the DLL, then the system can start applying the SPL's DLL.
The SPL will depend on two XML configuration files: Data source profile (here I call DatabaseMap.xml) and entity mapping file (hereinafter referred to as ClassMap.xml). When these two configuration files run throughout the system, load one-time load into memory, which increases the SPL's running efficiency, so if the two configuration files are modified, IisReset or recompile can see the effect.
DatabaseMap.xml
DatabaseMap.xml is a data source configuration file, configuring a database connection here, currently supporting three data sources (SQL Server, Oracle, Access), can configure multiple multiple data sources in this file, and take an example below :
XML Version = "1.0" encoding = "UTF-8"?>
database>
database>
database>
map>
The above lists the configuration information of three databases, where:
Database's Name refers to the data source name to use in the SPL. This value can be uniquely identified, so that you can use Name directly in the SPL.
There are three types of Database: mssqlserver means SQL database
Msaccess refers to Access database
Oracle means Oracle Database
Parameter is the various parameters to be used in the connection, which will have different values depending on the different data source types, and the specific reference is the above writing.
If you want to extend the parameters, you can add Parameter directly because the SPL reads [Value] to the connection string from the parameter.
There is a
Classmap.xml
Classmap.xml is an entity and table mapping relationship. I will explain with a typical example. For example, there is a table as a student watch Student, there are fields:
ID number INT PK automatic growth
NO students VARCHAR (40) PK
Name Name Varchar (40)
Birthday date Dataime
Grade grade Int
SCORE enrollment results Decimal
The corresponding mapping information is:
We are easy to understand from the above configuration:
Class's Name refers to the entity name, which is the same as the name of the entity class.
Table means a table name of the mapped database;
Database is a database source that specifies the default database, corresponding to DatabaseMap.xml configuration;
IssavetomeMory is an optional configuration. If this table is maintenance data, there is less field, less record, low modification, high frequency of frequency, you can set IssaveTomEMORY = "True", so that the data of this table will automatically save Take it, it will be explained later.
Attribute is a corresponding entity property and a table field name mapping,
Name is the entity's attribute name, which is the same as the properties in the entity class;
Column is the field name of the table, which is the same as the fields in the table;
TYPE is a type, such as integer integer, character String, date Date, digital Decimal, etc.
INCREMENT means that the field is automatically increased, which is identified as true, so the SPL will automatically generate from the database;
The key value indicates that this field is the primary key, and each table must have a primary key in the SPL, which is the unique identifier of the table;
Class's TYPE comparison table:
SQL Server
Oracle
TYPE in XML
.NET built-in type
Bigint
Nubmer
Bigint
System.Int64
binary
Binary
System.byte []
Bit
Boolean
System.Boolean
charr
Charr
String
System.string
Datetime
Date
Date
System.Datetime
Decimal
Number
Decimal
System.decimal
Float
Float
Double
System.double
Image
Binary
System.byte []
int
Number
Integer
System.Int32
Money
Decimal
System.decimal
nchar
Nchar
String
System.string
NText
String
System.string
Numeric
Number
Decimal
System.decimal
nvarchar
NVARCHAR2
String
System.string
REAL
REAL
Single
System.single
SmallDateTime
Date
Date
System.Datetime
Smallint
Number
Smallint
System.Int16
Smallmoney
Decimal
System.decimal
SQL_VARIANT
Object
System.Object
TEXT
String
System.string
Timestamp
Binary
System.byte []
Tinyint
Tinyint
SYSTEM.BYTE
UniqueIdentifier
GUID
System.guid
Varbinary
Binary
System.byte []
VARCHAR
VARCHAR2
String, varchar
System.string
Entity class definition
Through the above XML configuration, we have learned the definition method of o / r mapping. My suggestion is to create a new project and namespace because these entity classes are very different, and can be generated by code generator. I will give an example to explain the physical class structure:
Namespace Business Studies
{
Using system;
Using system.collections;
Using system.data;
Using personistencelayer;
Public Class Studentent: EntityObject
{
// Constant definition part
Public const string __id = "id";
Public const string __no = "no";
Public const string __name = "name";
Public const string __birdhday = "birthday";
Public const string __grade = "grade";
Public const string __score = "score";
// Local variable definition part
PRIVATE INT M_ID;
PRIVATE STRING M_NO;
Private string m_name;
Private system.datetime m_birdhday;
PRIVATE INT M_GRADE;
Private system.decimal m_score;
Public studENTENTITY ()
{
}
// Property definition part
Public Int ID
{
get
{
Return this.mid;
}
set
{
THIS.M_ID = VALUE;
}
}
Public String No
{
get
{
Return this.m_no;
}
set
{
THIS.M_NO = VALUE;
}
}
Public String Name
{
get
{
Return this.m_name;
}
set
{
THIS.M_NAME = VALUE;
}
}
Public system.datetime birthday
{
get
{
Return this.m_birthday;
}
set
{
THIS.M_ birthday = value;
}
}
Public Int grade
{
get
{
Return this.m_grade;
}
set
{
THIS.M_GRADE = VALUE;
}
}
Public System.Decimal Score
{
get
{
Return THIS.M_SCORE;
}
set
{
THIS.M_SCORE = VALUE;
}
}
}
}
The explanation is as follows:
1. The Namespace of the entity class can be defined by themselves, as long as it is defined, remember that it can be referenced in the system.
2. The inusing memory will be used.
3. The physical classes inherit in EntityObject, a Public Abstract class that is in the SPL, as long as the specific entity class is inherited, the entity class has Save (), retrieve (), delete () entity operation function.
4. The first part of the entity class is a constant definition. In other PLs, the constant definition is not mentioned, and this constant definition is very meaningful, because in future system applications, it is often not physically With entity attribute names, such as defining condition, you can use the "." Operation from the physical class, if you use the constant. 5. The second part of the entity is partial variable, as long as we define the correct type.
6. The third part of the entity is attribute, that is, the corresponding set and GET can be given to the local variable.
Through the above code, it is very simple to implement the definition of the entity class. So far the configuration of the SPL is completed, that is, our preparation of the SPL is completed.
Initial use
When we have the above preparation, use it in the application system, as long as one step is possible, it is initializing a SPL, we can complete a setting class in the SPL:
String DatabaseXML = "config / datamap.xml";
Setting.instance (). DatabaseMapFile = Server.MAppath (DatabaseXML);
It is through the above way, the address of the DatabaseMapFile is OK, it is of course an absolute address, so for the web, we have to make mappath, and for the WinForm system, as long as the relative directory of the EXE execution directory is Yes.
Initialization As long as the system is running once at the beginning, it can be placed in Global for web, and I am used to putting it on the web of the web, as long as WinForm is executed on the first Form, it is OK. Initialization is automatically performed when the SPL is used.
safety
Of course, everyone will naturally think of the security of the connection, the security of the connection should be handled by everyone's own system, such as a simple way, we can change the XML suffix to the config suffix, so others can't see Config. This is certainly not too safe, and everyone may also be placed in XML after encryption, and the decryption connection is performed.
Ok, the above configuration is actually relatively simple, of course, these configurations, we can do through the code generator because these memory is basically the same.
Listen to
November 2004
MSN: TINTOWN_LIU@hotmail.com