Front desk if only one or two data, in the background request to get the data set property value can also be accepted, but if a lot of data is passed, you have to HttpRequest reques["XXX" request to get the data set property value, the input code is very large, and is very time-consuming, This time-consuming and laborious approach is difficult to accept. Here's a trickery way to get the request data to be simple and save time. Words not much to say, directly on the code:
The C # code is as follows:
/// <summary> /// Set object Properties--reflection entity set Variable /// </summary> /// <param name= "obj" > Object </param>
<param name= "Set" > Set Property Actions </param>
protected voidSet_property (ObjectObj,del_propertySet) { stringparam =string. Empty,type; Propertyinfo[] Propertys=obj. GetType (). GetProperties (); foreach(PropertyInfo Pinchpropertys) { //GET Request Parameters Object[] columns = P.getcustomattributes (typeof(Jsonpropertyattribute),true); if(Columns. Length >0){ //Setting property valuesparam = request[(columns[0] asJsonpropertyattribute). PropertyName]; if(param! =NULL) {type=p.propertytype.tostring (); Type= Regex.match (type, type. IndexOf ("[") >-1?@"(? <=\[\w+\.) [^\]] +":@"(? <=\.) \w+"). Value; Switch(type) { Case "Boolean": P.setvalue (obj, Boolean.Parse (param),NULL); Break; Case "Byte": P.setvalue (obj, Byte.parse (param),NULL); Break; Case "Decimal": P.setvalue (obj, Decimal.Parse (param),NULL); Break; Case "DateTime": P.setvalue (obj, DateTime.Parse (param),NULL); Break; Case "Int16": P.setvalue (obj, Int16.parse (param),NULL); Break; Case "Int32": P.setvalue (obj, Int32.Parse (param),NULL); Break; Case "Int64": P.setvalue (obj, Int64.parse (param),NULL); Break; Case " Single": P.setvalue (obj, Single.parse (param),NULL); Break; Case "Entity": ; Break; default: P.setvalue (obj, param,NULL); Break; } } } } if(Set!=NULL)Set(obj); }
Example:
usingSystem;usingSystem.Data;usingSystem.Collections.Generic;usingDataobject.attrib;usingNewtonsoft.json;
// some entities namespacetiaoceng.entity{/// <summary> ///Order/// </summary>[DBTable (Name ="[Orders]", primarykey="or_id")] Public classOrdersmodel:dataobject.datafactory.datamodule<ordersmodel> { /// <summary> ///Order Constructor/// </summary> PublicOrdersmodel ():Base() { } /// <summary> ///Order Constructor/// </summary> /// <param name= "constring" >Database connection String</param> PublicOrdersmodel (stringconstring):Base(constring) {}/// <summary> ///Order ID/// </summary>[DbColumn (Name ="or_id", Size =4, DbType = SqlDbType.Int, PrimaryKey =true)] [Jsonproperty (PropertyName="or_id")] Public int? or_id {Get;Set;} /// <summary> ///The Purchaser ID, which is 0 as a non-user ID/// </summary>[DbColumn (Name ="m_id", Size =4, DbType =SqlDbType.Int)] [Jsonproperty (PropertyName="member")] PublicMembermodel Member {Get;Set; }/// <summary> ///Buyer Contact Number/// </summary>[DbColumn (Name ="Or_buyertel", Size = -, DbType =SqlDbType.VarChar)] [Jsonproperty (PropertyName="Or_buyertel")] Public stringOr_buyertel {Get;Set;} /// <summary> ///Buyer's address/// </summary>[DbColumn (Name ="or_buyeraddress", Size = $, DbType =SqlDbType.NVarChar)] [Jsonproperty (PropertyName="or_buyeraddress")] Public stringor_buyeraddress {Get;Set;}/// <summary> ///Paid Amount/// </summary>[DbColumn (Name ="Or_payprice", Size =9, DbType =sqldbtype.decimal)] [Jsonproperty (PropertyName="Or_payprice")] Public decimal? Or_payprice {Get;Set;}/// <summary> ///order status (0 shopping cart, 1 pending verification, 2 pending payment, 3 payment successful, 4 shipped, 5 confirmation of receipt, 6 request for refund, 7 refund success, 8 refund failure)/// </summary>[DbColumn (Name ="or_progressstate", Size =4, DbType =SqlDbType.Int)] [Jsonproperty (PropertyName="or_progressstate")] Public int? or_progressstate {Get;Set;} /// <summary> ///Order Progress Description/// </summary>[DbColumn (Name ="or_progress", Size = +, DbType =SqlDbType.NVarChar)] [Jsonproperty (PropertyName="or_progress")] Public stringor_progress {Get;Set;}/// <summary> ///status (0 pending review, 1 Audit pass, 2 audit failure, 3 Recycle Bin, 4. void)/// </summary>[DbColumn (Name ="or_state", Size =1, DbType =sqldbtype.tinyint)] [Jsonproperty (PropertyName="or_state")] Public byte? or_state {Get;Set; }
....}
// entity Object
Ordersmodel _order = new Ordersmodel ();
//set up order informationSet_property (_order, (obj) ={_order. Member=NewMembermodel () {m_id =m_id}; if(!string. IsNullOrEmpty (request["PayDate"])) _order. Or_paydate = DateTime.Parse (request["PayDate"] +"00:00:00"); if(!string. IsNullOrEmpty (request["shipdate"])) _order. Or_shipdate = DateTime.Parse (request["shipdate"] +"00:00:00"); _order. Or_price=_price; _order. Or_amount=_num; _order. Or_getintegral=_getintegral;});
In this way, no matter how much data the foreground passes, the background only need to pass the entity object in to save time and effort to complete the operation of setting property values, of course, the request parameters of the foreground should be consistent with the extended attribute name of the entity. is not very convenient, come and try it. Is there any good way or suggestion can contact me, we discuss together.
Sina Weibo (seeking attention) address:
Http://weibo.com/zhengdjin
C # Setting object Properties--reflection entity setting variables