none
why do we need Objects? RRS feed

  • General discussion

  • Hi All,

    I'm asking this question in a curiosity,

    This might be a simple question

    Why do we need to create object for each database fields and access them in application,

    Where as it can be done by using by fetching the Data's directly from Database and casting them to our required DataTypes.

     

    Wednesday, February 9, 2011 10:14 AM

All replies

  • I'm not sure I understand your question. Can you give an example of what you mean?
    http://pdkm.spaces.live.com/
    Wednesday, February 9, 2011 11:02 AM
  • Let me explain with example.

    I need to fetch a record from Database, lets say (name of the person),i can use the below code to get the user name

        private string UserName()
        {
          var conn = new SqlConnection("conString");
          conn.Open();
          var cmd = new SqlCommand("commandText", conn);
          return cmd.ExecuteScalar().ToString();
    
        }
    
    
    

    instead of this. Why do we need to create a class

    class UserName
      {
        string _Fname;
        string _Lname;
        public string FName
        {
          get{return _Fname; }
          set{ _Fname =value; }
        }
    
        public string LName
        {
          get { return _Lname; }
          set { _Lname = value; }
        }
       }
    
    
    

    and referring them in the application as

    string Name = UserName.Fname+UserName.LName
    
    
    

    Where as it can be done as

    string Name = UserName();
    

    Hope this is clear.

    Wednesday, February 9, 2011 11:43 AM
  • Well you don't *NEED* to but there are benefits.

    With reference to your specific question...

    Strong typing ensures the data matches what you expected you were going to read.

    Generics reduce the overhead of boxing/unboxing.

    Google for more info.

    Wednesday, February 9, 2011 12:16 PM
  • Yes you're essentially asking, 'what is all this object orientated design stuff, and is it worth it?'. That is a big question, ranging from correctly solving the business problems, fixing/changing code/model/schema. To be honest may as well just hit wiki - http://en.wikipedia.org/wiki/Object_orientated
    http://pdkm.spaces.live.com/
    Wednesday, February 9, 2011 1:57 PM
  • Hi folks,

    Do you need objects?  No. Not at all.

    Before objects came along structured code bases where large complicated balls of spaghetti code that was difficult to navigate. It was pretty much impossible to get the bigger picture oy the overall form and structure of the applications design.

    But then objects came along and they never really changed anything. Well... never really change most things. Some code bases are still big balls of spaghetti with bad design, and they are still being written now.  Objects didn't really help things move forward.

    Then again they did it!

    It just took a bit of time and some learning (that some people today still haven't bothered with).

    Time enough for large balls of mud to be created and hang around like the bad smells they contain.

     

     

    Actually let me ask the question back.

    Do you think we need objects?

     

     

    P.S. The code example looks like functional programming which is far better for multi-threaded applications than objects.


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Wednesday, February 9, 2011 3:03 PM
  • Oh,,,

    Yes if you're asking about why object orientated is good then that's a huge topic.  Why split your code up when you can put it all in one place... One huge lump of spaghetti code. 

     

    I thought the question was why use types for data access/business objects.

    The idea is that it makes things easier.

    The first time you see a type representing a table with a lot of fields you might well think there seems to be a lot of code. The thing to do is to generate that rather than write it though.

    Is this helping?

    Wednesday, February 9, 2011 3:42 PM
  • Hi Folks,

    So you mean to say, that object are used only for our convience ?

    Wednesday, February 9, 2011 4:17 PM
  • Hello,

    It all depends on the complexity of the project.

    Sometimes you may use TS (Transaction Script) to manage the database which basically means you manage the database completely in procedural manner.

    There are times that something in the middle between the procedural approach and object-oriented will do just fine and for that you can use AR (Active Record) and TM (Table Module). In fact the DataSet design is driven and based on the concept of AR and TM.

    Today, a lot of enterprise projects pushes to the DM approach with the emerge of ORM tools it makes perfect sense, database management couldn't be more easier.

    While Object-Oriented development and design is a good thing it's not the only choice, not in all cases, like many have stated here.

    I recommend the OP to read the following books.

    Microsoft .Net Architecting Applications for the Enterprise MUST be purchased with Patterns of Enterprise Application Architecture for reference.


    EDIT: for being stupid, it's a trend, it will change.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    • Edited by Eyal Solnik Thursday, February 10, 2011 1:32 PM
    Thursday, February 10, 2011 6:32 AM
  • Eyal. I don't think anyone suggested OO was the only choice??

    This is not a negative criticism, but if you are asking about "why use objects", then I would recommend you "hit the books" and look at some references about general software design before even worrying about specifics such as Enterprise Development. Perhaps someone could recommend a good introduction?


    http://pdkm.spaces.live.com/
    Thursday, February 10, 2011 1:08 PM
  • Eyal. I don't think anyone suggested OO was the only choice??

    You're right, sometimes I tend to read too briefly and the outcome is just bad, I'll try to be more careful next time, haha .. :)

    I'll edit my post.

    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Thursday, February 10, 2011 1:29 PM
  • A long tme ago in a distant... ermm.... no "just" maybe 15 years ago come to think of it.

    I used to do COBOL.

    I'd split my code up into meaningful chunks and minimise repetition.  If I had a requirement to do something in one place and something plus a bit more elsewhere I'd write a piece of code did something and then call it from another piece of code did the rest.

    If anyone had really pressed me I suppose I would have called it procedural code.

    It's kinda funny when people who clearly never wrote any COBOL tell me about the bad old days of COBOL programming.   Then I see their code.

    No doubt in 15 years people will be laughing at the bad old days of OO. 

    Most developers aren't very good.  Derek's right.  Average to poor devs turn out big lumps o mud.  I doubt any methodology will change that until someone invents one which thinks for the developer. 

    Friday, February 11, 2011 9:13 AM
  • It seems like the OP wanted to know what are the benefits of mapping relational database to objects and some of the replies are a bit off or insufficient, while in essence Object-Oriented affected everything in the development cycle and environment we're working in today, I doubt it's the kind of thing he/she expects to understand in this thread.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Saturday, February 12, 2011 9:12 AM
  • your curosity  must be in-lined with object oriented database design concept. Simple truth , "cursorily kills the cat".

    PHIJO MP
    Saturday, February 12, 2011 12:42 PM
  • "It seems like the OP wanted to know what are the benefits of mapping relational database to objects "
    Yes, Eyal i wanted to know the benefits of mapping relation Database to objects. but it was nice to read the replies and understand how it programming have changed in these years..
    Sunday, February 13, 2011 10:51 AM
  • your curosity  must be in-lined with object oriented database design concept. Simple truth , "cursorily kills the cat".

    PHIJO MP
    eh?

    http://pdkm.spaces.live.com/
    Sunday, February 13, 2011 4:42 PM
  • Hello Folks,

    Wish to hear from you,

     

    Thanks in advance..

    Monday, February 14, 2011 10:16 PM