The castle project is very large. activerecord is an O/R Mapping framework suitable for domain model development. It uses. the Reflection Feature of Net does not require configuration files. It integrates nhib.pdf to complete the data layer persistence function and generates data tables based on the created model, which enables us to develop the domain model, you only need to focus on the analysis and definition of Business Object Relationships, greatly reducing the workload.
Development Environment:
Windows Server 2003 enterprise + IIS 6.0
CastleProject-1.0-RC3
Visual Studio 2008
. NET Framework 3.5
Oracle 9i
Code Smith 4.1 (if you have defined a data table, it can be used to generate an object class, but it does not support composite primary keys)
Nunit 2.4.8.0
First let's take a look at the relationship between several classes in the official example MoreComplexSample-vs2003 of activerecord:
The entity class of activerecord must mark [activerecord] on the class name.
[Belongsto] + [hasmany] indicates the one-to-many relationship between two objects.
[Belonsto ("columnname")]Generate columnname columns as foreign keys. An order has only one customer (belongs ).
[Hasmany]No columns are generated. The customer can have multiple orders (has orders ). If the attribute type of belonsto in the class is not the class itself, it must have the corresponding hasmany (or hasandbelongstomany). Otherwise, an error is returned when activerecordstarter is used for initialization.
Shows the relationship between data tables:
Order. CS
[Belonsto ("Customerid")]
PublicCustomer customer
{
Get{ReturnCustomer ;}
Set{Customer=Value ;}
}
Customer. CS
[Haszy (lazy=True)]
PublicIset<Order>Orders
{
Get{ReturnOrders ;}
Set{Orders=Value ;}
}
For category, it uses its own type to define the parent attribute and Mark
[Belonsto] , Code As follows:
[Belonsto ("Parentid")]
PublicCATEGORY parent
{
Get{ReturnParent ;}
Set{Parent=Value ;}
}
A category table with a parent foreign key pointing to its primary key is generated.
[Hasandbelongstomany] indicates many-to-many relationship and generates a relational table.
[Hasandbelongstomany (Table = "tablename", columnkey = "columnname", columnref = "columnname")]
Hasandbelongstomany generates Association tables of two classes based on the specified table name. columnkey and columnref respectively represent the names of the columns that provide foreign keys for the class and relational objects in tablename. In this example, the relationship between category and product is many-to-many:
Category. CS
[Hasandbelongstomany (
Table = " Productcategory " , Columnkey = " Categoryid " , Columnref = " Productid " ,
Inverse = True , Lazy = True )]
Public Iset < Product > Products
{
Get { Return Products ;}
Set {Products = Value ;}
}
Product. CS
[Hasandbelongstomany (
Table = " Productcategory " , Columnkey = " Productid " , Columnref = " Categoryid " , Lazy = True )]
Public Iset < Category > Categories
{
Get { Return Categories ;}
Set {Categories = Value ;}
}
Both objects are marked as hasandbelongstoory and correspond to tables and columns. activerecord generates a relational table named produccategory that contains the productid and categoryid.
If the relational table is also defined as a class, such as lineitem, it is in order. [hasandbelongstomany] has been used in Cs to define the table name. Therefore, it needs to use two belonstashes to represent the order ing between order and product.
Order. CS
[Hasandbelongstomany (
Table = " Lineitem " ,
Columnkey = " Orderid " , Columnref = " Productid " , Inverse = True , Lazy = True )]
Public Iset < Product > Products
{
Get { Return Products ;}
Set {Products = Value ;}
}
Lineitem. CS
[Belonsto ( " Orderid " , Notnull = True , Uniquekey = " Constraintname " )]
Public Order order
{
Get { Return Order ;}
Set {Order = Value ;}
}
[Belonsto ( " Productid " , Notnull = True , Uniquekey = " Constraintname " )]
Public Product
{
Get { Return Product ;}
Set {Product = Value ;}
}
In addition to the relationships used in this example, [onetoone] also represents a one-to-one relationship:
Public ClassProduct
{
[Onetoone]
PublicProductdetail detail {Get;Set;}
}
In the corresponding productdetail class, you need to mark [primarykey (primarykeytype. Foreign)] on the primary key to indicate that its primary key is both a foreign key:
Public ClassProductdetail
{
[Primarykey (primarykeytype. Foreign)]
Public IntProductid {Get;Set;}
}