Вы находитесь на странице: 1из 10

Chapter 7

Overview of the PDSA


Data Model
TableofContents
Chapter 7 ....................................................................................................................... 7-1
Overview of the PDSA Data Model ................................................................................. 7-1
The PDSA Data Model ....................................................................................... 7-1
Definitions ........................................................................................................... 7-3
Overview of the Generated Code ....................................................................... 7-3
PDSA Data Layer ............................................................................................... 7-4
PDSADataManager Class ...................................................................... 7-4
GetDataSet Method ................................................................................ 7-5
PDSA Data Providers Configuration Section .......................................... 7-6
ExecuteSQL Method............................................................................... 7-7
Other Methods ........................................................................................ 7-9
Chapter Index ................................................................................................... 7-10

The PDSA Data Model


The PDSA Data Model (Figure 1) consists of a few different pieces. First off, you
have the UI layer (ASP.NET, WPF, Silverlight, etc.). Your UI can either call a

Overview of the PDSA Data


ViewModel or can just call the data classes directly. Calling a ViewModel does
make good architectural sense as this leads to more re-usable and code that can
be tested easily.
This chapter will provide you with an overview of the PDSA Data Layer, some
definitions you need to know and what each of the generated classes are for.
You will dive into more detail in succeeding chapters.

Figure 1. The PDSA Data Model

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

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

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.

Overview of the Generated Code


The following table lists the generated files and what they are used for. The next
few chapters will talk in more detail about how to use each of the generated
classes. A Product table is used for the description of each item in the following
table.
File Name

Class Name

Description

ProductEntity.Generated.xx

Product

Contains all of the properties that


map to each column in your Product
table. You should not modify this file
at all.

Product.xx

Product

A partial class definition that gives


you a place to add additional
properties to your entity class if you
need to.

ProductData.Generated.xx

ProductData

This is where the CRUD logic for the


Property table resides. You should not
modify this file at all.

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

7-3

Overview of the PDSA Data


ProductManager.xx

ProductManager

This is the aggregator class that has


properties for the Entity, Data and
Validator objects. You may add
additional methods to this class.

ProductValidator.xx

ProductValidator

This class is where you may add


additional business rules for your
entity object. There are many
business rules that are inferred from
the schema of your table, but this
gives you a place to augment these.

ProductValidator.Generated.xx

ProductValidator

This class is where all business rules


that can be inferred from the schema
are written. You may also add some
additional validation logic thru
Haystack such as, minimum value,
maximum value, and minimum
length.

PDSA Data Layer


This DLL contains very thin wrapper classes around ADO.NET. This layer makes
calls to the data provider based on what you have setup in your .Config file for
your application. There is not a lot that you have to understand about this layer
as it just has simple method calls such as GetDataSet, GetDataTable,
GetDataReader and ExecuteSQL to name a few. These are pretty selfexplanatory. This layer does all the work of loading the appropriate provider for
the database you wish to use; ie. SQL Server, Oracle, etc.

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

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

PDSA Data Layer

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.

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

7-5

Overview of the PDSA Data

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.

PDSA Data Providers Configuration Section


Did you notice that in the GetDataSet() method listed above that you did not see
a connection string? That is because of the way the PDSA Data Configuration
system works (Figure 2). The PDSA Data Manager will look into your .Config file
for the <PDSADataProviders> section. When it finds this section it will read the
defaultProvider attributes value. For example, it will read SqlClient. It then
matches this value up with one of the <add providerName> elements to load
that specific provider. In addition, within this <add> element is an attribute called
connectStringName. This value is read and matched up to the appropriate
<add> element contained within the <connectionStrings> element in your .Config
file.
You must have both of these sections in your .Config file or you must hard code
all of the values and load the provider manually. Trust us, you do not want to
code this! It is just easier to set your .Config file appropriately and let it do all the
work.

7-6

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

PDSA Data Layer

Figure 2. The PDSA Data Model Configuration Settings

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.

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

7-7

Overview of the PDSA Data

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

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

PDSA Data Layer


End Sub

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.

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

7-9

Overview of the PDSA Data

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

Haystack Code Generator for .NET


Copyright 2010 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Вам также может понравиться