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:
="1.0"="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:
[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:
Property | Store 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.