Before reading this article, please go through the following articles:
- Object Relational Mapping (ORM) Using NHibernate - Part 1 of 8
- Object Relational Mapping (ORM) Using NHibernate - Part 2 of 8
- Object Relational Mapping (ORM) Using NHibernate - Part 3 of 8
- Object Relational Mapping (ORM) using NHibernate - Part 4 of 8
- Object Relational Mapping (ORM) Using NHibernate - Part 5-A of 8
- Object Relational Mapping (ORM) Using NHibernate: Part 5-B of 8
- Object Relational Mapping (ORM) Using NHibernate - Part 6 of 8
Completing the E-commerce Example
In articles 1-6, we have finished the various types of associations between entity classes and inheritance between entity classes. The example to illustrate these ideas had been the sample ECommerce scenarios. We have reached a point where shipping must be done for the product. In this seventh article and its client code, we will demonstrate that the classes we have put forth to incorporate the shipping functionality will allow shipment items of an order to be shipped in parts by multiple shipping services. This is a flexibility that provides business advantages. Also possible is the case that multiple orders belonging to the same customer can be combined into one shipment. All this is possible because we have modeled the relationship between entities Shipping and PaymentApprovedOrder as Many-To-Many association with the OrderShipment as an association class. This article and the client code will also demonstrate that the way in which we add ShipmentItem, i.e. the item to shipped for a paid order, will capture the material movement in the ecommerce domain.
Background
Covered in Articles 1 - 6.
NOTE: The two projects included are ECommerce1 and ECommerce 2. ECommerce1 is for samples in articles 1 - 3 and Ecommerce 2 is for the rest of the samples until this article 7. The client project to test the classes and their associations for each article is also provided and each article's testcode is clearly named like "Test Code for Article 1" etc. While starting a subsequent article, the previous article's test code had been commented out in the test client project and the classes in the main project would have evolved and changed to fit the subsequent scenarios of the next article. The commented out previous article's test code has not been updated to reflect changes to the class. So the test code will compile and work correctly for the most recent article for each project i.e. article 3 for ECommerce1 and Article 7 & 6 for Ecommerce 2 which completes our ECommerce samples. Follow the instructions given earlier for using the projects with Microsoft Visual Studio 2012, though we all know how to get it done without any help.
Code Sample For Ecommerce Scenario
Here we will briefly explain and complete the ShipmentItem scenario and demonstrate the points articulated in the Introduction. The scenario is, once a customer gives the paid order to the ecommerce site and has selected his preferred method of shipping, the Ecommerce Site Shipping Department will process the order and ship all the items in the paid order to the customer.
So, when a paid order is created by the customer, the customer selects fast shipping or parcel service according to which service, FastShipping or ParcelService, was specified and stored in the base class reference "Shipping". This Shipping reference stores the paid order id to which it is attached in the PrimaryPaidOrderId property and is persisted to the database. Not a foreignkey but just a property (because Shipping and PaymentApprovedOrder are bound by association to the class OrderShipment). When the Shipping information must be input by personnel of the Shipping Department, they input this primarypaidorderid (PRIMARYPAIDORDERID) to which shipping has to be done to get a particular shipping instance. How do they know this PRIMARYPAIDORDERID? This is the the paid order id (i.e. id of a new order - PAYMENTAPPROVEDORDERID) for which a shipping has not been done which is available for the application to retrieve and show in a listbox from which the shipping department can select one. So using this, the particular shipping instance for that primary paid order id, stored in the repository while an order is created by a customer, is retrieved from the repository and shipping information is added to it and persisted again into the repository. If in case the shipping department decides to do the Shipping in parts i.e in more than one shipment for a particular order, then for subsequent parts they do not need to add the primary paid order id. They can just create new instances of shipping giving all the shipping information. The OrderShipment instance will associate Shipping and PaymentApprovedOrder anyway. This design will provide maximum flexibility to the business scenario.
The code in ECommerceSellerSystem for adding the Shipping information is:
public Shipping AddShippingInformation(
bool is_primary,
bool is_fast,
long prime_id,
string shipping_name,
DateTime shipping_start_date,
string tracking_number,
bool is_gift_wrapped)
{
Shipping shipping = null;
IRepository<Shipping> shipping_repo = new DBRepository<Shipping>();
NHibernate.ISession session = null;
NHibernateHelper nh = NHibernateHelper.Create();
session = nh.Session;
if (is_primary)
{
string query_string = "from Shipping shipping1 where shipping1.PrimaryPaidOrderId = :prime_id";
shipping = session.CreateQuery(query_string)
.SetParameter<long>("prime_id", prime_id)
.UniqueResult<Shipping>();
if (shipping != null)
{
shipping.ShippingName = shipping_name;
shipping.ShippingStartDate = DateTime.Today;
if (shipping is FastShipping)
{
((FastShipping)shipping).TrackingNumber = tracking_number;
}
}
}
else
{
if (is_fast)
{
shipping = new FastShipping
{
ShippingName = shipping_name,
ShippingStartDate = DateTime.Today,
PrimaryPaidOrderId = null,
TrackingNumber = tracking_number
};
}
else
{
shipping = new ParcelService
{
ShippingName = shipping_name,
ShippingStartDate = DateTime.Today,
PrimaryPaidOrderId = null,
IsGiftWrapped = is_gift_wrapped
};
}
}
if (shipping != null)
{
using (NHibernate.ITransaction trans = session.BeginTransaction())
{
session.SaveOrUpdate(shipping);
trans.Commit();
}
}
return shipping;
}

Gowtham RajamanickamPosted Mar 28, 2015, 12:30 PM
good