Smark. Data Object default value description

Source: Internet
Author: User

During data design, the default value is set for some fields, Smark. data also supports this function. A component describes a value attribute in the attribute to tell the component what value should be provided if this value is not set. The component provides a base class for description. You can extend the default Description Based on your actual needs. The following describes the base class:

 
[Attributeusage (attributetargets. property)] public abstract class valueattribute: attribute {public valueattribute (bool afterupdate) {afterbyupdate = afterupdate;} public bool afterbyupdate {Get; set;} Public Virtual void executing (iconnectincontext CC, object Data, propertymapper pm, string table) {} Public Virtual void executed (iconnectincontext CC, object data, propertymapper pm, string table ){}}

The basic class has two methods: one is to describe the work to be done before the data operation, and the other is to describe the work to be completed after the data operation. Generally, the default value is set before the data operation. Why do I need to define an operation. In fact, in some cases, you do need to do this after the data operation. If MSSQL auto-increment ID is to add and set the relevant ID value to the object, you need to do so after the data operation is completed:

[Attributeusage (attributetargets. property)] public class identity: valueattribute {public identity (): Base (true) {} public override void executed (iconnectincontext CC, object data, propertymapper pm, string table) {command cmd = new command ("select @ identity"); object value = cc. executescalar (CMD); PM. handler. set (data, convert. changetype (value, PM. handler. property. propertytype ));}}

If you want to set a default guid value for an attribute, you can do this for the primary key.

 
[Attributeusage (attributetargets. property)] public class uid: valueattribute {public UID (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {string uid = guid. newguid (). tostring ("N"); PM. handler. set (data, UID );}}

You can reference the UID attribute using the following definitions:

 
[ID] [uid] string employeeid {Get; set ;}

On this basis, many required default values can be implemented. The following is the default value description class implemented inside the component.

[Attributeusage (attributetargets. property)] public class yearmonth: valueattribute {public yearmonth (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, datetime. now. year. tostring ("0000") + datetime. now. month. tostring ("00");} [attributeusage (attributetargets. property)] public class year: valueattribute {public year (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, datetime. now. year. tostring () ;}} [attributeusage (attributetargets. property)] public class month: valueattribute {public month (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, datetime. now. month. tostring () ;}} [attributeusage (attributetargets. property)] public class day: valueattribute {public day (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, datetime. now. day. tostring () ;}} [attributeusage (attributetargets. property)] public class nowdate: valueattribute {public nowdate (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, datetime. now) ;}} [attributeusage (attributetargets. property)] public class defaultint: valueattribute {private int mvalue = 0; Public defaultint (INT value): Base (false) {mvalue = value;} public override void executing (iconnectincontext CC, object Data, propertymapper pm, string table) {PM. handler. set (data, mvalue) ;}[ attributeusage (attributetargets. property)] public class defaultdecimal: valueattribute {private decimal mvalue = 0; Public defadecdecimal (string value): Base (false) {mvalue = convert. todecimal (value);} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, mvalue) ;}[ attributeusage (attributetargets. property)] public class defaultstring: valueattribute {private string mvalue = ""; Public defaultstring (string value): Base (false) {mvalue = value ;} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, mvalue) ;}[ attributeusage (attributetargets. property)] public class defaultdate: valueattribute {private datetime mvalue = datetime. minvalue; Public defaultdate (string value): Base (false) {mvalue = datetime. parse (value);} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, mvalue) ;}[ attributeusage (attributetargets. property)] public class enabled: valueattribute {public enabled (): Base (false) {} public override void executing (iconnectincontext CC, object data, propertymapper pm, string table) {PM. handler. set (data, true );}}

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.