Re: Is this correct OO design?



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.
>


.



Relevant Pages

  • Is this correct OO design?
    ... //Set properties of Article Object with values from Database ... IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS ... Article OriginalArticle = new Article; ... public static void GetArticleData ...
    (microsoft.public.dotnet.general)
  • "Meatless MVC" seems success but new problem ahead.
    ... I've decided to completely revamp the design of my toy database program. ... #ifndef APPLICATION_H ... void startEng; ...
    (alt.comp.lang.learn.c-cpp)
  • Last design issues to be solved.
    ... I've decided to completely revamp the design of my toy database program. ... #ifndef APPLICATION_H ... void startEng; ...
    (comp.object)
  • Re: design quicky
    ... >>I have a design problem that occurs fairly often. ... >> void f ... not the responsibility of the client. ... clients can define the functionality of the map at runtime. ...
    (comp.object)
  • Re: ADO and ADO.net
    ... What is pessimistic and optimistic concurrency? ... Cor Ligthert [MVP] wrote: ... need to design application that using SQL server? ... However for your design is probably only important that ADO is mostly done ...
    (microsoft.public.dotnet.framework.adonet)