Quantcast
Channel: objectdatablocks Wiki & Documentation Rss Feed
Viewing all articles
Browse latest Browse all 20

Updated Wiki: Advanced Features

$
0
0

Advanced Features

Object Data Blocks supports inheritance through the specialisation of existing persisted classes. The following two classes inherit all the properties of the Person class. Abstract classes are combined into a single table whilst non-abstract classes are joined together transparently.

publicclass Customer: Person
{
   [Column, SqlFixed(9,2)]
   publicdecimal CreditLimit() {get; set;}
}

publicclass Employee: Person
{
   [Column]
   publicstring JobDescription {get; set;}
}

Common database features such as key identity creation, indexes and referential integrity can be added through the use of attributes. In this example we create a primary key with an auto-generated value and we specify the maximum length of the string data in the Line1 column. The Address type is used for the ShippingAddress so that these fields are always available when retrieving an order. Enumerations are also permitted as a column type.

publicenum AddressType
{
   Shipping = 0,
   Billing = 1
}

publicclass Address: Table
{
   [PrimaryKey, SqlIdentity(1,1)]
   publicint Id {get; set;}

   [Column, SqlLength(255)]
   publicstring Line1 {get; set;}

   [Column]
   public AddressType Type {get;set;}
}

publicclass Order
{
   [PrimaryKey, SqlIdentity(1,1)]
   publicint Id {get; set;}

   [Column]
   public Address ShippingAddress {get; set;}

   [UniqueIndex]
   publicstring Reference {get; set;}
}

Tables level attributes can also be added to specify compound keys and indexes , and other table-level information. In the next example, an association is created between customers and address's and a primary key is specified that is a combination of a valid Customer Id and Address Id.

[PrimaryKeys("CustomerId","AddressId")]
publicclass CustomerAddress
{
   [ForeignKey(typeof(Customer)]
   publicint CustomerId {get; set;}

   [ForeignKey(typeof(Address)]
   publicint AddressId {get; set;}
}

All customers and their address's can be retrieved using the Query<T> class. The following example prints address labels for each customer. Each customer must start on a new page.

var customerQuery = new Query<Customer>();
var customerAddressQuery = new Query<CustomerAddress>();
var addressQuery = new Query<Address>();

customerQuery.Join(customerAddressQuery, "Id", "CustomerId");
customerAddressQuery.Join(addressQuery,"AddressId", "Id");

using (QueryDataAdapter adapter = customerQuery.Execute(command))
{
   while (adapter.Read())
   {
      if (customerQuery.Binder.HasChanged) 
      {
         Customer customer = customerQuery.Item;
         PrintNewCustomer(customer)
      }
      PrintAddressLabel(addressQuery.Item);
   }
}

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>