[Reference] [Castle ar] 2. activerecord

Source: Internet
Author: User
Http://www.rainsts.net/article.asp? Id = 484
Castle activerecord is very flexible in the design of data entities and uses a large number of features, making its code method very similar to the Declarative Programming of WCF.

1. Object Type

We usually choose to inherit the entity type from activerecordbase (or its generic version), which provides almost all the operation methods required by the entity. [Serializable]
Public abstract class activerecordbase <t>: activerecordbase
{
Protected internal static int countall ();
Protected internal static void create (T instance );
Protected internal static void Delete (T instance );
Public static void deleteall ();
Protected static object execute (nhibernatedelegate call, object instance );
Public static bool exists ();
Public static t find (Object ID );
Public static T [] findall ();
Public static T [] findallbyproperty (string property, object value );
Protected internal static t findbyprimarykey (Object ID );
Public static t findfirst (Params icriterion [] criterias );
Public static t findone (Params icriterion [] criterias );
Protected internal static void refresh (T instance );
Protected internal static void save (T instance );
Public static T [] slicedfindall (INT firstresult, int maxresults ...);
Public static t tryfind (Object ID );
Protected internal static void Update (T instance );

...
}

Let's look at a simple example. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private int ID;

[Primarykey (generator = primarykeytype. Identity)]
Public int ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

Public class artester
{
Public static void test ()
{
Activerecordstarter. initialize (assembly. getexecutingassembly (),
New xmlconfigurationsource ("Ar. xml "));

Activerecordstarter. dropschema ();
Activerecordstarter. createschema ();

User user = new user ();
User. Name = "Tom ";
User. Save ();

User user2 = user. Find (user. ID );
Console. writeline (user2.name );
User2.name = "tomxxx ";
User2.update ();

User. Refresh ();
Console. writeline (user. Name );
}
}

Inheriting from activerecordbase is not mandatory. We can use activerecordmediator to achieve the same purpose. [Activerecord ("users")]
Public class user
{
Private int ID;

[Primarykey (generator = primarykeytype. Identity)]
Public int ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

Public class artester
{
Public static void test ()
{
Activerecordstarter. initialize (assembly. getexecutingassembly (),
New xmlconfigurationsource ("Ar. xml "));

Activerecordstarter. dropschema ();
Activerecordstarter. createschema ();

User user = new user ();
User. Name = "Tom ";
Activerecordmediator <user>. Save (User );

User user2 = activerecordmediator <user>. findbyprimarykey (user. ID );
Console. writeline (user2.name );
User2.name = "tomxxx ";
Activerecordmediator <user>. Update (user2 );

Activerecordmediator <user>. Refresh (User );
Console. writeline (user. Name );
}
}

In addition to activerecordbase, ar also provides activerecordvalidationbase. It allows us to use the corresponding features to verify the format of entity members. The specific usage will be studied in the next chapter.

2. ing features

Ar provides a large number of features to replace the Nhibernate configuration file, which is a common object ing feature.

Activerecordattribute

It is mainly used to specify the ing between object types and data tables. We can set the data table name by constructing parameters or table attributes. It also has a large number of other attributes used to manipulate the entity's cache policies, inheritance policies, and so on. For more information, see the Help file. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
}

Primarykeyattribute

Used to specify the primary key of a data table, including the auto-increment field. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private int ID;

[Primarykey (primarykeytype. Identity)]
Public int ID
{
Get {return ID ;}
Set {id = value ;}
}
}

You can also directly use a string field as the primary key. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private string name;

[Primarykey (primarykeytype. Assigned)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

However, this does not seem to be a good idea. I personally suggest using an ID field is better. Of course, we can not use the auto-increment type. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private string ID;

[Primarykey (primarykeytype. uuidhex)]
Public String ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

Propertyattribute/fieldattribute

These two features are used to specify the ing between object type attributes and fields and data table fields. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private string ID;

[Primarykey (primarykeytype. uuidhex)]
Public String ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}

Private int age;

[Property (column = "age1")]
Public int age
{
Get {return age ;}
Set {age = value ;}
}
}

Fieldattribute is used in a similar way. The member attributes also include whether to allow null (notnull), check, and formula. The most interesting thing is insert & update, which allows us to ignore this attribute or field when executing insert and update actions. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private string ID;

[Primarykey (primarykeytype. uuidhex)]
Public String ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}

