I’m currently working on an integration project to connect two disparate systems.
My plan is to setup an HTTP-API to allow System A to issue commands through HTTP-POST to System B.
The commands will be used to issue CRUD instructions to a SQL server database to retrieve and update membership card data (e.g. ‘create membership’, ‘update membership’, etc.) and then return the data to System A as XML. I know n-tier design dictates that I should have a MembershipCard class with several properties:
public class MembershipCard
{
private string number;
private decimal points;
public string Number
{
get { return number; }
set { number = value; }
}
public decimal Points
{
get { return points; }
set { points = value; }
}
Along with several methods that make calls to the DAL:
public string GetPoints(string cardnumber)
{
return MembershipCardDB.BalanceRequest(cardnumber);
}
However, I’m having some difficulty justifying this approach as the static DAL class, MembershipCardDB seems to perform all the work that I need (seen below):
public static string BalanceRequest(string cardNumber)
{
string response = string.Empty;
string sqlselect =
"SELECT Balance " +
"FROM tbl_MemberShipCard " +
"WHERE Card_No = @cardNumber " +
"FOR XML PATH ('Card'), ROOT('CardBalance')";
using (SqlConnection connect = new SqlConnection("Data Source=TEST\\TEST;Initial Catalog=TEST;User Id=sa;Password=TEST"))
{
connect.Open();
using (SqlCommand command = new SqlCommand(sqlselect))
{
command.Parameters.AddWithValue("@cardNumber", cardNumber);
response = (string)command.ExecuteScalar();
}
}
return response;
}
Is there anything that I’m overlooking by simply removing the MembershipCard class and just pulling the data from the database and formatting it as XML?
The point is that you should be able to write your program logic independently from any database interface or xml format. Put all the database stuff into a separate class that loads and creates the objects. Do whatever you want to do with the objects. Finally store the objects back to the database or to xml. If your code is only about importing and exporting data, however, you can drop the extra classes.
By the way, you can simplify the
MembershipCardclass by using auto-implemented properties:I often have a static
DBclassYour
MembershipCardclass could have a methodLike this you have a separation of database operations and other application logic.