Results 1 to 12 of 12

Thread: object mapping

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    object mapping

    Hi all,
    I am currently discussing a project with someone and I am putting forward the idea of using a datalayer, business layer, and UI layer(s). This is how I usually approach work as I see it as a best practise. The business layer contains classes that directly map to tables in the databases, so instead of always passing datasets around, I am passing custom collections of my objects.

    As I said I use this approach usually, but when trying to explain the benefits to someone else I really struggled. I need to sumarize the benefits/drawbacks in the following areas:
    1, Development Time
    2, Security
    3, RunTime speed
    4, Maintainability
    5, Scalability
    6, Cost

    I've tried searching on Google but only really get OR mapping tools returned. Any body know any good articles, or can add info to the above points for Object Relational Mapping?

    Cheers
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: object mapping

    This is extremely wierd of you to post this... I was just having this discussion with a Java architect about this. My suggestion was to do exactly what you describe; to create an Object to represent each database concept!

    I mean, we all love to deal with Objects, why not encapsulate the database itself with Objects? Each Object could be designed with internal intelligence to auto-generate the appropriate SQL statements based on its own internal structure.

    Anyway, I believe it is the right thing to do, but he insists that this approach is naiive and will not work.

    If you are willing to pursue your idea I will discuss the development with you.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  3. #3
    Addicted Member
    Join Date
    Aug 2002
    Location
    top of the mountain
    Posts
    234

    Re: object mapping

    I have one practical question: suppose we have table named 'employee' in db, and same class employee (represent one employee), where you put method e.g. GetAllEmployees() ? This method need to return all employees from table employee. Do you put this method in employee class or make new class?

    regard j

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    Re: object mapping

    Sorry meant to reply to your post before Dave,
    I generate objects to map to the DB, but I still use stored procedures to do the CRUD stuff. The things to generate the SQL are very damm clever but I wonder how much processing time they require? You might as well use the SQLDataAdapter mighten you and just do an update?
    Sometimes my objects overlap the database tables as well so the select SP has a join in it, and fills the object with necessary data from 2 tables. This isn't the norm though.

    janis, I generally have 2 classes per object. For your example, an employee object, and an employeeCollection object. The employee object can fill itself and return it to wherever it needs to go. The employeeCollection object has a GetEmployees(parameters....) function, that returns an array list of employee objects.

    This is only my view on stuff though and may be completely wrong! I hope someone can put me on the right track if it is!

    Cheers
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: object mapping

    Quote Originally Posted by janis
    I have one practical question: suppose we have table named 'employee' in db, and same class employee (represent one employee), where you put method e.g. GetAllEmployees() ? This method need to return all employees from table employee. Do you put this method in employee class or make new class?

    regard j
    In my suggested paradigm, the Class will have a GetEmployees or Retrieve method. The Class will inheret the appropriate intelligence from a root Class of intelligent design, which will "just know" how to get the Employees, based upon Filter (WHERE clause) inputs.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  6. #6
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: object mapping

    Quote Originally Posted by nswan
    Sorry meant to reply to your post before Dave,
    I generate objects to map to the DB, but I still use stored procedures to do the CRUD stuff.
    I am suggesting to completely do away with Stored Procedures as the rule. The exception can be extremely poor performance, but I would hope it would never come to that. Again, I may be being overly-naiive.

    The things to generate the SQL are very damm clever but I wonder how much processing time they require? You might as well use the SQLDataAdapter mighten you and just do an update?
    No, not really. The proposed intelligence I refer to can do very much more. I want to add a field to the Object itself, and have it "just know" how to update the Table definition for me. Likewise remove a Field from the Object. I never again want to use Enterprise Manager, rather some special Object manager for the middle tier. The Back-end database becomes an invisible after-thought, that only needs addressing in exceptional circumstances.

    Sometimes my objects overlap the database tables as well so the select SP has a join in it, and fills the object with necessary data from 2 tables. This isn't the norm though.
    As is expected, but again I think an Object can be instructed how to "just know" about such things, as in 2 Table Obejcts, and 1 Relationship Object, which knows about the 2 Tables. This is a very rough sketch as you can tell.

    This is only my view on stuff though and may be completely wrong! I hope someone can put me on the right track if it is!
    Cheers
    Nick
    I think we left right vs. wrong about 2 miles back.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: object mapping

    In the hopes of rekindling this idea I am posting an example of what I am talking about. It's not glamorous, but if you have time I'd like you to try it out. Its a generic and intelligent Record. Since it's VB6 we use aggregation as opposed to inheritance.

    A table Object would be the consumer of the Generic Record Class and use it at Initialization to define itself (hint: at run-time).

    Here is the Record Class. Notice it auto-generates a SQL segment of the INSERT statement, to be completed by its "parent" Table.
    VB Code:
    1. Option Explicit
    2. '
    3. Private m_colFieldNames As Collection
    4. Private m_colFieldValues As Collection
    5. '
    6.  
    7. Public Sub Add(strName As String, strValue As Variant)
    8.     m_colFieldNames.Add strName, strName
    9.     m_colFieldValues.Add strValue, strName
    10. End Sub
    11.  
    12. Private Sub Class_Initialize()
    13.     Set m_colFieldNames = New Collection
    14.     Set m_colFieldValues = New Collection
    15. End Sub
    16.  
    17. Public Property Get Names_Values() As Variant
    18.     '
    19.     Dim varFieldName As Variant
    20.     Dim varFieldValue As Variant
    21.     '
    22.     Names_Values = "("
    23.     '
    24.     For Each varFieldName In m_colFieldNames
    25.         Names_Values = Names_Values & varFieldName & ", "
    26.     Next
    27.     '
    28.     Names_Values = Left(Names_Values, Len(Names_Values) - 2) ' Remove extra comma + space
    29.     Names_Values = Names_Values & ") VALUES ("
    30.     '
    31.     For Each varFieldValue In m_colFieldValues
    32.         Names_Values = Names_Values & "'" & varFieldValue & "', "
    33.     Next
    34.     '
    35.     Names_Values = Left(Names_Values, Len(Names_Values) - 2) ' Remove extra comma + space
    36.     Names_Values = Names_Values & ")"
    37.     '
    38. End Property
    39.  
    40. Public Property Get Size() As Variant
    41.     Size = m_colFieldNames.Count
    42. End Property

    Here is a sample Table. It uses the Record Class to define itself at run-time.
    VB Code:
    1. Public Sub NewIssue( _
    2.     intIssueNumber As Integer, _
    3.     strDescription As String, _
    4.     intStatus As Integer, _
    5.     intUrgency As Integer, _
    6.     strDueDate As String, _
    7.     intValue As Integer, _
    8.     intTimeEstimate As Integer, _
    9.     strContact1 As String, _
    10.     strContact2 As String, _
    11.     strContact3 As String, _
    12.     strAssignee As String, _
    13.     strAssignee2 As String, _
    14.     strDateAdded As String, _
    15.     strDateAssigned As String, _
    16.     strDateOpened As String, _
    17.     strDateResolved As String, _
    18.     strDateClosed As String, _
    19.     strDateChanged As String, _
    20.     intParentIssue As Integer, _
    21.     strGroup As String, _
    22.     strCategory As String _
    23.     )
    24.     '
    25.     m_Record.Add "IssueNbr", intIssueNumber
    26.     m_Record.Add "Description", strDescription
    27.     m_Record.Add "Status", intStatus
    28.     m_Record.Add "Urgency", intUrgency
    29.     m_Record.Add "DueDate", strDueDate
    30.     m_Record.Add "BusValue", intValue
    31.     m_Record.Add "TimeEst", intTimeEstimate
    32.     m_Record.Add "Contact1", strContact1
    33.     m_Record.Add "Contact2", strContact2
    34.     m_Record.Add "Contact3", strContact3
    35.     m_Record.Add "AssignedTo", strAssignee
    36.     m_Record.Add "SuperUser", strAssignee2
    37.     m_Record.Add "AddDate", strDateAdded
    38.     m_Record.Add "AssignDate", strDateAssigned
    39.     m_Record.Add "OpenDate", strDateOpened
    40.     m_Record.Add "ResolveDate", strDateResolved
    41.     m_Record.Add "CloseDate", strDateClosed
    42.     m_Record.Add "ChgDate", strDateChanged
    43.     m_Record.Add "ParentIssueNbr", intParentIssue
    44.     m_Record.Add "GroupID", strGroup
    45.     m_Record.Add "Category", strCategory
    46.     '
    47. End Sub
    48.  
    49. Private Sub Class_Initialize()
    50.     Set m_Record = New clsRecord
    51.     Set m_Conn = New Connection
    52.     Set m_Command = New Command
    53.     '
    54.     m_Conn.Open "Provider=sqloledb;" & _
    55.                "Data Source=SQLSERVER;" & _
    56.                "Initial Catalog=DATABASE;" & _
    57.                "Integrated Security=SSPI"
    58.     '
    59.     m_Command.ActiveConnection = m_Conn
    60.     '
    61. End Sub
    62.  
    63. Public Property Get Insert_Satement() As Variant
    64.     Insert_Satement = "INSERT INTO " & TABLE_NAME & " " & m_Record.Names_Values
    65. End Property
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: object mapping

    Wouldnt the performance suffer using collections?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: object mapping

    Run-time, or design time performance? Run-time - who cares if its10% slower than arrays (I'm being generous). Design-time - this is where I am most concerned.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    Re: object mapping

    Hiya,

    have you lot ever looked at Code Smith? We had to create a load of business object type things the other week, and this just made it a breeze! There are some good templates out there already that you can just hack together and learn off.

    I was told about it ages ago but only just downloaded it the other week. Boy do i wish i got it ages ago!

    Cheers
    Nick

    ps i know that sounds like a sales speech, but it really is good!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  11. #11
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: object mapping

    Code Smith - looks interesting. I'm not sure by looking at it how it would be useful. I guess it's something you have to try it out in order to feel the benefit. This is also true for the Record Class I posted. It really simplified my life.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: object mapping

    Here is a very nice, effective and cheap OR Mapper tool.
    http://www.ormapper.net/News/

    I have used it before and it is pretty nice. The GUI for doing the mapping could use some work but the core of the deal is pretty cool especially for only $50. I came across it while making my own OR type tool, but mine used attributes to do the mapping and I kind of like how this one uses external xml files.

    Anyway I just figured I'd bring it up.

    I could probably big up the code to mine if someone is interested, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width