CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

Extending the ASP.NET 2.0 PayPal Commerce Starter Kit - InventoryProvider for Inventory Management

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.

 

DrinkingSencha Green Tea

 


Published Nov 11 2005, 05:05 PM by David Hayden
Filed under: ,

Comments

Christopher Steen said:

Atlas Video Tutorial
[Via: Dan Wahlin ]
Code Snippet for Properties with Prefix
Notation [Via:...
# November 14, 2005 10:14 AM

David Hayden said:

# November 15, 2005 9:38 AM

Rob Conery said:

David - thanks for this post! You have hit exactly on what I hope the community at large is seeing with this kit- how extensible the Provider Model and ASP.NET 2.0 can be.

Can I post this up to the forums (http://forums.commercestarterkit.rog) so others may use it?

Regards,
Rob (spookytooth)
# November 15, 2005 8:20 PM

][ Rob Conery Ramblings ][ said:

# November 15, 2005 8:47 PM

Rob Conery said:

I also wanted to mention that we just released the VB.NET source for the kit today. The site runs really well (though I swear C# seems to run faster :p ) and all the tests passed however I don't know VB.NET as well as I should so I am putting this out as beta for now.

Cheers,
Rob
# November 15, 2005 10:40 PM

David Hayden said:

Hey Rob,

Great work with the new starter kit and companion website!

You can post any of the code or information in my post to the forum.
# November 16, 2005 1:35 PM

Rob.Parse(Rob) said:

ASP.NET 2.0 Commerce Starter Kit InventoryProvider

# February 8, 2007 5:20 PM
Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home