473,769 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Crud Design Question

Friends,

I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.

I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.

A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienced designer (like me).

A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.

My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005

I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.

Opinions please.

Thank you,

John

public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStrin g; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}
}
Mar 20 '07 #1
13 2712

You should not mix the object's code, with the code that creates the
objects.

Instead, you push that code into a Manager or Controller class.

Ex

Employee (object)
EmployeeControl ler

you should not marry the db of choice to your code, this is why people
seperate the "BusinessLo gic" layer and the DataAccess layer.

See

http://sholliday.spaces.live.com/blog/
6/5/2006
Custom Objects and Tiered Development II // 2.0

"John Kraft" <jh*****@barbec ueguy.comwrote in message
news:06******** *************** *********@4ax.c om...
Friends,

I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.

I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.

A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienced designer (like me).

A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.

My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005

I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.

Opinions please.

Thank you,

John

public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStrin g; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}
}

Mar 20 '07 #2
Sloan

I don't agree with that.

Maybe you need to learn how to write SQL Server

there is only one database that matters; and it has been this way for
10 years



On Mar 20, 4:33 pm, "sloan" <s...@ipass.net wrote:
You should not mix the object's code, with the code that creates the
objects.

Instead, you push that code into a Manager or Controller class.

Ex

Employee (object)
EmployeeControl ler

you should not marry the db of choice to your code, this is why people
seperate the "BusinessLo gic" layer and the DataAccess layer.

See

http://sholliday.spaces.live.com/blog/
6/5/2006
Custom Objects and Tiered Development II // 2.0

"John Kraft" <jhkr...@barbec ueguy.comwrote in message

news:06******** *************** *********@4ax.c om...
Friends,
I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.
I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.
A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienced designer (like me).
A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.
My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005
I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.
Opinions please.
Thank you,
John
public class Class1
{
private static string connectionStrin g;
private int id;
private string name;
public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStrin g; } set { connectionStrin g = value; } }
public Class1()
{
// TODO: Add constructor logic here
}
public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}
public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}
public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;
private int id;
private string name;
public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }
public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}
public void Load(int id)
{
// do stuff here to fill c1 from the database
}
public void Save()
{
// do stuff here to save c1 to the database
}
public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);
Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);
}
}- Hide quoted text -

- Show quoted text -

Mar 21 '07 #3
A good rule to follow, someone once said, is that when you're tempted to
create a class with Manager or Controller in the name, you're probably
starting to wander away from good object-oriented design principles.

Just something to think about.

Tom Dacon
Dacon Software Consulting

"sloan" <sl***@ipass.ne twrote in message
news:Ob******** ******@TK2MSFTN GP06.phx.gbl...
>
You should not mix the object's code, with the code that creates the
objects.

Instead, you push that code into a Manager or Controller class.

Ex

Employee (object)
EmployeeControl ler

you should not marry the db of choice to your code, this is why people
seperate the "BusinessLo gic" layer and the DataAccess layer.

See

http://sholliday.spaces.live.com/blog/
6/5/2006
Custom Objects and Tiered Development II // 2.0

"John Kraft" <jh*****@barbec ueguy.comwrote in message
news:06******** *************** *********@4ax.c om...
>Friends,

I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.

I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.

A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperience d designer (like me).

A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.

My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005

I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.

Opinions please.

Thank you,

John

public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStri ng; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}
}


Mar 21 '07 #4
Ahh... the troll is back. This time he's had a sex change.

Ignore the troll.

On Mar 20, 6:01 pm, "Susie DBA [MSFT]" <susie...@hotma il.comwrote:
Sloan

I don't agree with that.

Maybe you need to learn how to write SQL Server

there is only one database that matters; and it has been this way for
10 years

