Programming in .NET – part 3 -initial object code

It would be usefull to read part 1 and part 2

Now back to the object creation

Every object must have some properties that correspond to
the DB fields of the table and , for easier of use, other properties/methods.

We will wrote those Publisher and we left others as an
exercise for the user. For saving /delete /creating a Publisher object we will
implement corresponding method update / delete / insert.

The fact that a publisher must have an unique name will be
handled later.

From the previous part, we know that we have to handle two
different databases ( Access and SQL Server)

So we must have two connections strings – one for each
database.To set up easy the connection , please see www.connectionstrings.com

 

 

For viewing all the Publisher records, we must store in some
part the set of records. This set will be a class named ColPublisher. This
class will contain a method named Load that will load the Publisher from the
database and store them into the collection.

 

Now, when the path of what to do is more clear, let’s write
some code.

Start Visual C# Express( make the settings appropiate for
C#, if it is the first time you launch) and create a new class library project
named BookObjects

and save in the C:\book

 

 

By default, the System.Data is listed on the References . If
not, please add as following the reference to System.Data.But anyway, add a
reference to System.Configuration.

Click right on Solution Explorer and Add reference to
OleDBConnection as in the figure :

 

 

Rename the Class1.cs by clickingon his properties ( click the file on
Solution Explorer and press F4) to Publisher.cs

If you answer yes to the next question, the class name in
Class1 will be renamed into Publisher – and make it a publicclass

 

Now we will wrote the basic properties for Publisher :

using
System;

using
System.Collections.Generic;

using

System.Text;


 

namespace

BookObjects

{




public
class
Publisher



{



#region

Database properties




private
int m_IDPublisher;




public
int IDPublisher



{




get



{






return
m_IDPublisher;



}




set



{



m_IDPublisher = value;



}



}


 




private
string m_Name;


 




public
string Name



{




get



{




return
m_Name;



}




set



{



m_Name = value;



}



}


 




private
string m_Site;


 




public
string Site



{




get



{




return
m_Site;





}




set



{



m_Site = value;



}



}



#endregion


 



}

}

 

 

We must fill those properties from database, so we must
wrote code to Fill the object:

For this, we can wrote like this, obtaining independence
from the database:

 

 



#region

Database
methods




public
void FillObject(System.Data.IDataReader
idr)



{




this.Name
= idr[“NamePublisher”].ToString();




this.Site
= idr[“SitePublisher”].ToString();



}



#endregion


 

 

 

Now we must REALLY load data from the database, so we must
add a new class ( Project => Add Class) and name it ColPublisher.cs.

Again , make it a publicclass
and let’s wrote code to load data:

 



public


void Load()



{


 



}

Now we see that we must differentiate between the two
connections – and load them from somewhere.

So we wrote code to load connection first:








public
static
string
ConnectionStringMDB





{




get



{




return
System.Configuration.ConfigurationManager.ConnectionStrings[“MDB”];



}



}




public
static
string
ConnectionStringSQLServer



{




get



{






return
System.Configuration.ConfigurationManager.ConnectionStrings[“SQLServer”];



}



}


 


 

As you know , we have two connection strings – one for
each database.

The moment is now to differentiate between those two – the
Load function must know what connection string to use .

I will add a new class named Settings and we will put there
all the common settings.

I will add an enumeration to know which database we will use


 



public


enum
DatabaseUsed



{



None,





MDB,



SQLServer



}

and we will read from the database :



public


static
DatabaseUsed
TheDatabase



{




get



{




return
Enum.Parse(typeof(DatabaseUsed), System.Configuration.ConfigurationManager.AppSettings[“DatabaseUsed”];



}



}

 

You can observe from here that we put the type of database
in a Appsetting . We can store the connection strings at the same place, but we
want to be compatible with the Microsoft standard.

The code can be downloaded from here

Next time we will wrote code to load data from the database
and put into the collection and more, to easily indentify the objects.

 

Further readings:

For .NET related best practices, you can read http://www.ssw.com.au/ssw/Standards/default.aspx

For ORM (Object-relational_mapping) as we do here, you can
read the article on Wikipedia (http://en.wikipedia.org/wiki/Object-relational_mapping)
and see a list of ORM-s at http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software
. I think that you can read at least one, as Nhibernate : http://www.hibernate.org/343.html

For generating same code from tables in database, you can
try reading Code Generation (http://en.wikipedia.org/wiki/Code_generation)
and specifically CodeSmith (http://www.codesmithtools.com/)

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *