 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 26th, 2005, 04:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Force Derived Method
Hi All,
Im looking for some assistance with my OO design.
I have a base class for accessing data that all of my classes will use.
I wish to set the parameters for a Stored Proc in the derived class.
But it is the Base class which contains the code to call this method.
How can I declare this method in the base class so that it will compile but ensure that the derived class method is called?
Much appreciaton for any assistance.
Thanks for your time
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
|
|

May 26th, 2005, 08:00 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Rod,
What is your reason for wanting to inherit in the first place? Why not make properties on the class- set them from the business class and then call the SP with the parameters that were set from the business class?
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
|
|

May 26th, 2005, 10:46 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Hay Hal,
Hmmm, Good Idea...I guess I hadn't though that through well enough.
There are a lot of shared properties.
Fields that are common to all tables.
Things such as LastModifiedDateTime and the like.
This is the data access layer and the base class handles all the code for those properties.
With the inheritance each new class has all the base functionality, all I have to do is set create the stored procs and set the parameters in the class file to get the base functionality.
But I guess I can still inherit the class and call the bus class for the data access like you suggested.
I guess I was trying to do too much with the base class.
Thanks Hal, I'll ponder the options.
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
|
|

May 26th, 2005, 08:16 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Rod,
You could inherit a base class and use that in the other data layer objects. The thought is still the same.. have the business layer set properties (specific to that class or from the base class) and then call the SP that was inherited.
If so much is the same, though, why make different classes that inherit and not just use the base class directly? Just for typing purposes? (Not that I object to that design... I do it myself when appropriate)
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
|
|

May 29th, 2005, 03:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Hal,
I use a property in the derived class which sets a stored procedure name.
This stored procedure handles the basic functions of the tables data access and is essentially the same for all tables/classes. bar the parameters list and the differing fields.
So I can Insert, Update and Delete a record.
All this means that to create a new class within my data access layer, all I have to do is Name it, specify the DataAccessStoredProcName, and have a method that sets the parameters for the class that are extra to the base class parameters.
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
|
|

May 29th, 2005, 03:45 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
I have a data access layer already, I am using Microsoft Data Access Application Block.
I'll post some code. Show you what Im trying to do.
This is a LOW priority issue, so please dont waste undue time on it, I just wouldn't mind nowing if Im heading in the right direction.
Thanks heaps for your comments to date.
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.CustomMessaging;
using DAL.Types;
using Microsoft.ApplicationBlocks.Data;
#endregion
namespace DAL.Stock
{
public class Product:Base
{
private Guid productID;
private string productName;
private string productDescription;
private ProductGroup productGroup;
public Product()
{SetQueryParameters();}
public Product(Guid productId)
{
SetQueryParameters();
DataAccessMessage dam = Select();
productName = dam.ResultantDataSet.Tables[0].Rows[0]["productName"].ToString();
productDescription = dam.ResultantDataSet.Tables[0].Rows[0]["ProductDescription"].ToString();
productGroup = new ProductGroup(new Guid(dam.ResultantDataSet.Tables[0].Rows[0]["ProductGroupID"].ToString()));
}
public static DataAccessMessage List()
{return Base.List("Product_List");}
private void SetQueryParameters()
{
dataAccessStoredProcedure = "Product_DataAccess";
parameters.Add(new SqlParameter("@ProductID", productID));
parameters.Add(new SqlParameter("@ProductName", productName));
parameters.Add(new SqlParameter("@ProductDescription", productDescription));
parameters.Add(new SqlParameter("@ProductGroupID", productGroup.ProductGroupID));
parameters.Add(new SqlParameter("@RecordModifiedBy", recordModifiedBy));
parameters.Add(new SqlParameter("@RecordModifiedDate", recordModifiedDate));
parameters.Add(new SqlParameter("@DataAccessType", dataAccessType.ToString()));
}
}
}
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using DAL.CustomMessaging;
using DAL.Types;
#endregion
namespace DAL
{
public class Base
{
protected string recordStatus = string.Empty;
protected string recordModifiedBy = string.Empty;
protected string recordCreatedBy = string.Empty;
protected DateTime recordModifiedDate = DateTime.Now;
protected DateTime recordCreatedDate = DateTime.Now;
//protected SqlParameter[] parameters;
protected Parameters parameters = new Parameters();
protected DataAccessType dataAccessType = new DataAccessType();
protected string dataAccessStoredProcedure = string.Empty;
protected string DataAccessStoredProcedure = string.Empty;
protected SqlConnection conn = new SqlConnection("");
public static SqlConnection Conn = new SqlConnection("");
DataAccessMessage dam;
public Base()
{
}
protected DataAccessMessage Select()
{
SetDataAccessType(DataAccessType.Select);
try
{
DataSet ds = SqlHelper.ExecuteDataset(Conn, dataAccessStoredProcedure, parameters);
dam = new DataAccessMessage();
dam.RecordsAffected = 0;
dam.RecordsReturned = ds.Tables[0].Rows.Count;
dam.ResultantDataSet = ds;
if (ds.Tables[0].Rows.Count > 0)
{
dam.Successful = true;
dam.Message = "The records where successfully selected.";
dam.DebugInformation = string.Empty;
}
else
{
dam.Successful = true;
dam.Message = "The are no records to return.";
dam.DebugInformation = string.Empty;
}
}
catch (Exception e) { DataAccessError(e); }
return dam;
}
public DataAccessMessage Insert(string RecordModifiedBy)
{
SetDataAccessType(DataAccessType.Insert);
try
{
dam = new DataAccessMessage();
dam.RecordsAffected = SqlHelper.ExecuteNonQuery(conn, dataAccessStoredProcedure, parameters);
dam.RecordsReturned = 0;
dam.ResultantDataSet = new DataSet();//EmptyDataset
dam.Successful = true;
dam.Message = "The record has been successully saved.";
dam.DebugInformation = string.Empty;
//Make sure the update occured.
if (dam.RecordsAffected <= 0)
{
dam.Successful = false;
dam.Message = "The record was not saved.";
dam.DebugInformation = "No rows were effected by the insert query.";
}
}
catch (Exception e) { DataAccessError(e); }
return dam;
}
public DataAccessMessage Update(string RecordModifiedBy)
{
SetDataAccessType(DataAccessType.Update);
try
{
dam = new DataAccessMessage();
dam.RecordsAffected = SqlHelper.ExecuteNonQuery(conn, dataAccessStoredProcedure, parameters);
dam.RecordsReturned = 0;
dam.ResultantDataSet = new DataSet();//EmptyDataset
dam.Successful = true;
dam.Message = "The record has been successully updated.";
dam.DebugInformation = string.Empty;
//Make sure the update occured.
if (dam.RecordsAffected <= 0)
{
dam.Successful = false;
dam.Message = "The record was not updated.";
dam.DebugInformation = "No rows were effected by the update query.";
}
}
catch (Exception e) { DataAccessError(e); }
return dam;
}
public DataAccessMessage Delete(string RecordModifiedBy)
{
SetDataAccessType(DataAccessType.Delete);
try
{
dam = new DataAccessMessage();
dam.RecordsAffected = SqlHelper.ExecuteNonQuery(conn, dataAccessStoredProcedure, parameters);
dam.RecordsReturned = 0;
dam.ResultantDataSet = new DataSet();//EmptyDataset
dam.Successful = true;
dam.Message = "The record has been successully deleted.";
dam.DebugInformation = string.Empty;
//Make sure the update occured.
if (dam.RecordsAffected <= 0)
{
dam.Successful = false;
dam.Message = "The record was not deleted.";
dam.DebugInformation = "No rows were effected by the delete query.";
}
}
catch (Exception e) { DataAccessError(e); }
return dam;
}
public static DataAccessMessage List(string StoredProcedureName)
{
DataAccessMessage dam = new DataAccessMessage();
try
{
DataSet ds = SqlHelper.ExecuteDataset(Conn, StoredProcedureName);
dam.RecordsAffected = 0;
dam.RecordsReturned = ds.Tables[0].Rows.Count;
dam.ResultantDataSet = ds;
if (ds.Tables[0].Rows.Count > 0)
{
dam.Successful = true;
dam.Message = "The records where successfully selected.";
dam.DebugInformation = string.Empty;
}
else
{
dam.Successful = true;
dam.Message = "The are no records to return.";
dam.DebugInformation = string.Empty;
}
}
catch (Exception e)
{
dam = new DataAccessMessage(
false,
"There was an error accessing the database.",
e.Message,
new DataSet(),
0,
0);
}
return dam;
}
private void DataAccessError(Exception e)
{
dam = new DataAccessMessage(
false,
"There was an error accessing the database.",
e.Message,
new DataSet(),
0,
0);
}
private void SetDataAccessType(DataAccessType dat)
{
foreach (SqlParameter para in parameters)
{
if (para.ParameterName == "@DataAccessType")
{
para.Value = dat.ToString();
break;
}}}}}
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
|
|

May 30th, 2005, 06:32 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Rod,
I don't have the time at the moment to look at the code. I'll try to take a look later this week. I know Tuesday is booked solid.
I do, however, now better understand what your doing- and why.
My first quick comment- and I know many people disagree- is don't use the Application Blocks. It's easy enough to do data access without them- and I have seen them cause many headaches.
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
|
|

June 1st, 2005, 01:19 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Thanks Hal,
I have only used application block in one commercial app and found them very helpful in that.
Aside from one bug, which I found a fix for on the net, I have not had any trouble
Although I haven't had any luck getting hte latest version to work on VS2005 Beta.
What issues have you come accross?
When you say you don't use the application blocks to you create you own data access handler class or do you put the code in each class?
Best Regards,
Rod
======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
|
|
 |