The Battle of strength

Source: Internet
Author: User

There are a number of problems that often occur during programming:

Scenario: Domain entity A has attributes (B,c,d,e), then the class of the entity a that we build has the attribute b,c,d,e. However, for different roles a may not behave the same way, it may be necessary to finance the B,c,d,e,f attribute (f is the computed attribute) in order to b,c in production. The D,e,g property (g is the XX attribute), which appears to require the B,c,d,e,h attribute (h is the xx attribute).

We often need to create class A1 (B,c,d,e,f), A2 (B,c,d,e,g), A3 (B,c,d,e,h), and a1,a2,a3 inherit from Class A.

There is a kind of demand, sometimes we just need a property b,c or b,d, equal to a projection of the properties of a.

The solutions are:

1. Build the view and build the entity corresponding to it.

The 2.A entity is converted to A1,A2,A3, and the class is converted to AT1 (b,c), AT2 (b,d) for the projection entity a.

3.A entities converted to anonymous entity classes with LINQ

Regardless of how it is handled, there must be an entity conversion problem (cyclic assignment, a1,a2,a3 can be as a, but a cannot be converted directly to A1,A2,A3, the projection entity is more impossible to convert between them), and the entity type increases.

At the database access level to be converted to an entity type, either write the conversion method yourself, or take advantage of the reflection dynamic turn.

Why should I recommend http:smartobject.codeplex.com? The advantages are obvious!

The properties of 1.SmartObject are very easy to extend, and the direct dynamic D=new SmartObject ();d. Property =value or D["Properties"]=value adds an attribute

2. JSON serialization (D.tojson (), use of smartjosnserializer.serialize (list) for list<smartobject>, and deserialization (smartobject.parse ("JSON string") a single type or Smartjosnserializer.deserialize ("JSON string") to deserialize a list type is supported well

3. Provides properties and index access methods (Json.NET deserialization to Jobject can only be obj[prop]. Toobject<t> (), this step obviously requires an extra overhead operation, so the access style is not elegant.

4. The type after deserialization is accurate (you will find that the type is imprecise after json.net deserialization)

5.datatable,idatareader conversion to a list object has a general fast conversion method that can be implemented simply:

 Public Staticilist<Dynamic> ToList ( ThisDataTable DataTable) {List<Dynamic> list =Newlist<Dynamic>(); if(DataTable. Rows! =NULL) && (DataTable. Rows.Count >0) {datacolumncollection columns=DataTable.                Columns;  for(inti =0; I < DataTable. Rows.Count; i++)                {                    DynamicObj2 =NewSmartObject (); DataRow Row=DataTable.                    Rows[i];  for(intj =0; J < Columns. Count; J + +)                    {                        stringColumnName =Columns[j].                        ColumnName; ObjectObj3 =Row[j]; Obj2[columnname]= (Obj3 = = DBNull.Value)?NULL: obj3; } list.                ADD (OBJ2); }            }            returnlist; }         Public Staticilist<Dynamic> ToList ( ThisIDataReader Reader) {List<Dynamic> list =Newlist<Dynamic>(); if(!Reader. IsClosed) {using(reader) { while(reader. Read ()) {DynamicObj2 =NewSmartObject ();  for(inti =0; I < reader. FieldCount; i++)                        {                            stringName =Reader.                            GetName (i); ObjectObj3 =Reader.                            GetValue (i); Obj2[name]= (Obj3 = = DBNull.Value)?NULL: obj3; } list.                    ADD (OBJ2); }                }            }            returnlist; }         Public Staticilist<Dynamic> ToList ( ThisDataTable DataTable, Func<datarow,Dynamic>parser) {List<Object> list =Newlist<Object>(); if((DataTable. Rows! =NULL) && (DataTable. Rows.Count >0)) && (Parser! =NULL) {datacolumncollection columns=DataTable.                Columns;  for(inti =0; I < DataTable. Rows.Count; i++) {list. ADD (Parser (DataTable).                Rows[i])); }            }            returnlist; }         Public Staticilist<Dynamic> ToList ( ThisIDataReader Reader, Func<idatareader,Dynamic>parser) {List<Dynamic> list =Newlist<Dynamic>(); if(!reader. IsClosed && (Parser! =NULL))            {                using(reader) { while(reader. Read ()) {list.                    ADD (parser (reader)); }                }            }            returnlist; }    }

SmartObject is not suitable for scenes with high performance requirements, and dynamic types cannot enjoy smart hints.

Contention of strength and weakness

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.