Académique Documents
Professionnel Documents
Culture Documents
As you can see in Figure 1, there are a few layers. The PDSA Data Layer and
the Data Providers are provided for you as DLLs that you just call and use. The
PDSA Data Classes are generated as source code for you by the Haystack
Code Generator for .NET.
In this chapter we will show you using the PDSA Data Layer. However, it is
important to note that Haystack will put a wrapper around the code shown here.
So you wont generally write the code you see in this chapter, but this will help
you to understand what is going on underneath the hood of the generated
classes.
7-2
Definitions
Definitions
Lets define a few terms that will be used in the PDSA Data Model.
Term
Definition
Entity Class
Also referred to as a Data Transfer Object. This class has nothing but
properties. These properties are typically decorated with the appropriate
attributes to allow this object to be serialized across a WCF or Web service.
This class will be generated with one property for each column in the table
from which it is modeled.
Manager
Class
This class will be the primary class that you interact with to retrieve data and
modify data in your table. This class is an aggregate class in that it contains
properties with references to an Entity object, a Data object and a Validator
object. You may add additional methods to this class as well.
Data Class
This class contains all the CRUD (Create, Read, Update, Delete) logic for the
table from which it is modeled. All your select, insert, update and delete
statements are contained within this class.
Validator
Class
This class is where the business rules for the Entity class reside. This class
does not know anything about databases, and is only used to validate that the
data within an Entity class is valid.
Class Name
Description
ProductEntity.Generated.xx
Product
Product.xx
Product
ProductData.Generated.xx
ProductData
7-3
ProductManager
ProductValidator.xx
ProductValidator
ProductValidator.Generated.xx
ProductValidator
PDSADataManager Class
The PDSADataManager class is the main class in the PDSA Data Layer that all
the generated data classes use. Below is a sample of the code that you can write
directly against the PDSA Data Layer if you had a need to. (NOTE: You should
not have a need to down to this layer since the generated classes do this for
you.)
7-4
GetDataSet Method
C#
private void GetDataSet()
{
DataSet ds;
PDSADataManager mgr = new PDSADataManager();
ds = mgr.Provider.GetDataSet(
"SELECT * FROM PDSASample.Product");
grdValues.DataSource = ds.Tables[0];
}
VB.NET
Private Sub GetDataSet()
Dim ds As DataSet
Dim mgr As New PDSADataManager()
ds = mgr.Provider.GetDataSet( _
"SELECT * FROM PDSASample.Product")
grdValues.DataSource = ds.Tables(0)
End Sub
In the above code you call the GetDataSet from the Provider property of the
PDSADataManager class to retrieve a DataSet. This is much easier than writing
all of the normal ADO.NET code you would normally have to write. There are
several overloads of the GetDataSet method that you can call.
7-5
C#
public virtual DataSet GetDataSet(IDbCommand cmd);
public virtual DataSet GetDataSet(string sql);
public virtual DataSet GetDataSet(IDbCommand cmd,
string connectString);
public virtual DataSet GetDataSet(string sql,
IDbConnection cnn);
public virtual DataSet GetDataSet(string sql,
string connectString);
VB.NET
Public Overridable Function GetDataSet( _
ByVal cmd As IDbCommand) As DataSet
Public Overridable Function GetDataSet( _
ByVal sql As String) As DataSet
Public Overridable Function GetDataSet( _
ByVal cmd As IDbCommand, _
ByVal connectString As String) As DataSet
Public Overridable Function GetDataSet(ByVal sql As String, _
ByVal cnn As IDbConnection) As DataSet
Public Overridable Function GetDataSet(ByVal sql As String, _
ByVal connectString As String) As DataSet
The generated data classes most typically call the first one listed in the code
above. A Command object works best since you will want to setup a SQL
statement with parameters and fill in the data into each parameter and submit
that. This avoids any SQL injection attacks and provides the best performance.
7-6
ExecuteSQL Method
To execute a INSERT, UPDATE or DELETE method you can call the
ExecuteSQL method on the PDSADataProvider object. The listing below shows
how to execute an INSERT statement by passing parameters.
7-7
C#
private void ExecuteSQL()
{
PDSADataManager mgr = new PDSADataManager();
PDSADataProvider prov;
IDbCommand cmd;
IDataParameter param;
int rows;
string sql = "INSERT INTO PDSASample.Product
(ProductName,Price)
VALUES (@ProductName,@Price)";
prov = mgr.Provider;
cmd = prov.CreateCommand(sql);
cmd.Connection = prov.CreateConnection(prov.ConnectString);
param = prov.CreateParameter("ProductName",
DbType.String);
param.Value = "A New Product";
cmd.Parameters.Add(param);
param = prov.CreateParameter("Price",
DbType.Decimal);
param.Value = 200;
cmd.Parameters.Add(param);
// Execute the SQL and close the connection
rows = prov.ExecuteSQL(cmd, true);
}
VB.NET
Private
Dim
Dim
Dim
Dim
Dim
Dim
Sub ExecuteSQLHardCodedInterface()
mgr As New PDSADataManager()
prov As PDSADataProvider
cmd As IDbCommand
param As IDataParameter
rows As Integer
sql As String = "INSERT INTO
PDSASample.Product(ProductName,Price)
VALUES (@ProductName,@Price)"
prov = mgr.Provider
cmd = prov.CreateCommand(sql)
cmd.Connection = prov.CreateConnection(prov.ConnectString)
param = prov.CreateParameter("ProductName", _
DbType.[String])
param.Value = "A New Product"
cmd.Parameters.Add(param)
param = prov.CreateParameter("Price", DbType.[Decimal])
param.Value = 200
cmd.Parameters.Add(param)
' Execute the SQL and close the connection
rows = prov.ExecuteSQL(cmd, True)
7-8
Again, this code is significantly less than if you were to program ADO.NET
yourself. Plus this has the benefit of being generic and can be use against SQL
Server, Oracle, or any other database for which you have a provider.
The ExecuteSQL method has many overloads as shown in the listing below:
public virtual int ExecuteSQL(IDbCommand cmd);
public virtual int ExecuteSQL(IDbCommand cmd,
bool CloseConnection);
public virtual int ExecuteSQL(IDbCommand cmd,
string connectString);
public virtual int ExecuteSQL(string sql, IDbConnection cnn);
public virtual int ExecuteSQL(string sql,
string connectString);
Other Methods
There are other methods available in the PDSA Data Layer. You have the
ExecuteScalar method to return a single value. You can retrieve a DataTable or
a Data Reader and you can open connections to the database. As stated before
you will probably not need to use these classes and methods as they are all
wrapped up for you in the generated data classes, but it is good to know what is
underneath the hood.
7-9
Summary
In this chapter you were given an overview of the PDSA Data Model. Now that
you have an overview of what is generated, you can now go on and read the
other chapters to gain further information on what you can do with each of the
generated classes.
Chapter Index
B
Business Class, 7-3
D
Data Class, 7-3
Data Model, 7-1
Data Model Figure, 7-2
E
Entity Class, 7-3
ExecuteScalar Method, 7-9
ExecuteSQL Method, 7-7
G
GetDataSet Method, 7-5
M
Manager Class, 7-3
O
Overview of the Generated Code, 7-3
Overview of the PDSA Data Model, 7-1
P
PDSA Data Model, 7-1, 7-4
PDSA Data Model Definitions, 7-3
PDSADataManager Class, 7-4
PDSADataProviders Configuration
Section, 7-6
V
Validator Class, 7-3
7-10