I downloaded the ASP.NET 2.0 PayPal Commerce Starter Kit (CSK) today for kicks and ended up creating a new provider, called InventoryProvider,
that provides the basis for a more functional and feature-rich
inventory management system. I say "provides the basis," because
my immediate intentions were to do the minimal amount of work possible
to have CSK use the provider and then add new features in increments
later.
AmountOnHand
Currently, CSK fulfills its inventory management via an AmountOnHand field in the Products Table.

This is fine for a starter kit and simple needs, but I thought
I would provide the foundation for extending these needs a bit
further using what exists today.
InventoryProvider
CSK uses the Provider Model to abstract the
functionality needed by the website from the actual concrete classes
that provide it. It has providers to handle the product catalog,
shopping cart, orders, tax calculations, etc. I followed this
model by creating an InventoryProvider to provide functionality and features associated with Inventory Management.

Product Detail Page and Inventory Manager
One of the places where CSK uses the AmountOnHand field associated with a product is on the Product Detail Page where it decides if the Add To Cart Link and Out of Stock Label should be displayed. I no longer want to access that information directly, but have the page call the InventoryManager which is responsible for handling Inventory Management needs.

The InventoryManager sits right with the other managers used by CSK.

Here is a stripped down version of the InventoryManager showing you the functionality in question. I am following the same style as the other manager classes in CSK:
public class InventoryManager
{
/// <summary>
/// Get product quantity on hand
/// </summary>
/// <param name="productID"></param>
/// <returns></returns>
public static int GetAmountOnHand(int productID)
{
return Commerce.Providers.
InventoryProvider.Instance.GetQtyInStock(productID);
}
}
Simplest Thing Possible - ReUse Existing Functionality in CSK
Although my needs in the future will require a bit more
functionality, I don't want to be bothered with that at this
point. I just need to get InventoryManager integrated into the UI to abstract the idea of AmountOnHand from Product while still using the existing functionality. Hence, I just stole the code from the SqlCatalogProvider to load a product and return the value in the AmountOnHand Field.
I contributed about 4 lines of code below, which is the GetQtyInStock method implemented in SqlInventoryProvider. Note that the code is far from optimized and just serves the near term. As you can see, CSK uses the Microsoft Data Access Application Block for its data access needs.
public override int GetQtyInStock(int productID)
{
int qtyInStock = 0;
SqlDataReader rdr = null;
//Define the parameters
SqlParameter[] paramArray = new SqlParameter[]
{
new SqlParameter("@ProductID", SqlDbType.Int,
0, ParameterDirection.Input, true, 10, 0,
null, DataRowVersion.Current, productID),
};
try
{
//Fill the dataset using the connection
// string from the db base class
rdr = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader
(_connectionString, CommandType.StoredProcedure, "CMRC_PRODUCTS_Get",
paramArray);
if (rdr.Read())
{
int ordinal = rdr.GetOrdinal("AmountOnHand");
qtyInStock = rdr.GetInt32(ordinal);
}
}
catch (SqlException x)
{
string sException = "Error Executing CMRC_PRODUCTS_Get: \r\n";
foreach (SqlParameter p in paramArray)
{
sException += p.ParameterName + "=" + p.Value + "\r\n";
}
throw new Exception(sException, x);
}
return qtyInStock;
}
Web.Config Provider Stuff
With all Provider Model implementations, you end up putting information into Web.Config
to create this pluggable environment. I have no other needs than
the basics. I used the current providers in CSK as a template for
the InventoryProvider:
<configSections>
<section name="CommerceInventoryProvider"
type="Commerce.Providers.InventoryProviderConfigurationHandler,
Commerce.InventoryProvider"/>
</configSections>
<CommerceInventoryProvider defaultProvider="SqlInventoryProvider">
<providers>
<clear/>
<add name="SqlInventoryProvider"
type="Commerce.Providers.SqlInventoryProvider,
Commerce.InventoryProvider"
connectionStringName="CommerceTemplate" />
</providers>
</CommerceInventoryProvider>
Conclusion
Hopefully this sheds some light on how easy it is to extend the ASP.NET 2.0 PayPal Commerce Starter Kit. The InventoryProvider mentioned in this article can be extended to provide quite a bit of additional functionality than the current implementation.
Drinking: Sencha Green Tea
Posted
11-11-2005 5:05 PM
by
David Hayden