|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionBeing a J2EE developer in the past and a very big fan of OOD , I started a few months ago to work in .NET. Now I have seen the .NET data persistence approach, the Using JDO in the past , I considered making a XML description for each BO to map it to the database tables, but I saw that a better thing would be to use .NET attributes. I have found an article on the Internet with an example of using attributes for such a purpose. Let’s say we have a BO called public class User
{
public User(){}
private long _id;
private string _name;
private string _username;
private string _password;
//for each of these fields we have a Property
public long UserID {
set{_id = value;}
get {return _id;}
}
//etc
…
}
Now let’s map in to the database: [DBTable("Users","dbo")]
[Serializable]
public class User
{
public User(){}
private long _id;
private string _name;
private string _username;
private string _password;
[DBPrimaryKeyField("ID_User",DbType.Int16)]
[DBColumn("ID_User",DbType.Int16,false)]
public long UserID
{
set{_id = value;}
get {return _id;}
}
[DBColumn("Name",DbType.String,true)]
public string Name
{
//set, get
}
[DBColumn("Username",DbType.String,false)]
public string Username
{
//set, get
}
[DBColumn("Password",DbType.String,false)]
public string Password
{
//set, get
}
}
We have 3 attribute classes
Now let’s look at some things that can be made with the BO. This is done using an public interface IPersistenceService
{
object Create(Object o, Type objectType);
void Update(Object o, Type objectType);
void Delete(Object id, Type objectType);
int Count(string condition, Type objectType);
Object GetObject(Object id, Type objectType);
void LoadDataSet(DataSet dataSet, String sqlQuery);
void LoadDataSet(DataSet dataSet, String sqlQuery, String tableName);
object Create(SqlConnection con, Object o, Type objectType);
void Update(SqlConnection con,Object o, Type objectType);
void Delete(SqlConnection con,Object id, Type objectType);
int Count(SqlConnection con,string condition, Type objectType);
Object GetObject(SqlConnection con,Object id, Type objectType);
void LoadDataSet(SqlConnection con,DataSet dataSet, String sqlQuery);
void LoadDataSet(SqlConnection con,DataSet dataSet, String sqlQuery,
String tableName);
SqlConnection GetConnection();
void CloseConnection(SqlConnection con);
}
We have a factory for this interface to get an implementation. In this case the implementation is made against SQL Server in String connectionString = “…”;
IPersistenceService ps = PersistenceServiceFactory.getPS(connectionString);
Now let’s create an User u = new User();
u.Name = "Dan Bunea";
u.Password = "myusername";
u.Password = "mypassword";
//obtain id for the new user in the
long id = (long)ps.Create(u,typeof(User));
or just: (if you don’t need the ID obtained for the object) ps.Create(u,typeof(User));
Now that we have a user in the User U2 = (User)ps.GetObject(id,typeof(User));
Let’s update it and save the changes. U2.Name = "Dan Bunea Updates";
U2.Password = "newusername";
U2.Password = "newpassword";
P2s.Update(U2,typeof(User));
Simple J , no? Now let’s delete it. Ps.Delete(id,typeof(User));
l have also included some methods to load SqlConnection con = ps.GetConnection();
User u = new User();
u.Name = "Dan Bunea";
u.Password = "myusername";
u.Password = "mypassword";
object id = (long)ps.Create(con,u,typeof(User));
User U2 = (User)ps.GetObject(con,id,typeof(User));
U2.Name = "Dan Bunea Updates";
U2.Password = "newusername";
U2.Password = "newpassword";
P2s.Update(con,U2,typeof(User));
Ps.Delete(con,id,typeof(User));
ps.CloseConnection(con);
Let’s talk a little about performance. For each BO the user must not be concerned about With special thanks for those that have written other articles that helped me write this, and in the hope that this was a pleasant and useful reading , I finish my small article here.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||