473,781 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this correct OO design?

I'm not sure if this is bad design or not. It seems flawed, but I'm not
sure. Is it wrong to create an instance of a class within the class
itself? Any feedback appreciated.

public class Article
{
int articleID;
string title;

//Constructor: Load Data from DataAccess Class
public Article(int articleID)
{
this.articleID = articleID;

//Set properties of Article Object with values from Database
DataAccess.GetA rticleData(this , articleID);
}

public void Update()
{
/*
IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS
(OriginalArticl e) WITHIN THE CLASS ITSELF(Article) ?
*/

Article OriginalArticle = new Article(this.ar ticleID);

//check to see if the new and origianal values match
if(this.title != OriginalArticle .Title)
{
Response.Write( "<br>Title doesn't match current Title in
Database...Perf orm Important Operation Here.");
}
else
{
Response.Write( "<br>Title hasn't changed.");
}
}

public string Title
{
get{return title;}
set{title = value;}
}
}

public class DataAccess
{
public static void GetArticleData( Article article, int ArticleID)
{
//load database data, populate Article object from parameter
article.Title = "Original Title from Database";
//article.Field1 = ...
//article.Field2 = ...
//etc...
}
}

public class _Default : Page
{
private void Page_Load(objec t sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write( "<br>MyArticle. Title: "+MyArticle.Tit le);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Updat e();
}
}

results:
MyArticle.Title : Original Title from Database
Title doesn't match current Title in Database...Perf orm Important
Operation Here.

Jul 21 '05 #1
4 1497
This is NOT bad design. It is not particularly efficient, but there is no
magical OO principle that is violated.

In fact, optimistic concurrency mechanisms often do something similar
(usually in the DB layer) by reading the matching db record first, before
updating the row, to check if the db value is different from an expected
value, either in all of the columns, or in a timestamp column.

Is that what you are trying to build logic for? A condition where two
people have opened a record, and modified it, and one has saved their
changes (to the title in your example)? If so, you may want to take a close
look at the timestamp data type in SQL Server, and Optimistic Concurrency
with Row Versioning.

This link may help.
http://msdn.microsoft.com/library/de...oncurrency.asp
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
<cm******@yahoo .com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I'm not sure if this is bad design or not. It seems flawed, but I'm not
sure. Is it wrong to create an instance of a class within the class
itself? Any feedback appreciated.

public class Article
{
int articleID;
string title;

//Constructor: Load Data from DataAccess Class
public Article(int articleID)
{
this.articleID = articleID;

//Set properties of Article Object with values from Database
DataAccess.GetA rticleData(this , articleID);
}

public void Update()
{
/*
IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS
(OriginalArticl e) WITHIN THE CLASS ITSELF(Article) ?
*/

Article OriginalArticle = new Article(this.ar ticleID);

//check to see if the new and origianal values match
if(this.title != OriginalArticle .Title)
{
Response.Write( "<br>Title doesn't match current Title in
Database...Perf orm Important Operation Here.");
}
else
{
Response.Write( "<br>Title hasn't changed.");
}
}

public string Title
{
get{return title;}
set{title = value;}
}
}

public class DataAccess
{
public static void GetArticleData( Article article, int ArticleID)
{
//load database data, populate Article object from parameter
article.Title = "Original Title from Database";
//article.Field1 = ...
//article.Field2 = ...
//etc...
}
}

public class _Default : Page
{
private void Page_Load(objec t sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write( "<br>MyArticle. Title: "+MyArticle.Tit le);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Updat e();
}
}

results:
MyArticle.Title : Original Title from Database
Title doesn't match current Title in Database...Perf orm Important
Operation Here.

Jul 21 '05 #2
No it's not a concurrency-related issue. I have a news portion of my
web app. where each news article has the potential for a document
library associated with it. if the user creates a press release and
wants to upload related documentation, they have the ability to do so.

there's 3 areas that potentially must be modified when an article has
changed:

1. Articles Table: housing all data related to article itself
Fields include: (title, summary, body, opendate, closedate,
DepartmentName, DocLibraryID (see 2.), etc...

2. DocLibrary Table: housing all documents (if any) associated with a
given article.
Fields include: (ArticleID, FilePath, other irrelevant fields)

3. Physical Directory where docs are stored.

when the article is updated - I assuming it's the proper way of doing
so - I start my changes from the bottom up in steps 3-2-1 fashion.

I my example I need to check if the user has modified the article to
display under a different department or has changed its display date.
This will effect the physical path to the article's document library.

for example:
Original article: Created for "Public Relations" on January 31, 2005
path: /Docs/News/PublicRelations/2005_01_31/

Updated article: Moved to "General News" on April 15, 2005
new path: /Docs/News/GeneralNews/2005_04_15/

Jul 21 '05 #3
Hello cmo63126,

In early days of database development, it was common for the primary key of
a record to be derived from the values in the record. So, for example, the
ID of a customer John Smith who lives on 123 Main St may be JOSMI12X999 with
the last four characters used to create a unique id between this record an
the record of Joe Smart at 123 Elm.

It quickly became apparent that there's a problem with this: What happens
if John Smith moves from 123 Main to 765 Willow? Do you change his ID? and
doesn't that mean that you have to find all the records linked to his ID and
change them too?

You have created the EXACT same problem for yourself. You have tied the
location of the document to a variable value in a database record, which
means if the value changes, you have to change the location... and you are
paying for it by creating a needlessly complex design to cope.

Strong suggestion:
Do not place files in a fileshare that has anything at all to do with the
department. Create a tool for placing files in the site doc library.
First, get a unique and arbitrary value for the id of your document. (if it
is random, like a GUID, you can do interesting things like create
subdirectories based on parts of the ID). For simplicity sake, let's say
you put 100% of you files in a single "documents" folder on your site. Your
tool will rename the file to the ID (keep the extension), and place it into
the Documents folder. In your documents table, put in metadata, like the
original file name, date imported, and the user id of the document owner
(assuming you want to eventually find the document, or delete it, or replace
it with a new version).

Now, when the user changes the department of the news item, your document
doesn't move. You also get the advantage that a document cannot easily
overwrite another document because the name happens to be the same (common
in sales or marketing organizations, where most documents start with a
template).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
<cm******@yahoo .com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
No it's not a concurrency-related issue. I have a news portion of my
web app. where each news article has the potential for a document
library associated with it. if the user creates a press release and
wants to upload related documentation, they have the ability to do so.

there's 3 areas that potentially must be modified when an article has
changed:

1. Articles Table: housing all data related to article itself
Fields include: (title, summary, body, opendate, closedate,
DepartmentName, DocLibraryID (see 2.), etc...

2. DocLibrary Table: housing all documents (if any) associated with a
given article.
Fields include: (ArticleID, FilePath, other irrelevant fields)

3. Physical Directory where docs are stored.

when the article is updated - I assuming it's the proper way of doing
so - I start my changes from the bottom up in steps 3-2-1 fashion.

I my example I need to check if the user has modified the article to
display under a different department or has changed its display date.
This will effect the physical path to the article's document library.

for example:
Original article: Created for "Public Relations" on January 31, 2005
path: /Docs/News/PublicRelations/2005_01_31/

Updated article: Moved to "General News" on April 15, 2005
new path: /Docs/News/GeneralNews/2005_04_15/

Jul 21 '05 #4
heh. seems obvious now.

thanks, you've been a big help.

Jul 21 '05 #5

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

Similar topics

0
2158
by: Philip Meyer | last post by:
1) setting image as Background with alignment in center not working..help Explanation : i need to set a image as background and also it should be dispalyed in the center.i am using apache fop 0.25 Problem : i am using apache fop 0.25 and i used the below to set a gif as background and alignment to be center...
17
2417
by: eric.nave | last post by:
What is the correct way to find all the form elements in a particular div? I'd like to be able to loop through them and disable them. For example: <form> <div id="div1"> <input type=text name="a"> <input type=text name="b"> </div>
5
1874
by: mm nn | last post by:
Hi, I want to create a table like this: ID Autonum Datefld Date Cat Text Itm Text tCount Number
5
1618
by: Adfra | last post by:
Hi, Situation is: Used an embedded query which was "optimized" be Access with Brackets ("()" -> "."). Now problem is: I get the error "Syntaxerror in From part" in normal view as well as in the design view. When I click ok -> Error and Window close.
9
18565
by: Mike Bridge | last post by:
I am using MS Access 2003, and I typed in a query in SQL view which was working fine. Now when I open it, it gives me the error "Join expression not supported". Apparently, Access "fixed" it for me. Unfortunately, I can't correct it because when I click "OK", the window disappears. I don't see any way to open the query directly in SQL view, and both the Design and Data views exit as soon as I close the error dialog. Is there any way...
7
1818
by: WildHare | last post by:
If I have a class and I add it to an ArrayList and then want to access that class using using the index operator (e.g. ArrayList) the ArrayList returns a type "Object". I can cast the return to the correct type (my class) but that will lead to very convoluted calls to get embedded elements or to call methods. For example: I have a class called "Field"
7
1935
by: phal | last post by:
Hi I think there are many different browsers to browse to the Internet, how can I write the javascript to identify different browser and display according to the users. Some browser disable the javascript by default or by the user, how can i solve this problem if the javascript is disable. thank you
2
2301
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and ChildB) derived from class Base. Now i want to create an object, but i don't want to know which class to instantiate.
27
2839
by: pamela fluente | last post by:
Hello, for the following code, VS suggests that construct (width= height= ) is out of date and a newer one is recommended: <table><tr><td width="92" height="38" valign="middle">AnyThing</td></ tr></table> Would you please tell me the right construct (clearly must be functionally equivalent).
0
9639
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
9474
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
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
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
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
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
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.