On Mar 20, 4:33 pm, "sloan" <s...@ipass.net wrote:
You should not mix the object's code, with the code that creates the
objects.
Instead, you push that code into a Manager or Controller class.
Ex
Employee (object)
EmployeeControl ler
you should not marry the db of choice to your code, this is why people
seperate the "BusinessLo gic" layer and the DataAccess layer.
See
http://sholliday.spaces.live.com/blog/
6/5/2006
Custom Objects and Tiered Development II // 2.0
"John Kraft" <jhkr...@barbec ueguy.comwrote in message
news:06******** *************** *********@4ax.c om...
Friends,
I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.
I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.
A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienced designer (like me).
A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.
My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005
I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.
Opinions please.
Thank you,
John
public class Class1
{
private static string connectionStrin g;
private int id;
private string name;
public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStrin g; } set { connectionStrin g = value; } }
public Class1()
{
// TODO: Add constructor logic here
}
public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}
public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}
public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;
private int id;
private string name;
public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }
public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}
public void Load(int id)
{
// do stuff here to fill c1 from the database
}
public void Save()
{
// do stuff here to save c1 to the database
}
public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);
Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);
}
}
Mar 21 '07 #5
On Mar 20, 3:20 pm, John Kraft <jhkr...@barbec ueguy.comwrote:
Friends,

I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.

I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.

A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienced designer (like me).

A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.

My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.

Code is C#, .NET 2.0, VS2005

I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.

Opinions please.

Thank you,

John

public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStrin g; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}

}

public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}

}

public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}

}
One immediately obvious problem is that if you make the data access
methods static then you can't take advantage of polymorphism. At
least, not in C#.

Mar 21 '07 #6
bob
Hi,
I think the flexibility of the datalayer should be considered
valuable.
I recently had a mature project that was based on SQlServer and we had
to deploy it on SQLAnywhere.
I altered the datalayer to use a data factory and with a workable
amount of modification the Application was up and away.
Hate to think of the effort involved if I had to rummage around in
the business objects.
To me it comes down to what the app is going to do. If it is quick and
dirty then I'll use data aware objects straight on the GUI if I can
get away with it, but otherwise I stick with 3 tier.
Bob
Mar 21 '07 #7
Hi John,

some ideas:
since you will use only one database (atleast not one database per class),
you could put the connectionstrin g in a single class (maybe static class),
instead of making it a property of each class.

The Load method shouldn't use a ref-parameter, better is to return the
created instance as return-value.
(If you still want to use a parameter, it should be an out parameter. Then
you don't have to assign a value, wich will be overidden anyway.)

Maybe you should provide a private constructor, wich is called by the Load
method. The public constructor can remain for the object creation. But also
a Create-method could be possible.

The Id should be managed by the business-layre (or below). So atleast the
setter of the Id should be private.

Think of a way to load a list from the database. Maybe with the option of
deferred loading, but that's not easy to implement.

Another option in general is to use an OR-Mapper. They do much of the work
for you and work better than anything, you can develop in a short time.

HTH
Christof

"John Kraft" <jh*****@barbec ueguy.comschrie b im Newsbeitrag
news:06******** *************** *********@4ax.c om...
Friends,

I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.

I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.

A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienced designer (like me).

A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.

My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005

I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.

Opinions please.

Thank you,

John

public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStrin g; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}
}

Mar 21 '07 #8

"Tom Dacon" <td****@communi ty.nospamwrote in message
news:ut******** ******@TK2MSFTN GP03.phx.gbl...
>A good rule to follow, someone once said, is that when you're tempted to
create a class with Manager or Controller in the name, you're probably
starting to wander away from good object-oriented design principles.
Well, in this case the additional class in question would be a Persister,
and other aspects of control/management should also have their own classes.

Still, applying dependency inversion by splitting out the database access
code with a well-defined interface is valuable in such a situation, to
enhance testing, reusability, and breakage from changing the data layer.
>
Just something to think about.

Tom Dacon
Dacon Software Consulting

"sloan" <sl***@ipass.ne twrote in message
news:Ob******** ******@TK2MSFTN GP06.phx.gbl...
>>
You should not mix the object's code, with the code that creates the
objects.

Instead, you push that code into a Manager or Controller class.

Ex

Employee (object)
EmployeeContro ller

you should not marry the db of choice to your code, this is why people
seperate the "BusinessLo gic" layer and the DataAccess layer.

See

http://sholliday.spaces.live.com/blog/
6/5/2006
Custom Objects and Tiered Development II // 2.0

"John Kraft" <jh*****@barbec ueguy.comwrote in message
news:06******* *************** **********@4ax. com...
>>Friends,

