Examples of OOP in practice ??
I am trying to get to grips with OOP using vb/asp.net and was wondering if anyone knows where I can get a hold of a simple project which makes use of the following :
Has some kind of dataclass which handles all database operations.
Some kind of business class which inherits from the above.
All it has to do is maybe write a record and read/search/update a record. I dont need some giant solution, just a simple example so I can pick up some of the required techniques.
I am sick of writing hacky code and want to be a code warrior hero.
:wave:
p.s - I Luv U :blush:
a class using those classes
Code:
/// <summary>
/// PK Promotions base product/widget/thing-ah-mah-jig.
/// </summary>
public class Product : Base.SqlObject
{
#region Constructors
/// <summary>
/// Create a new empty Product. This product will not exist in SQL Server until saved.
/// </summary>
public Product(){}
public Product(System.Guid key)
{
if(key != System.Guid.Empty)
{
System.Data.SqlClient.SqlDataReader r = Helpers.SqlProcedure.ToDataReader
("Products_GetProductByProductGuid",
new System.Data.SqlClient.SqlParameter("@ProductGuid", key),
ConnectionString);
if(r.Read())
{
_key = key;
_name = r["ProductName"].ToString().Trim();
_description = r["ProductDescription"].ToString().Trim();
_phase = new ProductPhase(new System.Guid(r["ProductPhaseGuid"].ToString().Trim()));
}
r.Close();
}
}
#endregion
#region Data Members
/// <summary>
/// This method deletes this product and all it's related data from SQL Server.
/// Note: This method will cascade the deletion of all related product information.
/// Information will be delted across all tables on which ProductGuid has
/// a PRIMARY KEY relation. Don't call it unless you mean it.
/// </summary>
public override void Delete()
{
if(_key == System.Guid.Empty){return;}
Helpers.SqlProcedure.ExecuteNonQuery
("Products_DelProductByProductGuid",
new System.Data.SqlClient.SqlParameter("@ProductGuid", _key),
ConnectionString);
}
/// <summary>
/// Save the information for this product to SQLServer.
/// Note: This method does not alter any tables
/// except the Products table.
/// </summary>
public override void Save()
{
if(_phase.Key == System.Guid.Empty)
{//set idle
}
if(_key != System.Guid.Empty)
{ //update
Helpers.SqlProcedure.ExecuteNonQuery
("Products_SetProductByProductGuid",
new System.Data.SqlClient.SqlParameter[]{
new System.Data.SqlClient.SqlParameter("@ProductGuid", _key),
new System.Data.SqlClient.SqlParameter("@ProductPhaseGuid", _phase.Key),
new System.Data.SqlClient.SqlParameter("@ProductName", _name),
new System.Data.SqlClient.SqlParameter("@ProductDescription", _description)},
ConnectionString);
}
else
{ //insert
Helpers.SqlProcedure.ExecuteNonQuery
("Products_AddProduct",
new System.Data.SqlClient.SqlParameter[]{
new System.Data.SqlClient.SqlParameter("@ProductPhaseGuid", _phase.Key),
new System.Data.SqlClient.SqlParameter("@ProductName", _name),
new System.Data.SqlClient.SqlParameter("@ProductDescription", _description)},
ConnectionString);
}
}
#endregion
#region Properties
private System.Guid _key = System.Guid.Empty;
/// <summary>
/// Products read only PRIMARY KEY.
/// This Guid Primary Key is also used as this products
/// Product Code.
/// </summary>
public System.Guid Key
{
get{return _key;}
}
private ProductPhase _phase = new ProductPhase();
/// <summary>
/// The phase the product is currently in.
/// </summary>
public ProductPhase Phase
{
get{return _phase;}
set{_phase = value;}
}
/// <summary>
/// The read only description of the current phase.
/// </summary>
public string PhaseDescription
{
get{return _phase.Description;}
}
private string _name = "";
/// <summary>
/// The name used to identifiy this product.
/// </summary>
public string Name
{
get{return _name;}
set{_name = value;}
}
private string _description = "";
/// <summary>
/// A brief description that goes along with the product.
/// </summary>
public string Description
{
get{return _description;}
set{_description = value;}
}
#endregion
#region Static Members
/// <summary>
/// Get the total number of products.
/// </summary>
/// <returns>Integer number of products.</returns>
public static int ProductCount()
{
return (int)Helpers.SqlProcedure.ExecuteScalar
("Products_GetProductsCount",
Base.SqlObject.ConnSTR());
}
/// <summary>
/// Get the total number of products in a certian phase.
/// </summary>
/// <param name="phase_key">The Guid PRIMARY KEY of the product count from.</param>
/// <returns>The total number of products in the product phase determined by phase_key.</returns>
public static int ProductCount(System.Guid phase_key)
{
return (int)Helpers.SqlProcedure.ExecuteScalar
("Products_GetProductsCountByProductPhaseGuid",
new System.Data.SqlClient.SqlParameter("@ProductPhaseGuid", phase_key),
Base.SqlObject.ConnSTR());
}
/// <summary>
/// Get a Product by name.
/// </summary>
/// <param name="name">The name of the product.</param>
/// <returns>Product.</returns>
public static Product FromName(string name)
{
return new Product(new System.Guid(Helpers.SqlProcedure.ExecuteScalar
("Products_GetProductByProductName",
new System.Data.SqlClient.SqlParameter("@ProductName", name),
Base.SqlObject.ConnSTR()).ToString()));
}
#endregion
}