Xcodefactory3.0 full introduction -- dataentrance simplifies data access

Source: Internet
Author: User
In the previous xcodefactory3.0 main interface introduction and simple examples, we have seen dataentrance several times, such as the initialization code in the main method: idatabaseinfomgr dbinfomgr = new databaseinfomgr ();
Dataentrance. initializedbaccesserfactory (dbinfomgr, new xdbaccesserfactory ());


In studentform, we will obtain the idbaccesser reference again: Private idbaccesser dealstudent = dataentrance. createdbaccesser (typeof (student ));

In fact, without dataentrance, we can still use the generated data layer code normally, but it is complicated. For example, we can directly obtain the idbaccesser reference: idbaccesser dealstudent = new studentsqldealer (conn );

In doing so, there are two defects:
(1) The database type is hardcoded in the code. The code above specifies the visitor who uses the SQL Server database. When the database type changes, all similar places must be manually modified.
(2) even if I only want to extract the value of a specified field of a specified object, I need to go through the following tedious process: idbaccesser dealstudent = new studentsqldealer (conn );
String wherestr = string. Format ("Where {0} = '000000'", student. _ id );
Student ST = (student) dealstudent. getaobject (wherestr );
Int age = ST. Age;


Maybe dealstudent is no longer needed after the age of 1001 students, but this is also the case. In practical applications, we will encounter many similar situations. If this happens every time, most users will be irritated. However, if this happens, the user will be happy: int age = (INT) dataentrance. getfieldvalue (typeof (student), "1001", student. _ id );

The above four pieces of code are required. here we can use one sentence to solve the problem, and dataentrance also solves the problem of binding to a specific database type.

As described above, we can see that dataentrance has two main objectives:
(1) isolate the code for using data visitors from the code for creating data visitors. This is a bit like an abstract factory model. If you need to change the database type during the application process, you only need to modify the field of the specified database type in the relevant configuration file, and the program does not need to be re-compiled.
(2) simplify the use of the data layer to the greatest extent. The preceding example shows the power of dataentrance.

Let's take a look at how dataentrance and dataentrance work?
First, Dataentrance is a static class, so that when we use it, we can directly reference its method through the class name, without the need to manage dataentrance instances.
Second, Dataentrance needs to be initialized at system startup.
Although dataentrance is a static class, it needs to be initialized. The initialization action is to set information about the database, such as the connection string and database type. Here, we need to consider that if the user changes the database configuration information while the program is running, our dataentrance should be able to adapt to the latest situation as well. To enable dataentrance to receive such notifications, I designed the idatabaseinfomgr interface, which released the dbconfigchanged event. It is defined as follows:

/// <Summary>
/// Idatabaseinfomgr is used to provide database connection information to dataentrance
/// </Summary>
Public interface idatabaseinfomgr
{
Databasetype getdbtype ();
String getconnstring ();
String getconnstring (string dbname); // for multiple databases
Void activatedbconnchangeeevent (); // external notification idatabaseinfomgr database connection information has changed

Event eventhandler dbconfigchanged;

Bool ismultidatabase {Get ;}
}

I pass the reference of this interface as the parameter initialized by dataentrance, so that dataentrance can make corresponding adjustments when the database configuration information changes. Let's take a look at the Declaration of dataentrance's initialization method:

Public static bool initializedbaccesserfactory (idatabaseinfomgr dbinfomgr, idbaccesserfactory dbfactory );

The first parameter is referenced by idatabaseinfomgr. We can implement the idatabaseinfomgr interface by ourselves. In fact, when xcodefactory is used to generate the data layer code, we have automatically generated a default databaseinfomgr. in the CS file, we can directly use the databaseinfomgr class defined in it, but I suggest you modify the IMPLEMENTATION OF THE getconnstring method and the getdbtype Method -- get the connection information and database type information from the configuration file, instead of writing them to method implementations.
The second parameter is an idbaccesserfactory reference. What is the use of the idbaccesserfactory interface? To illustrate it, let's take a look at the following code:

String name = dataentrance. getfieldvalue (typeof (mobileuser), "0", "name"). tostring ();

For dataentrance. in the getfieldvalue method, the first parameter we pass in is our data object type. That is to say, we need to obtain the corresponding response class instance based on the data object type, which needs to be completed through reflection, idbaccesserfactory does this, and idbaccesserfactory also caches the created data visitor instance. Because reflection cannot traverse custom assembly, I put xdbaccesserfactory. CS in the local folder enterpriseserverbase. dataaccess. The idbaccesserfactory interface is defined as follows:

Public interface idbaccesserfactory
{
Void initialize (databasetype dbtype, string connstr, bool cachaccesser );

Idbaccesser createdbaccesser (type dataclasstype );
Idbaccesser createdbaccesser (databasetype dbtype, string connstr, type dataclasstype );
Idbaccesser createdbaccesser (type dataclasstype, string dealernamespace );
}

Then, how does the createdbaccesser method create a visitor instance based on the name of the data object class? You must still remember the xcf Convention: The data catalog class name format is: "database table name + Database Type + dealer", and the data object class is in the same namespace as the visitor, so once the idbaccesserfactory knows the type of the data object class, it is natural that the visitor type can be pushed down based on the database type, and then the visitor instance can be created through reflection.
For the initialization of dataentrance, we usually only need to do this:

Idatabaseinfomgr dbinfomgr = new databaseinfomgr ();
Dataentrance. initializedbaccesserfactory (dbinfomgr, new xdbaccesserfactory ());


Again
Methods In the idbaccesser interface can basically find the corresponding static method in dataentrance. However, this static method has one more parameter than the corresponding idbaccesser method. This parameter is the type of the data object class. As mentioned above, this type is used to dynamically generate visitor instances in idbaccesserfactory. Therefore, in the past, we had to first create a new visitor and then perform data access operations. Now we don't have to worry about this. You can use dataentrance to perform operations on the data layer.
The following example shows how to find the name of the tutor based on the student ID:

// Do not use dataentrance:
Idbaccesser studentdealer = dbaccesserfactory. createdbaccesser (typeof (student ));
Student Stu = (student) studentdealer. getaobject ("where id = '001 '");
Idbaccesser mentordealer = dbaccesserfactory. createdbaccesser (typeof (MENTOR ));

Mentor mentor = (MENTOR) mentordealer. getaobject (string. Format ("where id = '{0}'", Stu. mentorid ));
String thename = mentor. Name;

// You can do this using dataentrance:
String mentorid = dataentrance. getfieldvalue (typeof (student), "001", "mentorid"). tostring ();
String thename = dataentrance. getfieldvalue (typeof (MENTOR), mentorid, "name"). tostring ();

The advantage of using dataentrance is obvious, and our code is completely independent of the database type. You only need to specify the database type in one place, that is, the initialize method parameter of idbaccesserfactory. The value of this parameter is ultimately from the configuration file.

 

Last, We need to further explore dataentrance. dataentrance, just like its name, is the portal for accessing the data layer.I want to make all our interactions with the data layer passDataentrance, and we do not want to care about idbaccesserfactory any more.Now. So I decided to encapsulate everything in the static method of dataentrance (such as the initialization of idbaccesserfactory), so that we can use it as much as possible. Through the smart perception of vs. net, you can see all the public static methods of dataentrance. The functions of these methods are clear at a glance and basically correspond to the methods of the idbaccesser interface.
So far, we have been able to use dataentrance to complete all data layer access operations. If you only want to use the data-Layer Code generated by xcodefactory and do not want to care about other things, it is enough to know about dataentrance. If you want to know how the code works internally, for more advanced usage and other advanced content, please continue with my post.

Dataentrance source code

Xcodefactory3.0 full Guide directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.