I'm working on some crud stuff, and I was looking for opinions on the
subject. Below, I have pasted some VERY simple sample code. Class2
is a "traditiona l" crud type object. In a real example, these objects
would probably implement some kind of ICrud interface with the common
methods.

I'm finding that many times, though, I want to use these objects as
simple data objects, and I don't need all the database functionallity.
I don't want to have to worry about setting connections and such; and
all I hate seeing all the crud methods when I only want to set
properties.

A more common solution would be to have simple data objects that are
passed to a data access class/layer/other-buzzword. However, that
approach can get messy when dealing with tons of objects and an
inexperienc ed designer (like me).

A thought I came up with was to "blend" the two concepts. Essentially,
leave the data access in the object, but make the methods all static;
like Class1. This has the benefit of allowing me to treat the object
as a simple data object, yet leaving me full data accessibility
without having to instantiate/reference a bunch more classes.

My question is, is there anything fundamentally wrong with the Class1
approach? Is there anything that is going to really bite me in the
rump-roast? It seems to work, and I haven't had a problem with it
yet; but I am very new to design from scratch.
Code is C#, .NET 2.0, VS2005

I should probably also note that I am simply playing around with
design concepts. This isn't for anything "real" right now.

Opinions please.

Thank you,

John

public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStr ing; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}
}



Mar 21 '07 #9
On Wed, 21 Mar 2007 09:03:02 +0100, "Christof Nordiek" <cn@nospam.de >
wrote:
>Hi John,

some ideas:
since you will use only one database (atleast not one database per class),
you could put the connectionstrin g in a single class (maybe static class),
instead of making it a property of each class.