Private datetime createdatetime;

[Property (update = false)]
Public datetime createdatetime
{
Get {return createdatetime ;}
Set {createdatetime = value ;}
}
}

Public class artester
{
Public static void test ()
{
Activerecordstarter. initialize (assembly. getexecutingassembly (),
New xmlconfigurationsource ("Ar. xml "));

Activerecordstarter. dropschema ();
Activerecordstarter. createschema ();

User user = new user ();
User. Name = "Tom ";
User. createdatetime = datetime. now;
User. Save ();

Console. writeline (user. createdatetime );
System. Threading. thread. Sleep (2000 );

User user2 = user. Find (user. ID );
User2.name = "ABC ";
User2.createdatetime = datetime. Now. adddays (100 );
User2.update ();

User. Refresh ();
Console. writeline (user. Name );
Console. writeline (user. createdatetime );
}
}

Output:
2007-5-8 12:12:46
ABC
2007-5-8 12:12:46

This is very practical.

Compositekeyattribute/keypropertyattribute

Defines the key combinations in a data table. Note that the key combination type must be serializable and the equals and gethashcode methods must be overwritten. [Serializable]
Public class ID
{
Private int X;

[Keyproperty]
Public int x
{
Get {return X ;}
Set {x = value ;}
}

Private int y;

[Keyproperty]
Public int y
{
Get {return y ;}
Set {Y = value ;}
}

Public override bool equals (Object OBJ)
{
Return base. Equals (OBJ );
}

Public override int gethashcode ()
{
Return base. gethashcode ();
}
}

[Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private ID;

[Compositekey]
Public ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

Public class artester
{
Public static void test ()
{
Activerecordstarter. initialize (assembly. getexecutingassembly (),
New xmlconfigurationsource ("Ar. xml "));

Activerecordstarter. dropschema ();
Activerecordstarter. createschema ();

User user = new user ();
User. ID = new ID ();
User. Id. x = 1;
User. Id. Y = 2;
User. Name = "Zs ";
User. Create (); // an error occurs when you call save .????
}
}

Nestedattribute

Nestedattribute is used to mark attributes of custom types. Public class postaladdress
{
Private string country;

[Property]
Public String country
{
Get {return country ;}
Set {Country = value ;}
}

Private string address;

[Property]
Public String address
{
Get {return address ;}
Set {address = value ;}
}

Private string postcode;

[Property]
Public String postcode
{
Get {return postcode ;}
Set {postcode = value ;}
}
}

[Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private int ID;

[Primarykey (primarykeytype. Identity)]
Public int ID
{
Get {return ID ;}
Set {id = value ;}
}

Private string name;

[Property (unique = true, notnull = true)]
Public string name
{
Get {return name ;}
Set {name = value ;}
}

Private postaladdress address;

[Nested]
Public postaladdress address
{
Get {return address ;}
Set {address = value ;}
}
}

Public class artester
{
Public static void test ()
{
Activerecordstarter. initialize (assembly. getexecutingassembly (),
New xmlconfigurationsource ("Ar. xml "));

Activerecordstarter. dropschema ();
Activerecordstarter. createschema ();

User user = new user ();
User. Name = "zhagnsan ";
User. Address = new postaladdress ();
User. Address. Country = "China ";
User. Address. Address = "Beijing ...";
User. Address. postcode = "123456 ";

User. Save ();

User user2 = user. Find (user. ID );
Console. writeline (user2.address. Address );
}
}

------------------------ Note --------------------------------

1. If a property must be read-only, we must specify its access. [Activerecord ("users")]
Public class user: activerecordbase <user>
{
Private int ID;

[Primarykey (primarykeytype. Identity, access = propertyaccess. fieldcamelcase)]
Public int ID
{
Get {return ID ;}
}
}

2. If you want to use big data, such as a string with a length greater than 255, or byte [], we 'd better use "stringclob" or "binaryblob ". [Activerecord]
Public class data: activerecordbase <DATA>
{
Private int ID;

[Primarykey (primarykeytype. Identity, access = propertyaccess. fieldcamelcase)]
Public int ID
{
Get {return ID ;}
}

Private string content;

[Property (columntype = "stringclob", length = 2048)]
Public String content
{
Get {return content ;}
Set {content = value ;}
}
} [Last modified by yujia, 15:39:03]

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.