Relation Object Lazy-load

In NObject O/R Mapping persistence layer, relation object, including parent object and sub object-set, is dealt with in lazy-load method in order to improve performance:

l    Lazy-load of relation parent object is as follows:

Order o = Order.GetByOrderID(om, 10252);

// ...

// o.Customer have not been loaded

// ...

Customer c = o.Customer; // o.Customer is loaded here

// ...

l    Lazy-load of relation sub object-set is as follows:

Order o = Order.GetByOrderID(om, 10252);

// ...

// o.OrderDetailSet have not been loaded

// ...

ObjectSet details = o.OrderDetailSet; // o.OrderDetailSet is loaded here

foreach (OrderDetail detail in details)

{

  // ...

}

Using cache in NObject O/R Mapping persistence layer: after lazy-loading relation-object, if relational parent-object or object in relational object-set has already been updated, then the relation object or the relation object-set here will update automatically, which is transparent to developers and needn't to deal with.

l    Update relational parent object automatically:

Order o = Order.GetByOrderID(om, 10252);

Customer c = o.Customer;

// ...

PrintObject(c);

 

Customer c2 = Customer.GetByCustomerID(om, c.CustomerID);

c2.CompanyName = "CompanyName Updated...";

// ...

PrintObject(c);

--output result--

==== Customer ====

Address = Boulevard Tirou, 255

City = Charleroi

CompanyName = Suprêmes délices

ContactName = Pascale Cartrain

ContactTitle = Accounting Manager

Country = Belgium

CustomerID = SUPRD

Fax = (071) 23 67 22 21

Phone = (071) 23 67 22 20

PostalCode = B-6000

Region =

==== Customer ====

Address = Boulevard Tirou, 255

City = Charleroi

CompanyName = CompanyName Updated...

ContactName = Pascale Cartrain

ContactTitle = Accounting Manager

Country = Belgium

CustomerID = SUPRD

Fax = (071) 23 67 22 21

Phone = (071) 23 67 22 20

PostalCode = B-6000

Region =

l    Update relational sub object-set automatically:

Order o = Order.GetByOrderID(om, 10252);

Console.WriteLine(o.OrderDetailSet.Count);

 

OrderDetail od = new OrderDetail(om);

od.OrderID = o.OrderID;

od.Save();

 

Console.WriteLine(o.OrderDetailSet.Count);

--output result--

3

4

Related Topics

Lazy-load and Caching