Llbl Gen 3.x source code tracing and parsing Type converter type converter

Source: Internet
Author: User

Typeconverter class. The description in msdn provides a unified method to convert the value type to other types, as well as access standard values and sub-attributes.
Let's take a look at the example to translate a string into a type converter with a point structure.
Public class pointconverter: typeconverter {
Public override bool canconvertfrom (itypedescriptorcontext context, type sourcetype ){
If (sourcetype = typeof (string) {return true ;}
Return base. canconvertfrom (context, sourcetype );
}

Public override object convertfrom (itypedescriptorcontext context, cultureinfo culture, object Value ){
If (value is string ){
String [] V = (string) value). Split (New char [] {','});
Return new point (Int. parse (V [0]), Int. parse (V [1]);
}
Return base. convertfrom (context, culture, value );
}
Public override object Convertor (itypedescriptorcontext context,
Cultureinfo culture, object value, type destinationtype ){
If (destinationtype = typeof (string) {return (point) value). x + "," + (point) value). Y ;}
Return base. convertize (context, culture, value, destinationtype );
}
This converter can convert the string between 1 and 2 to the Type Point.
For example, convert an object to a string
Point Pt = new point (1, 2 );
String point = typedescriptor. getconverter (PT). converttostring (PT );
In turn, convert from string to object
Point Pt = (point) typedescriptor. getconverter (typeof (point). convertfromstring ("1, 2 ");

Use this feature in llbl Gen to translate 1 or 0 of the bit type into the bool value true/false, and translate y or N of the string into the bool value true/false.
This enhances the type check during compilation and greatly reduces the chance of errors.

Typeconverter is used in llbl Gen 2.6. As shown in, sushortded applies the system. boolean type conversion.

Use different. Net Type lists the current application types. If no application type conversion is required, Click Reset to default.

Shows how to apply typeconverter in llbl Gen 3.1.

For example, if you want to apply typeconverter to the employeestatus string type, use 1 to indicate that the employee is in service, 0 to indicate that the employee has left the company, and set it first. net type is boolean type, and then set its typeconverter to use in field mappings.

F7: generate a projectSource codeTo view the persistenceinfoprovidercore type in the databasespecific project,
Let's take a look at the source of private void inituserentitymappings ().Code

Base. addelementfieldmapping ("userentity", "suincluded", "suincluded", true, (INT) sqldbtype. nvarchar, 1, 0, 0, false, "", new paradox. typeconverters. booleanstringconverter (), typeof (system. string), 2 );

The third to last parameter applies the booleanstringconverter type converter.

In the orm framework, how does 1/0 be converted to true/false.
Enter the employeestatus attribute of the employee
Public Virtual System. boolean employeestatus
{
Get {return (system. int32) getvalue (INT) employeefieldindex. employeestatusid, true );}
Set {setvalue (INT) employeefieldindex. employeestatusid, value );}
}

Enter the setvalue method of entitybase2
Protected bool setvalue (INT fieldindex, object value)
{
Return setvalue (fieldindex, value, true );
}

Enter the overload method protected bool setvalue (INT fieldindex, object value, bool extends mdesyncforfkfields)
From this method, we still did not find out how to apply typeconverter.

Let's take a look at the definition of employeestatus, which is defined as bit in the database.ProgramThe language has been defined as bool. The only conversion scenario is after select and before insert/update. In this way, find the basis in dynamicqueryengine.
Let's look at the implementation of its select method.
Protected override void createsingletargetinsertdq (ientityfieldcore [] fields, ifieldpersistenceinfo [] fieldspersistenceinfo,

{

If (persistenceinfo. isidentity)
{
Newparameter = This. creator. createparameter (field, persistenceinfo, _ outputparameterdirection );
Query. addparameterfieldrelation (field, newparameter, persistenceinfo. typeconvertertouse );
We can see the typeconvertertouse read from the ing file, which seems to find some clues. Right-click addparameterfieldrelation and choose View call hierarchy from the menu.

Expand implementations in the call hierarchy window to find the implementation types of this interface method, batchactionquery and query.

Enter batchactionquery, the method definition is empty
Public iparameterfieldrelation addparameterfieldrelation (ientityfieldcore field, dbparameter parameter, typeconverter typeconvertertouse)
{
Return NULL;
}

Go to the method definition in the query and see that a parameterfieldrelation object is constructed here.

Public iparameterfieldrelation addparameterfieldrelation (ientityfieldcore field, dbparameter parameter, typeconverter typeconvertertouse)
{
Iparameterfieldrelation relation = new parameterfieldrelation (field, parameter, typeconvertertouse );
If (_ parameterfieldrelations = NULL)
{
_ Parameterfieldrelations = new list <iparameterfieldrelation> ();
}
If (! _ Parameterfieldrelations. Contains (relation ))
{
_ Parameterfieldrelations. Add (relation );
}
Return relation;
}

Let's take a look at the annotation defined by parameterfieldrelation. Its function is to define the relationship between the query parameter and field.

Class to define the relation between a parameter of a query and a field. This relation is used to find back a related entityfieldcore instance when an output parameter is found in a query so the value
Of the output parameter can be assigned to the related entityfield
This type definition has three member variables.
Private readonly ientityfieldcore _ field;
Private readonly dbparameter _ parameter;
Private readonly typeconverter _ typeconvertertouse;
There is a method named sync, the method body is as follows
Public void sync ()
{
If (_ field! = NULL) & (_ parameter! = NULL ))
{
Object value = _ parameter. value;
# If! Cf
If (_ typeconvertertouse! = NULL)
{
Value = _ typeconvertertouse. convertfrom (null, null, value );
}
# Endif
_ Field. forcedcurrentvaluewrite (value );
}
}

The Compact Framework framework cannot use typeconverter. If typeconvertertouse is not empty, convert the passed value and assign it to the attribute.
Now, I understand how to use typeconverter, but I still don't know the time when it is called. Right-click method name sync and find all references to find all references.

In the query. CS file, it is used as follows:
Public void reflectoutputvaluesinrelatedfields ()
{
Foreach (iparameterfieldrelation relation in _ parameterfieldrelations)
{
// Reflect value in related field object.
Relation. Sync ();

Continue to find all references on the reflectoutputvaluesinrelatedfields method, and search by stack or above.
Enter the execute method of actionquery

Public int execute ()
{
Int returnvalue = This. Command. executenonquery ();

If (returnvalue> 0)
{
// Reflect new PK Field Values retrieved, if any, in their related fields.
This. reflectoutputvaluesinrelatedfields ();
}
Next, find all references on the execute method.
Go to the executeactionquery method of dataaccessadapterbase.
Public Virtual int executeactionquery (iactionquery querytoexecute)
{
Tracehelper. writelineif (tracehelper. persistenceexecutionswitch. traceinfo, "dataaccessadapterbase. executeactionquery", "method enter ");
Try
{
Preparequeryexecution (querytoexecute, false );
Return querytoexecute. Execute ();
}
Finally
{
Closeconnectionifpossible ();
Tracehelper. writelineif (tracehelper. persistenceexecutionswitch. traceinfo, "dataaccessadapterbase. executeactionquery", "method exit ");
}
}
The analysis of this part is the complete stack of the application type converter.

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.