473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Constructor Design

Hi,

I have a class that needs to retreive data based on a id passed to.
Currently, I have designed the constructor to take the id as a parameter and
then connects to my DAL which retrieves data from my db. Is this good
design?

Thanks
Oct 16 '06 #1
2 1141
"Randy" <rh**@vic.comwrote in message
news:ea**************@TK2MSFTNGP04.phx.gbl...
I have a class that needs to retreive data based on a id passed to.
Currently, I have designed the constructor to take the id as a parameter
and then connects to my DAL which retrieves data from my db. Is this good
design?
That depends on whether your class is supposed to represent a single data
object connected to the ID. That is, it's technically possible to add
something to the class so that you can alter the ID and retrieve a different
data object, but IMHO if you find yourself doing that, then including the ID
as part of the object construction sort of breaks the logical abstraction
that your class theoretically represents.

That said, it's not a black and white issue. If you prefer an optimization
in which you construct the object with the initial ID already in place,
there's nothing technically wrong with that. I just think it devalues the
abstraction the class gets you in the first place.

If the class will ever only be used to retrieve that one data object
connected to the ID, then yes...I think it makes a lot of sense to pass the
ID into the constructor. In that case, passing the ID reinforces the
abstraction being used, rather than weakening it.

Pete
Oct 16 '06 #2
Randy,

Well, if you take the advice of the Framework Design Guidelines book
literally then no, it would not be a good design. The authors say to
do minimal work in a public constructor. I happen to apply that rule
more literally than most because I see no compelling reason not to. If
the semantics of the object require that it always be in a state that
is derived from a database call then create a static factory method on
the class that returns such an object. You can give the factory method
a name that implies that a call to the database is occurring (ie. Load,
Fetch, Select, etc).

Brian

Randy wrote:
Hi,

I have a class that needs to retreive data based on a id passed to.
Currently, I have designed the constructor to take the id as a parameter and
then connects to my DAL which retrieves data from my db. Is this good
design?

Thanks
Oct 16 '06 #3

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

Similar topics

6
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
21
by: Steve - DND | last post by:
I know it's possible to require inheriting classes to implement a particular function or property, but is it possible to require a inheriting class to implement a constructor of it's own? ...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
5
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
11
by: Rimpinths | last post by:
I'm new at developing user controls in C#, and one thing I've noticed right off the bat is that the constructor gets called twice -- once at design time, once at run time. In short, I'm trying...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.