473,385 Members | 1,620 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,385 software developers and data experts.

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.GetArticleData(this, articleID);
}

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

Article OriginalArticle = new Article(this.articleID);

//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...Perform 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(object sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write("<br>MyArticle.Title: "+MyArticle.Title);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Update();
}
}

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

Jul 21 '05 #1
4 1479
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.googlegr oups.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.GetArticleData(this, articleID);
}

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

Article OriginalArticle = new Article(this.articleID);

//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...Perform 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(object sender, EventArgs e)
{
Article MyArticle = new Article(2112);
Response.Write("<br>MyArticle.Title: "+MyArticle.Title);
MyArticle.Title = "New Title from MyArticle object instance";
MyArticle.Update();
}
}

results:
MyArticle.Title: Original Title from Database
Title doesn't match current Title in Database...Perform 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*********************@g14g2000cwa.googlegro ups.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
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...
17
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...
5
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
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...
9
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...
7
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...
7
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...
2
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...
27
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></...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...
0
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,...

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.