Thinking about only XML in Orm without ing entities? Looking forward to your suggestions

Source: Internet
Author: User
Opening

Not written for a long timeArticleI have opened too many series before. On the one hand, I think about my work, and on the other hand, I am sorry for my summary of my knowledge during my business hours, because the previous series have always been

I don't have much to finish, but I can't help everyone here. I also want to sort out the series and ideas and the content to be written during the Chinese New Year, in the next year, we will release the following series:

1,Step by step-build your own ORM series-Index

2. Design Mode-series Indexes

3,System Architect-Basic to enterprise application architecture-series Index

4. Cloud computing-from basic to application architecture index series

Of course, there may be other related articles to be opened during this period, but I will not affect the progress of these articles. Otherwise, I am too sorry for everyone, and I hope you will put forward your own ideas and opinions, with your help, I can write

More comprehensive. Thank youThank you!

Summary

Recently, the following problems have been encountered in the project, and some relevant materials have been checked, but they have not been well resolved. The problem is as follows:

1. How to map data only through XML files? Of course, XML here is responsible for the corresponding information of the database table columns in the Orm.

2. You do not want to create an object for each table in the database.

3. Convenient operations on the interface.

4. Some adaptability and low coupling requirements should also be considered.

With the above requirements, I have come up with several ideas after thinking, but they are not ideal solutions. Please refer to the following solutions and give your opinions.

Solution

the above also gives a few questions. I have sorted out and considered these questions, the following solutions are obtained, but they are not ideal. If you put forward your requirements in depth, you will not be able to meet them.

1. Using nhib.pdf

of course, nhib.pdf itself has provided such operations. My special example is as follows. If you want to view details, please refer to the master blog Li yongjing in the garden.

here I will post the Code :

1. Check the ing file first.

 
    
     
      
     
     
     
   
 
    
 
     

2. For other configurations, I will not elaborate on the configuration of Nhibernate. The following shows the sample code:

Using (isession session = new sessionfactory (). opensession ())

{

Using (itransaction trans = session. begintransaction ())

{

Idictionary contract = new hashtable ();

Contract ["name"] = "contract name ";

Contract ["pay"] = 0.0 m; // Contract Amount

Contract ["Description"] = "contract description ";

// The first parameter is the object name used in the ing, and the second parameter is the instance

Session. Save ("contract", contract );

Trans. Commit ();

}

}

The stored code shown above is the same as the updated code. The query code is provided:

Using (isession session = new sessionfactory (). opensession ())

{

Using (itransaction trans = session. begintransaction ())

{

Idictionary contract = (idictionary) Session. createquery ("from contract where contractid =: ID ")

. Add ("ID", 1 );

. Uniqueresult ();

Session. Clear ();

Trans. Commit ();

}

}

Through the above Code and tests, we can indeed access the data through the corresponding methods provided by nhib.pdf. I will not give other corresponding operations, but we know from the above that we have not created the corresponding entities, I

We can complete the ing operation, which is indeed good, but such implementation is indeed inconvenient.

 

Ii. Using custom objects

How should we do this custom object? The idea of a custom object is as follows:

The following figure shows the sample code, which may not be run. The specific public object class code is as follows:

Public class commonobject: ilist

