Better n-tier suggestions

  • Thread starter Thread starter cbmeeks
  • Start date Start date
C

cbmeeks

I work for a small company that has existing code from several people.

The existing code base is pretty good overall but we do have lots of
embedded SQL, hard-coded this, and other types of garbage floating
around.

One thing I want to do is better separate our data layers with our
business layers. I would like to do that using some type of ORM.

However, here's the deal, I want to do it by hand...ie, the hard way.
Our needs are pretty slim for now so I don't believe this to be a
major problem.

I am looking for suggestions/pointers to accomplish the following:

Say I have a table in my database called Persons. Persons table
contains columns ID, Name, Age.

Now, in C#, I would like to do the following:

Persons p = new Persons();
p.Name = "Chuck U. Farley";
p.Age = 35;
p.Save();

The Save method would create the desired SQL for the insert.

Now, I can mock up something that actually works pretty well. But the
problem is if I decide to change the database column "Name" to
"FullName". I then have to change code, etc.

I use methods to reduce the amount of code changes but I just feel the
whole process could be made simpler.

Any suggestions?

Thanks!
 
With all due respect, it can be made easier, use LINQ to SQL. Doing
something like this by hand for something this simple is just masochistic.

Even if you can't use LINQ to SQL, there are other options to make your
life much, much easier. NHibernate is an option that might be of use to
you.

In the end, if you use something with designer support, it will help
prevent you from having to make these changes by hand when they arise.
 
With all due respect, it can be made easier, use LINQ to SQL. Doing
something like this by hand for something this simple is just masochistic.

Even if you can't use LINQ to SQL, there are other options to make your
life much, much easier. NHibernate is an option that might be of use to
you.

In the end, if you use something with designer support, it will help
prevent you from having to make these changes by hand when they arise.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I work for a small company that has existing code from several people.
The existing code base is pretty good overall but we do have lots of
embedded SQL, hard-coded this, and other types of garbage floating
around.
One thing I want to do is better separate our data layers with our
business layers. I would like to do that using some type of ORM.
However, here's the deal, I want to do it by hand...ie, the hard way.
Our needs are pretty slim for now so I don't believe this to be a
major problem.
I am looking for suggestions/pointers to accomplish the following:
Say I have a table in my database called Persons. Persons table
contains columns ID, Name, Age.
Now, in C#, I would like to do the following:
Persons p = new Persons();
p.Name = "Chuck U. Farley";
p.Age = 35;
p.Save();
The Save method would create the desired SQL for the insert.
Now, I can mock up something that actually works pretty well. But the
problem is if I decide to change the database column "Name" to
"FullName". I then have to change code, etc.
I use methods to reduce the amount of code changes but I just feel the
whole process could be made simpler.
Any suggestions?

Thanks. And yes, I did forget to mention that LINQ is out due to
machines only having .NET 2.0 and management doesn't want to roll out
3.5 (hey, I'm just a programmer...lol)

I will check out NHibernate.

Fortunately, our tables almost never change. We've had like 4 changes
in a year and they were very minor.

Thanks again.
 
cbmeeks said:
I will check out NHibernate.

It is good.

Another popular O/R mapper is LLBLGen.
Fortunately, our tables almost never change. We've had like 4 changes
in a year and they were very minor.

That is very frequent change in my book. But different types
of projects means different perspectives.

Arne
 
cbmeeks said:
The existing code base is pretty good overall but we do have lots of
embedded SQL, hard-coded this, and other types of garbage floating
around.

One thing I want to do is better separate our data layers with our
business layers. I would like to do that using some type of ORM.

However, here's the deal, I want to do it by hand...ie, the hard way.
Our needs are pretty slim for now so I don't believe this to be a
major problem.
Any suggestions?

Drop the idea of doing it yourself.

Some projects has spend thousands of hours developing very good
and reliable O/R-mappers.

Some (like NHibernate) is free.

Why spend hours developing and maintaining your own ? It is
money out the window !

Arne
 
You can try here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!139.entry
downloadable code available.

I have a 2.0 version as well. (Go to the blog main page).



//p.Save();//

I actually don't like this. I think the code which creates/updates the
objects....should be seperate from the object itself.
Via a Controller or Manager class.

Check my samples... I show the Customer (has) Order(s) (has) OrderDetail(s)
example with custom business objects.

There is a "birds eye view" link to a MS article as well. Read that thing a
couple of times.
 
You can try here:http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!139.entry
downloadable code available.

I have a 2.0 version as well. (Go to the blog main page).

//p.Save();//

I actually don't like this. I think the code which creates/updates the
objects....should be seperate from the object itself.
Via a Controller or Manager class.

Check my samples... I show the Customer (has) Order(s) (has) OrderDetail(s)
example with custom business objects.

There is a "birds eye view" link to a MS article as well. Read that thing a
couple of times.

Thanks for all of the suggestions guys.

One of the biggest reasons I wanted to do most of the heavy lifting is
because I *want* to learn the most I can and I have the time to do
it. While I do have deadlines, I'm in a position where I could
literally take months or longer to get a super simplistic approach.
Which by then, I might be able to get LINQ. hahaha

I may not have worded my original post correctly. But bottom line is
that I am tired of creating stored procedures for every little thing
and managing those. I'm tired of creating embedded SQL to insert into
our many tables. There are probably a million ways this could be
better.

One obvious solution would be creating datasources/datasets and using
that. But I wanted something really light weight and tailored to our
needs.

Yesterday, I coded up something that actually worked pretty well.

It's sorta like a Model/Controller method. I have two classes:

The "Model" class is a simple C# class that has a few methods and
properties. The Save() creates the SQL and updates the db.

The "Controller" class is a simple C# class that puts some business
logic in and calls the Model.

Works pretty well.

Of course, I still have a lot to do. Things like searching, updating,
deleting, etc. But fortunately, I can get some real testing in small
areas to see how fast (if at all) I can roll out future projects using
this framework.

cbmeeks
http://codershangout.com
 
cbmeeks wrote:
Thanks for all of the suggestions guys.

One of the biggest reasons I wanted to do most of the heavy lifting is
because I *want* to learn the most I can and I have the time to do
it. While I do have deadlines, I'm in a position where I could
literally take months or longer to get a super simplistic approach.
Which by then, I might be able to get LINQ. hahaha
<....>

Whilst I admire your desire to do it yourself I think your time would be
better spent learning another ORM that you could then use in your future
projects. You could be pretty sure that whatever you develop will be
sub-par (features wise) than a commercial/open source offering
(NHibernate, LLBLGEN, LINQ).
If you did come to 6 months down the track and you've spent your time
developing your own ORM and you now have a deadline that requires
features that you cannot support, you would be well placed if you had
spent that time instead familiarising yourself with another product.

My 2c :)

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top