Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Scheme of Persistence

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Nov 2007CDDL3 min read 23K   83   20   2
Mapping business objects and Store Procedures

Introduction

Normally when a business application is created, this one involves the kept one and recovery of the information in BD. This process in .Net normally does by means of Datasets in such a way that the information is used by the above mentioned structure for the representation of the business. The components to that they present themselves are an option for this process and that the representation does with business objects.

The idea is that we have business objects and Store Procedures that can or not to return information table, and a mapping exists between some and others, by what there is needed a table of mapping and a process that allows the creation of multiple objects and realizes the mapping, this is provided in the component SQLSPMapping.dll; also, it allows the handling of the context transactional.

Using the code

In this example SqlServer is used with the database Northwind for what they add a pair of Store Procedures for the kept one the information and "CustOrdersDetail" is modified.

For the use of this component there must exist a file XML, (actually for every assembly that uses this component), in this file the information of Store Procedures keeps that use the classes, Also, these names do not have because to be those of the store inside the code of .Net, for what a mapping exists also between store and methods. This file must be stored in the same place where the assembly is and must respect the structure of name "[Assembly name].pers.xml"

The structure of the xml is the following one:

XML
<?xml version="1.0" encoding="utf-8" ?> 
<Mapping
  Connection='Connection String'
  Assembly='Assembly for database connection'
  ConnectionClass='Name of class connection'>
  <'Name Space'>
    <'Name Space'>
      <'Name Space'>
        <Class name="persistent class"
          Connection='Connection String'
          Assembly='Assembly for database connection'
          ConnectionClass='Name of class connection'>
          <Procedure 
            Method="[Name of method in .Net code]" 
            StoreProcedure="[name of Stored Procedure]"
            Connection='Connection String'
            Assembly='Assembly for database connection'
            ConnectionClass='Name of class connection'>
            <InputParameters>
              <parameter Property="[.Net Property]" ParameterBD="[Parameter BD]" type="0"/>
              <parameter Property="[.Net Property]" ParameterBD="[Parameter BD]" type="0"/>
              <parameter Property="[.Net Property]" ParameterBD="[Parameter BD]" type="0"/>
            </InputParameters>
            <OutputParameters>
              <parameter Property="[.Net Property]" ParameterBD="[Parameter BD]" type="0"/>
              <parameter Property="[.Net Property]" ParameterBD="[Parameter BD]" type="0"/>
              <parameter Property="[.Net Property]" ParameterBD="[Parameter BD]" type="0"/>
            </OutputParameters>
      </Procedure>      
    </Class> 
  </WinPersistApp>
</Mapping>

Since it is possible to observe the node root it is " Mapping " and the hierarchy of the namespace comes, it is like that to guarantee the unicidad of the classes, also it is possible to see that there are attributes in the nodes Mapping, Class and Procedure for the connection to BD, for different connections to databases. Obviously if any fact is not specified this one is taken of the previous immediate reference. It is important to notice that this file is for assembly and can have more than one class and every class can have more than one and every class can have more than one procedure and every procedure can have very different number of parameters of entry or output columns.

The business classes are:

C#
[Persistent(typeof(Orden))]
public class Orden
{
  string custId;
  int orderID;
  DateTime orderDate;
  DateTime requiredDate;
  DateTime shippedDate;
  ArrayList detalle;
  public string CustomerID
  {
    get{return custId;}
    set {custId=value;}
  }

  public int OrderID
  {
    get {return orderID;} 
    set
    {
      orderID=value;
      OrdersDetail od=new OrdersDetail();
      od.IdParent=value;
      PersistenceMgr ej=new PersistenceMgr();
      detalle=ej.Execute(od,"DetalleOrden");
      if(detalle==null)
        detalle=new ArrayList();
    }
  }

  public DateTime OrderDate{get {return orderDate;} set{orderDate=value;}}
  public DateTime RequiredDate{get {return requiredDate;} set{requiredDate=value;}}
  public DateTime ShippedDate{get {return shippedDate;} set{shippedDate=value;}}
  public ArrayList Detalle{get{return detalle;}} 
 
  public void Save()
  {
    PersistenceMgr pers=new PersistenceMgr();
    pers.BeginTransaction();
    pers.Execute(this,"ActualizaOrden");
    foreach(OrdersDetail det in this.detalle)
    {
      det.Save();
    }
    pers.CommitTransaction();
  }
}




[Persistent(typeof(OrdersDetail))]
public class OrdersDetail
{
  string productName;
  double unitPrice;
  int quantity;
  double discount;
  double extendedPrice;
  int idParent;
  int idProduct;
  public int IdParent{get {return idParent;} set{idParent=value;}}
  public string ProductName{get {return productName;} set{productName=value;}}
  public double UnitPrice{get {return unitPrice;} set{unitPrice=value;}}
  public int Quantity{get {return quantity;} set{quantity=value;}}
  public double Discount{get {return discount;} set{discount=value;}}
  public double ExtendedPrice{get {return extendedPrice;} set{extendedPrice=value;}}
  public int IDProduct{get {return idProduct;} set{idProduct=value;}}
  public void Save()
  {
    PersistenceMgr pers=new PersistenceMgr();
    pers.BeginTransaction();
    pers.Execute(this,"ActualizaDetalle");
    pers.CommitTransaction();
  }
}  

Where we can observe that both classes have the attribute " Persistent " with parameter " typeof ([class yam]) " that is used to recover the mapeo who is in the file xml of the particular class.

The instructions that are used for the execution can be:

PersistenceMgr pers=new PersistenceMgr();
pers.BeginTransaction();
pers.Execute(this,"ActualizaDetalle");
pers.CommitTransaction();

On having executed them, the values that it has this (the class on which we are working) use as parameters for the "Procedure" indicated in the xml for this class, in this case "ActualizaDetalle" that mapping the store left:"updOrderDetail" and has the parameters of the store left:

PropertyStore Procedure Parameter
IdParent@OrderID
IDProduct@ProductID
Quantity@Quantity

All this mapping is in the archive xml.

If they find some error or progress please make it go over.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Web Developer
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhy not a O/R Mapper?!?! Pin
Magnus Salgo26-Nov-07 18:50
Magnus Salgo26-Nov-07 18:50 
GeneralRe: Why not a O/R Mapper?!?! Pin
IVAN MARTINEZ HERNANDEZ8-Dec-07 16:06
IVAN MARTINEZ HERNANDEZ8-Dec-07 16:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.