Re: Is this correct OO design?
- From: "Nick Malik [Microsoft]" <nickmalik@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 15 Apr 2005 07:29:24 -0700
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/default.asp?url=/library/en-us/cpguide/html/cpconOptimisticConcurrency.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.
--
<cmo63126@xxxxxxxxx> wrote in message
news:1113572388.347120.149310@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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.
>
.
- Follow-Ups:
- Re: Is this correct OO design?
- From: cmo63126
- Re: Is this correct OO design?
- References:
- Is this correct OO design?
- From: cmo63126
- Is this correct OO design?
- Prev by Date: Create Table in SQL server (Visual basic.Net )
- Next by Date: Re: Create Table in SQL server (Visual basic.Net )
- Previous by thread: Is this correct OO design?
- Next by thread: Re: Is this correct OO design?
- Index(es):
Relevant Pages
|