The Load method shouldn't use a ref-parameter, better is to return the
created instance as return-value.
(If you still want to use a parameter, it should be an out parameter. Then
you don't have to assign a value, wich will be overidden anyway.)

Maybe you should provide a private constructor, wich is called by the Load
method. The public constructor can remain for the object creation. But also
a Create-method could be possible.

The Id should be managed by the business-layre (or below). So atleast the
setter of the Id should be private.

Think of a way to load a list from the database. Maybe with the option of
deferred loading, but that's not easy to implement.

Another option in general is to use an OR-Mapper. They do much of the work
for you and work better than anything, you can develop in a short time.

HTH
Christof
Christoff (and othesr that have answered my question),

I did the load method that way on purpose. I had a counterpart to it
in there called Create() that did what you are saying, but I removed
it because it didn't really fit into what I was trying to do; even if
it is a better design. (see more on this later)

Essentially, I was trying to make the classes easier to use. Most of
the time when we use objects like this, we simply use them as data
object; that is, we don't do database ops on them. The Load method
was intended to fill an instance that already existed. My goal was
to allow something like this...

Class1.Connecti onString = connectionStrin g;
Class1 c1 = new Class1();
c1.F1 = 5;
c1.F2 = 6;
.... etc
Class1.Save(c1) ;
If I were to use the Controller method that was suggested by others,
it would look something like...

Class1Controlle r class1Controlle r = new
Class1Controlle r(connectionStr ing);

Class1 c1 = class1Controlle r.Create();
c1.F1 = 5;
c1.F2 = 6;
class1Controlle r.Save(c1);

At cursory glance, the Controller option only looks like slightly more
typing. However, if I have 50 data object classes that are all
intereacting and I'm dealing with, my overhead just went from 50 -->
100 classes I gotta deal with. That's gonna make my app look like a
Los Angeles Expressway at rush hour. Even if I make the Controller
classes themselves static, I'll remove the constructor but have to add
in a property to set the connectionstrin g.

It's going to be especially bad, if my classes are members of another
class... like a property of a Vendor object might be a VendorContact
object that has a property of a VendorContactTy pe object.

It could very well be my inexperience, but I've been looking into
several different design ideas; and they all seem so convoluted and
overblown for anything I've ever done. I do appreciate the comments
by everyone though.
Back to the Load method you mentioned. That's just one of my quirky
Idiosyncrasies. I'm a huge believer that a method should do ONLY what
it says it's going to do...

Create should simply create.
Load should simply load, not create and then load.
Save should simply save, not save and then refresh/reload.
etc.

I hate stuff that happens "by magic." A good example is a method I
once ran into called "PrintPO." Any guesses as to what it did? I'll
tell ya. It printed the po, faxed the po to the vendor, emailed to
the vendor, updated the database, emailed a group of people a
notification, and a few other minor things. I simply wanted to print
a copy of the po to look at, and the next thing I knew I had ordered
several thousand dollars worth of merchandise. Oops.
Thanks for the input,

John



>>
public class Class1
{
private static string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public static string ConnectionStrin g { get { return
connectionStri ng; } set { connectionStrin g = value; } }

public Class1()
{
// TODO: Add constructor logic here
}

public static void Load(ref Class1 c1, int id)
{
// do stuff here to fill c1 from the database
}

public static void Save(Class1 c1)
{
// do stuff here to save c1 to the database
}

public static void Delete(Class1 c1)
{
// do stuff here to Delete c1 from the database
}
}
public class Class2
{
private string connectionStrin g;

private int id;
private string name;

public int Id { get { return id; } set { id = value; } }
public string Name { get { return name; } set { name = value; } }
public string ConnectionStrin g { get { return connectionStrin g; }
set { connectionStrin g = value; } }

public Class2(string connectionStrin g)
{
//
// TODO: Add constructor logic here
//
this.connection String = connectionStrin g;
}

public void Load(int id)
{
// do stuff here to fill c1 from the database
}

public void Save()
{
// do stuff here to save c1 to the database
}

public void Delete()
{
// do stuff here to Delete c1 from the database
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();

Class1.Connecti onString = "connection string stuff here";
Class1 c1 = new Class1();
Class1.Load(ref c1, 42);

Class2 c2 = new Class2();
c2.ConnectionSt ring = "connection string stuff here";
c2.Load(42);

}
}
Mar 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1488
by: andy2O | last post by:
Hello comp.lang.py, Can you help me with ideas for the following (somewhat newbie) OO design question in Python? Note, I'm using psuedo-code, not actual Python for the examples! Background: ----------- I need to represent a small variety of mathematical constructs symbolically using Python classes.
0
1922
by: James Walters | last post by:
Hello, DB novice checking in here with a basic design question. I have a table called 'nms_apps' which stores information about all of our applications which we have developed/maintained for our client. One column which I would like to use is called 'used_by', which would store information about which business sections (Financial Management Branch, Human Resources Branch, etc.) use a particular application. Often
0
1386
by: Krist | last post by:
Hi All, I have a database design question, pls give me some help.. I want to define tables for salesman's sales target commission . The commission could be given per EITHER sales amount of : Group of Products OR Group of Brand. e.g : the data example : For one salesman_A : product_1, product_2, product_3 etc.. => sales = $100 - $200 =>
1
3281
by: Krist | last post by:
Hi All, There is some additional info I forget on this same topic I just posted. I have a database design question, pls give me some help.. I want to define tables for salesman's sales target commission . The commission could be given per EITHER sales amount of : Group of Products OR Group of Brand. e.g : the data example : For one salesman_A : product_1, product_2, product_3 etc.. => sales = $100 - $200 =>
1
2054
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able to kick off the process and continue to work in the appliaction while getting progress updates and the ability to cancel. The method that seems easiest to me is this: The class exposes certain events for progress. Start, ProgressUpdate, and...
3
1871
by: reageer | last post by:
Hi all, I have a design question: I have a bunch of users (name, address, zip, etc.). They are assigned a card with a specific id. The only thing unique is this card id, or probably the combination of all other user fields. So it's seductive to use the card id as the primary key. This card allows access to certain places and all access is logged.
29
2228
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
9
1672
by: fjm | last post by:
Hey everyone, I lost my internet connection for about 5 months and finally got it back. This is one of the first places I came back to. I have to say that I had a heck of a time finding it because of the name change and facelift. :) Anyway, good to be back. I have a question that may be more of a design question. If this post doesn't belong here, I appoligize in advance, feel free to move it. I need to create a form that will be...
2
2538
by: RoaringChicken | last post by:
Hi. Vista Ultimate Access 2007 I'm developing an inventory database and have question on design. The database stores collection details. One item in the collection, one record. The design question is about showing related items. For example: I have a typewriter Model 1 I have a manual for typewriter Model 1 I have a manual for typewriter Model 2 but no typewriter model 2. What I would like is that when the record for typewriter...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.