{

Private idictionary <string, column> Columns = NULL;

Public commonobject ()

{

Columns = new dictionary <string, column> ();

}

Public commonobject (INT capacity)

{

Columns = new dictionary <string, column> (capacity );

}

Public column add (column col)

{

This. Columns. Add (Col. Name, col );

Return Col;

}

Public bool remove (column col)

{

Return this. Columns. Remove (Col. Name );

}

Public column this [String key]

{< br>
Get

{< br>
return this. columns [Key];

}< br>
set

{< br>
This. columns [Key] = value;

}


Public int add (object value)

{

Throw new notimplementedexception ();

}

Public void clear ()

{

Throw new notimplementedexception ();

}

Public bool contains (object value)

{

Throw new notimplementedexception ();

}

Public int indexof (object value)

{

Throw new notimplementedexception ();

}

Public void insert (INT index, object value)

{

Throw new notimplementedexception ();

}

Public bool isfixedsize

{< br>
Get

{< br>
throw new notimplementedexception ();

}

Public bool isreadonly

{

Get

{

Throw new notimplementedexception ();

}

}

Public void remove (object value)

{

Throw new notimplementedexception ();

}

Public void removeat (INT index)

{< br>
throw new notimplementedexception ();

}

Public object this [int Index]

{< br>
Get

{< br>
throw new notimplementedexception ();

}< br>
set
{
throw new notimplementedexception ();

}

Public void copyto (array, int index)

{

Throw new notimplementedexception ();

}

Public int count

{

Get

{

Throw new notimplementedexception ();

}

}

Public bool issynchronized

{< br>
Get

{< br>
throw new notimplementedexception ();

}

Public object syncroot

{

Get

{

Throw new notimplementedexception ();

}

}

Public ienumerator getenumerator ()

{

Throw new notimplementedexception ();

}

}

The specific code has been given above. The following shows the column sample code:

Class Column

{

Public string name

{

Get;

Set;

}

Public object Value

{

Get;

Set;

}

Public System. Data. dbtype datatype

{

Get;

Set;

}

Public String dbcolumnname

{

Get;

Set;

}

Public int Length

{

Get;

Set;

}

Public bool isnull

{

Get;

Set;

}

}

Of course, the above is not complete, just for demonstration instructions. In this way, the ing with XML can be completed, and the specific access process can be relatively more friendly.

Public void test ()

{

Commonobject comobj = commonfactory <commonobject> ();

Comobj. Add (new column ());


Comobj ["test"] = new column ();

}

Private t commonfactory <t> ()

{

Return (t) activator. createinstance (typeof (t ));

}

3. Using Dynamic types in. Net 4.0

The following describes the implementation methods:

We use dynimic in. net4.0 to define an instance in memory. This instance is dynamically created through the column attributes configured in the XML file. The specific code is as follows:

Static void main (string [] ARGs)

{

Dynamic OBJ = gettest ();

Console. writeline (obj. Name );

Console. writeline (obj. tt );

System. Threading. thread. Sleep (10000 );

}

Private static dynamic gettest ()

{

Dynamic OBJ = new expandoobject ();

VaR person = OBJ as idictionary <string, Object>;

Person ["name"] = "test ";

Person ["TT"] = "AAA ";

Return OBJ;

}

Through the above ideas, we can do the following:

Private static dynamic gettest1 (string xmlfile)

{

Dynamic OBJ = new expandoobject ();

VaR person = OBJ as idictionary <string, Object>;

Xmldocument Doc = new xmldocument ();

Doc. loadxml (xmlfile );

Property [] list = xmlhelper. getpropertys (DOC );

Foreach (Property in List)

{

Person [property. Name] = property. value;

}

Person ["name"] = "test ";

Person ["TT"] = "AAA ";

Return OBJ;

}

This completes the process of creating objects dynamically and using dynamically created objects between applications, but there are the following problems:

As described in, I think everyone knows, the dynamic type is the compilation during the dynamic runtime, not the static compilation. The attribute information of the dynamic object must be parsed at the runtime, therefore, it is not convenient to use it, so I think of the following methods.

 

After I thought about it, I found that if such a plug-in can be implemented, it would be fine. Of course, the current idea is here. I checked the relevant file about function awareness, however, no APIs can be directly set or controlled between them.

Other feasible ideas

Of course, I only provided several feasible solutions, but not necessarily suitable solutions. Please give me more advice and suggestions. If you have good ideas or implementation solutions, thank you for your reference.

Tutorial.

Follow-up

In the next article, if I have a good idea of implementation, I will provide a complete implementation. Of course, if I have no idea, I will write it here for the time being. Thank you for your brainstorming. Maybe I am just getting a better chance.

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.