1.1. Overview:
The. net code generated using the NetTier template, including the complete data layer, uses Microsoft's technology
EnterpriseLibrary1.1, which generates corresponding query functions and stored procedures for each table.
Supports multi-field query and paging. The data layer is a factory model. You only need to call the DataRepository class to obtain the corresponding table connection.
Port instance, and then you can modify the table. For multi-table join operations, you can obtain query records of subsets in the parent table.
Directory, which is saved in the IList container. For sub-tables, an object instance of the parent table is provided. Provides transaction functions for database operations
Yes. For more complex operations, you can directly provide the database operation instance of EnterpriseLibrary for direct operations.
Features:
1. Generate the vs.net project and solution;
2. Integrated with EnterpriseLibrary1.1, you can directly use the EnterpriseLibrary configuration file for database links.
Parts;
3. The ing between data tables and entities is. Each table has an entity generated, which corresponds to each
Fields. The object class is serialized and has a trigger event. Fields of Enumeration type are supported;
4. operations on the entities generated by tables and views include: basic CRUD operations: UPDATE, DELETE, INSERT,
Select all, paged select, FIND; supports primary key, foreign key, multi-Table Association, sorting, paging, SQL
Statement and View query;
5. Obtain strong datasets stored in Ilist or Vlist and can be bound to DataGrid, GridView, or
In other page controls, table sorting is supported;
6. Generate the webservice Service for data distribution;
7. Create a stored procedure script and automatically install it on the database server;
8. Generate a complete nant build. xml file, automatically compile and test the code, and generate APIs in chm format.
Document;
9. Generate complete data verification rules based on the database, including a framework for managing rules;
10. Each table has an entity class. You can inherit from your customer code and add your own handler.
Method;
11. Generating a data source eliminates the need to process the data source;
12. Create a full set of web admin user controls. You can perform web management on databases;
13. Complete nunit testing;
14. Complete annotations, which comply with Microsoft naming rules;
15. The nettiers template is free and open-source.
1.2. Framework Structure:
The data logic includes the customer business logic components and data interface logic components (persistence layer logic ).
Above.
The following uses the single-Table employee of the northwind database as an example.
Structure of the Employee data table:
Generated OO structure:
The interface calls DataRepository to obtain the provider of each function block, and then adds, deletes, modifies, and queries a single table.
If you need more complex business logic, such as using transactions to process several tables at the same time, you need to write additional business
Logic code. The logic layer code can be placed in the bl layer. It is worth mentioning that the generation generated in the bl Layer
Code, only the most basic and general business logic, only the entity class VO of single table data is generated, and the processing of each field of single table data is
And sorting and retrieval of data sets.
EmployeesCollection
EmployeesCollectionBase
Employees
EmployeesBase EmployeesVO
In DataRepository, the method used to obtain the provider of each function module is static.
Sqlprovider instances, which are provided to the outside world for their corresponding abstract class definition methods.
The instance call sequence is:
:
DataRepository
:
SqlDataProvider
:
SqlEmployeesProviderBase
1: EmployeesProvider
2: GetAll
3: GetByEmployeeID
Analysis of DataRepository. EmployeesProvider. GetAll () Operations: the order of Instantiation is
The DataRespository class maintains a global variable Current, which is actually
SqlDataProvider instance, while Current is of the DataProviderBase type, the relationship is:
DataProviderBase
SqlDataProvider
(From sqlclient)
When obtaining the EmployeesProvider in DataRepository
The EmployeesProvider () method in SqlDataProvider, and a new SqlEmployeesProvider
For example, the implementation of all methods in this instance is implemented in the SqlEmployeesProviderBase class, because
SqlEmployeesProvider inherits the SqlEmployeesProviderBase class, which is equivalent to the dao layer.
Database Operations.
SqlEmployeesProviderBase
EmployeesProviderBase
(From base)
SqlEmployeesProvider
When generating each sqlprovider instance, you must first instantiate SqlDataProvider.
The Activator class is generated based on the name of the class provided by the configuration file. To support different databases, nettier
Yes. A configuration file nettiersconfigdata. config and the dll used to read the configuration file are provided:
NetTiers. Configuration. dll.
The Activator class can dynamically construct objects, a bit like
Class. forName (ClassName). newInstance () statement. Because the Activator Class is used to construct an object, its Class name is
It can be uncertain during compilation, so it can be used as the plug-in interface to make a program that can be added with the DLL plug-in Winamp.
.
1.3. Method of calling the interface:
1.3.1. Get all data
Obtain all the Employee data, sorted by the LastName field:
Using Northwind. DataAccessLayer;
// Get all the employee, sort it on the LastName and print them out
TList <Employees> employees = DataRepository. EmployeeProvider. GetAll ();
Employees. Sort (EmployeeColumns. LastName, ListSortDirection. Ascending );
Foreach (Employee in employees)
{
Console. WriteLine ("{1} {0}", employee. FirstName, employee. LastName );
}
1.3.2. add data
Create a new employee and save it.
Using Northwind. DataAccessLayer;
// Create a new record and save
Employee employee = new Employee ();
Employee. FirstName = "John"; employee. LastName = "Doe ";
Employee. BirthDate = DateTime. Now; employee. Address = "10, fake street ";
Employee. City = "Metroplolis"; employee. Country = "USA ";
Employee. HireDate = DateTime. Now;
Employee. HomePhone = "0123456789 ";
Employee. Notes = "This is a fake employee ";
Employee. Title = "M ";
Employee. TitleOfCourtesy = "Dear ";
Employee. PostalCode = "5556 ";
Employee. Region = "New-York ";
DataRepository. EmployeeProvider. Insert (employee );
// Look, new id already populated
Console. WriteLine ("New Employee ID" + employee. Employee ID );
1.3.3. modify data
Obtain and modify data according to the subscript;
Using Northwind. DataAccessLayer;
// Select by Index and Update
TList <Employees> employees =
DataRepository. EmployeeProvider. GetByLastName ("Doe ");
If (employees. Count = 1)
{
Employees [0]. Notes = "This is a modified fake employee ";
DataRepository. EmployeeProvider. Save (employees [0]);
Console. Write (employees [0]);
}
1.3.4. delete data
Query and delete data using the primary key
Using Northwind. DataAccessLayer;
// Select by primary key and Delete
// (Demonstrate that insert, update, delete methods can also take collection
As parameter)
Employee employee = SqlDataRepository. EmployeeProvider. GetByEmployeeID (13 );
DataRepository. EmployeeProvider. Delete (employees );
1.3.5. Transaction Control
Transaction Control instances can use transactions to control insert, modify, and delete operations to ensure that all operations are successful or fail to be rolled back;
Using Northwind. DataAccessLayer;
// The SqlClient can work with transactions.
// Also show the Save method, wich encapsulate the use of Insert, Update and
Delete methods.
TransactionManager transaction = DataRepository. CreateTransaction ();
Transaction. BeginTransaction (/* IsolationLevel. ReadUncommited */);
Try
{
// Insert
Employee employee = new Employee ();
Employee. FirstName = "John ";
Employee. LastName = "Doe ";
Employee. BirthDate = DateTime. Now;
Employee. Address = "10, fake street ";
Employee. City = "Metroplolis ";
Employee. Country = "USA ";
Employee. HireDate = DateTime. Now;
Employee. HomePhone = "0123456789 ";
Mployee. Notes = "This is a fake employee ";
Employee. Title = "M ";
Employee. TitleOfCourtesy = "Doctor ";
Employee. PostalCode = "5556 ";
Employee. Region = "New-York ";
DataRepository. EmployeeProvider. Save (transaction, employee );
// Modify the employee instance
Employee. Notes = "This is a modified fake employee ";
// Update
DataRepository. EmployeeProvider. Save (transaction, employee );
Transaction. Commit ();
Console. WriteLine ("OK ");
}
Catch (Exception ex)
{
Try {transaction. Rollback ();} catch (){}
Console. WriteLine ("nok: {0}", ex );
}
1.3.6. Save Association
Deep storage: stores parent objects and subsets at the same time.
/*
DeepSave helper method can help you to save an object and its children in
One call.
*/
Using Northwind. DataAccessLayer;
Order order = Order. CreateOrder ("ALFKI", 1, DateTime. Now, DateTime. Now,
DateTime. Now, 1, 0.1 m, "ship name", "ship address", "paris", "idf", "75000 ",
"France ");
Order. OrderDetailCollection. Add (order. OrderID, 1, 15.6 m, 10, 0.02f );
Order. OrderDetailCollection. Add (order. OrderID, 2,122.6 m, 43, 0.03f );
DataRepository. OrderProvider. DeepSave (order );
Console. WriteLine ("new order saved: orderId is:" + order. OrderID. ToString ());
1.3.7. Multi-Database Support
Supports operations on multiple different databases at the same time. Configure different database connections in the el configuration file or
Dynamically generate different database connections in the code to use different types of databases at the same time.
/*
You can configure multiple data provider in the configuration console, and
Write code to acces a specific one, instead of the default.
*/
Using Northwind. DataAccessLayer;
SqlDataProvider myRepository = DataRepository. Providers ["my second data
Provider "] as Northwind. DataAccessLayer. SqlClient. SqlDataProvider;
This. listBox1.DataSource = myRepository. ProductProvider. GetAll ();
This. listBox1.DisplayMember = "ProductName ";
This. listBox1.ValueMember = "ProductID ";
// Or if you can't have it pre-configured, you can change the connection
String at runtime.
Using Northwind. DataAccessLayer;
// New syntax using a declared connection string:
TList <Products> list =
DataRepository. Connections ["NorthwindConnectionString2"]. Provider. CustomersProvide
R. GetAll ();
// New syntax using a dynamic connection string:
DataRepository. AddConnection ("DynamicConnectionString", "Data
Source = (local); Initial Catalog = Northwind; Integrated Security = true ;");
TList <Products <list =
DataRepository. Connections ["DynamicConnectionString"]. Provider. ProductsProvider. Ge
TAll ();
This. listBox1.DataSource = list;
This. listBox1.DisplayMember = "ProductName ";
This. listBox1.ValueMember = "ProductID ";
1.4. Configure the nettiers Template
1.4.1. Datasource directory:
The Database Name and connection parameters of SourceDatabase are displayed on the configuration page in codesmith connector E.
The tables whose SourceTables need to be materialized in the data table
EntireDatabase specifies the tables to be materialized in the database. This setting replaces SourceTables settings.
1.4.2. General Directory
OuputDirectory output directory. The configuration page is displayed in codesmith certificate e.
BusinessLogicLayerFolderName: logical layer sub-directory name. It is recommended to leave it empty.
DataAccessLayerFolderName: Sub-directory name of the data layer. Recommended Value: DataAccessLayer
SqlFolderName: Directory Name of the stored procedure script. Recommended: SQL
NameSpace project NameSpace, with the same name and directory.
GenerateUnitTest declare to generate nunit test project name
VsNetIntegration declares whether to generate an entire project or generate a project separately by layer.
VsNetVersion Vs.net version
1.4.3. Webservice Parameters
GenerateWebService declares whether to generate webservice
WebServiceOutputPath: Path of the file generated by Webservice
WebServiceUrl data layer subdirectory name; Recommended Value: DataAccessLayer
SqlFolderName indicates the url of WebServiceOutputPath
After the configuration is complete, you can save it as a configuration file. You only need to load it next time.
Added a web layer template called weblayer to add, delete, modify, and query a single table;
For an example of configuring the SQL Server database northwind, see the netier directory.
The property. xml file can be directly loaded and then the corresponding code is generated. The complete project is generated and can be compiled directly.
Run.
1.1. Overview:
The. net code generated using the NetTier template, including the complete data layer, uses Microsoft's technology
EnterpriseLibrary1.1, which generates corresponding query functions and stored procedures for each table.
Supports multi-field query and paging. The data layer is a factory model. You only need to call the DataRepository class to obtain the corresponding table connection.
Port instance, and then you can modify the table. For multi-table join operations, you can obtain query records of subsets in the parent table.
Directory, which is saved in the IList container. For sub-tables, an object instance of the parent table is provided. Provides transaction functions for database operations
Yes. For more complex operations, you can directly provide the database operation instance of EnterpriseLibrary for direct operations.
Features:
1. Generate the vs.net project and solution;
2. Integrated with EnterpriseLibrary1.1, you can directly use the EnterpriseLibrary configuration file for database links.
Parts;
3. The ing between data tables and entities is. Each table has an entity generated, which corresponds to each
Fields. The object class is serialized and has a trigger event. Fields of Enumeration type are supported;
4. operations on the entities generated by tables and views include: basic CRUD operations: UPDATE, DELETE, INSERT,
Select all, paged select, FIND; supports primary key, foreign key, multi-Table Association, sorting, paging, SQL
Statement and View query;
5. Obtain strong datasets stored in Ilist or Vlist and can be bound to DataGrid, GridView, or
In other page controls, table sorting is supported;
6. Generate the webservice Service for data distribution;
7. Create a stored procedure script and automatically install it on the database server;
8. Generate a complete nant build. xml file, automatically compile and test the code, and generate APIs in chm format.
Document;
9. Generate complete data verification rules based on the database, including a framework for managing rules;
10. Each table has an entity class. You can inherit from your customer code and add your own handler.
Method;
11. Generating a data source eliminates the need to process the data source;
12. Create a full set of web admin user controls. You can perform web management on databases;
13. Complete nunit testing;
14. Complete annotations, which comply with Microsoft naming rules;
15. The nettiers template is free and open-source.
1.2. Framework Structure:
The data logic includes the customer business logic components and data interface logic components (persistence layer logic ).
Above.
The following uses the single-Table employee of the northwind database as an example.
Structure of the Employee data table:
Generated OO structure:
The interface calls DataRepository to obtain the provider of each function block, and then adds, deletes, modifies, and queries a single table.
If you need more complex business logic, such as using transactions to process several tables at the same time, you need to write additional business
Logic code. The logic layer code can be placed in the bl layer. It is worth mentioning that the generation generated in the bl Layer
Code, only the most basic and general business logic, only the entity class VO of single table data is generated, and the processing of each field of single table data is
And sorting and retrieval of data sets.
EmployeesCollection
EmployeesCollectionBase
Employees
EmployeesBase EmployeesVO
In DataRepository, the method used to obtain the provider of each function module is static.
Sqlprovider instances, which are provided to the outside world for their corresponding abstract class definition methods.
The instance call sequence is:
:
DataRepository
:
SqlDataProvider
:
SqlEmployeesProviderBase
1: EmployeesProvider
2: GetAll
3: GetByEmployeeID
Analysis of DataRepository. EmployeesProvider. GetAll () Operations: the order of Instantiation is
The DataRespository class maintains a global variable Current, which is actually
SqlDataProvider instance, while Current is of the DataProviderBase type, the relationship is:
DataProviderBase
SqlDataProvider
(From sqlclient)
When obtaining the EmployeesProvider in DataRepository
The EmployeesProvider () method in SqlDataProvider, and a new SqlEmployeesProvider
For example, the implementation of all methods in this instance is implemented in the SqlEmployeesProviderBase class, because
SqlEmployeesProvider inherits the SqlEmployeesProviderBase class, which is equivalent to the dao layer.
Database Operations.
SqlEmployeesProviderBase
EmployeesProviderBase
(From base)
SqlEmployeesProvider
When generating each sqlprovider instance, you must first instantiate SqlDataProvider.
The Activator class is generated based on the name of the class provided by the configuration file. To support different databases, nettier
Yes. A configuration file nettiersconfigdata. config and the dll used to read the configuration file are provided:
NetTiers. Configuration. dll.
The Activator class can dynamically construct objects, a bit like
Class. forName (ClassName). newInstance () statement. Because the Activator Class is used to construct an object, its Class name is
It can be uncertain during compilation, so it can be used as the plug-in interface to make a program that can be added with the DLL plug-in Winamp.
.
1.3. Method of calling the interface:
1.3.1. Get all data
Obtain all the Employee data, sorted by the LastName field:
Using Northwind. DataAccessLayer;
// Get all the employee, sort it on the LastName and print them out
TList <Employees> employees = DataRepository. EmployeeProvider. GetAll ();
Employees. Sort (EmployeeColumns. LastName, ListSortDirection. Ascending );
Foreach (Employee in employees)
{
Console. WriteLine ("{1} {0}", employee. FirstName, employee. LastName );
}
1.3.2. add data
Create a new employee and save it.
Using Northwind. DataAccessLayer;
// Create a new record and save
Employee employee = new Employee ();
Employee. FirstName = "John"; employee. LastName = "Doe ";
Employee. BirthDate = DateTime. Now; employee. Address = "10, fake street ";
Employee. City = "Metroplolis"; employee. Country = "USA ";
Employee. HireDate = DateTime. Now;
Employee. HomePhone = "0123456789 ";
Employee. Notes = "This is a fake employee ";
Employee. Title = "M ";
Employee. TitleOfCourtesy = "Dear ";
Employee. PostalCode = "5556 ";
Employee. Region = "New-York ";
DataRepository. EmployeeProvider. Insert (employee );
// Look, new id already populated
Console. WriteLine ("New Employee ID" + employee. Employee ID );
1.3.3. modify data
Obtain and modify data according to the subscript;
Using Northwind. DataAccessLayer;
// Select by Index and Update
TList <Employees> employees =
DataRepository. EmployeeProvider. GetByLastName ("Doe ");
If (employees. Count = 1)
{
Employees [0]. Notes = "This is a modified fake employee ";
DataRepository. EmployeeProvider. Save (employees [0]);
Console. Write (employees [0]);
}
1.3.4. delete data
Query and delete data using the primary key
Using Northwind. DataAccessLayer;
// Select by primary key and Delete
// (Demonstrate that insert, update, delete methods can also take collection
As parameter)
Employee employee = SqlDataRepository. EmployeeProvider. GetByEmployeeID (13 );
DataRepository. EmployeeProvider. Delete (employees );
1.3.5. Transaction Control
Transaction Control instances can use transactions to control insert, modify, and delete operations to ensure that all operations are successful or fail to be rolled back;
Using Northwind. DataAccessLayer;
// The SqlClient can work with transactions.
// Also show the Save method, wich encapsulate the use of Insert, Update and
Delete methods.
TransactionManager transaction = DataRepository. CreateTransaction ();
Transaction. BeginTransaction (/* IsolationLevel. ReadUncommited */);
Try
{
// Insert
Employee employee = new Employee ();
Employee. FirstName = "John ";
Employee. LastName = "Doe ";
Employee. BirthDate = DateTime. Now;
Employee. Address = "10, fake street ";
Employee. City = "Metroplolis ";
Employee. Country = "USA ";
Employee. HireDate = DateTime. Now;
Employee. HomePhone = "0123456789 ";
Mployee. Notes = "This is a fake employee ";
Employee. Title = "M ";
Employee. TitleOfCourtesy = "Doctor ";
Employee. PostalCode = "5556 ";
Employee. Region = "New-York ";
DataRepository. EmployeeProvider. Save (transaction, employee );
// Modify the employee instance
Employee. Notes = "This is a modified fake employee ";
// Update
DataRepository. EmployeeProvider. Save (transaction, employee );
Transaction. Commit ();
Console. WriteLine ("OK ");
}
Catch (Exception ex)
{
Try {transaction. Rollback ();} catch (){}
Console. WriteLine ("nok: {0}", ex );
}
1.3.6. Save Association
Deep storage: stores parent objects and subsets at the same time.
/*
DeepSave helper method can help you to save an object and its children in
One call.
*/
Using Northwind. DataAccessLayer;
Order order = Order. CreateOrder ("ALFKI", 1, DateTime. Now, DateTime. Now,
DateTime. Now, 1, 0.1 m, "ship name", "ship address", "paris", "idf", "75000 ",
"France ");
Order. OrderDetailCollection. Add (order. OrderID, 1, 15.6 m, 10, 0.02f );
Order. OrderDetailCollection. Add (order. OrderID, 2,122.6 m, 43, 0.03f );
DataRepository. OrderProvider. DeepSave (order );
Console. WriteLine ("new order saved: orderId is:" + order. OrderID. ToString ());
1.3.7. Multi-Database Support
Supports operations on multiple different databases at the same time. Configure different database connections in the el configuration file or
Dynamically generate different database connections in the code to use different types of databases at the same time.
/*
You can configure multiple data provider in the configuration console, and
Write code to acces a specific one, instead of the default.
*/
Using Northwind. DataAccessLayer;
SqlDataProvider myRepository = DataRepository. Providers ["my second data
Provider "] as Northwind. DataAccessLayer. SqlClient. SqlDataProvider;
This. listBox1.DataSource = myRepository. ProductProvider. GetAll ();
This. listBox1.DisplayMember = "ProductName ";
This. listBox1.ValueMember = "ProductID ";
// Or if you can't have it pre-configured, you can change the connection
String at runtime.
Using Northwind. DataAccessLayer;
// New syntax using a declared connection string:
TList <Products> list =
DataRepository. Connections ["NorthwindConnectionString2"]. Provider. CustomersProvide
R. GetAll ();
// New syntax using a dynamic connection string:
DataRepository. AddConnection ("DynamicConnectionString", "Data
Source = (local); Initial Catalog = Northwind; Integrated Security = true ;");
TList <Products <list =
DataRepository. Connections ["DynamicConnectionString"]. Provider. ProductsProvider. Ge
TAll ();
This. listBox1.DataSource = list;
This. listBox1.DisplayMember = "ProductName ";
This. listBox1.ValueMember = "ProductID ";
1.4. Configure the nettiers Template
1.4.1. Datasource directory:
The Database Name and connection parameters of SourceDatabase are displayed on the configuration page in codesmith connector E.
The tables whose SourceTables need to be materialized in the data table
EntireDatabase specifies the tables to be materialized in the database. This setting replaces SourceTables settings.
1.4.2. General Directory
OuputDirectory output directory. The configuration page is displayed in codesmith certificate e.
BusinessLogicLayerFolderName: logical layer sub-directory name. It is recommended to leave it empty.
DataAccessLayerFolderName: Sub-directory name of the data layer. Recommended Value: DataAccessLayer
SqlFolderName: Directory Name of the stored procedure script. Recommended: SQL
NameSpace project NameSpace, with the same name and directory.
GenerateUnitTest declare to generate nunit test project name
VsNetIntegration declares whether to generate an entire project or generate a project separately by layer.
VsNetVersion Vs.net version
1.4.3. Webservice Parameters
GenerateWebService declares whether to generate webservice
WebServiceOutputPath: Path of the file generated by Webservice
WebServiceUrl data layer subdirectory name; Recommended Value: DataAccessLayer
SqlFolderName indicates the url of WebServiceOutputPath
After the configuration is complete, you can save it as a configuration file. You only need to load it next time.
Added a web layer template called weblayer to add, delete, modify, and query a single table;
For an example of configuring the SQL Server database northwind, see the netier directory.
The property. xml file can be directly loaded and then the corresponding code is generated. The complete project is generated and can be compiled directly.
Run.