CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    Data Table to Business Object Mapping.

    In the past while I have looked into several techniques adopted for mapping relational data to business object. In O/R mappers, there seems to be two main methods, using XML files or Attributes.

    I am just curious, why not just use static methods?

    Code:
    public interface IBaseBusinessObject
    {
          string GetTable();
          Dictionary<string,string> GetMapping();
    }
    
    public class MyBusinessObject : IBaseBusinessObject
    {
          static string tableName = "MyTableName";
          static Dictionary<string, string> mapping = new Dictionary<string,string>
          {
               {"PropertyA", "ColumnA"},
               {"PropertyB", "ColumnB"},
               {"PropertyC", "ColumnC"}
          }
    
          public static string GetTable()
         { 
               return tableName;
         }
    
         public static Dictionary<string,string> GetMapping()
         {
              return mapping;
         }
    }
    To me it seems to make more sence mapping using this method then having to deal with reading attributes. But, I have yet to see an O/R Mapper implemented in this way and I am wondering why not?

    Any thoughts?

    Mike B
    Last edited by MikeB; April 15th, 2009 at 08:49 AM.

  2. #2
    Join Date
    Feb 2001
    Posts
    2,455

    Re: Data Table to Business Object Mapping.

    Ok, so two problems with the above:

    1) Cannot implement interface methods as static.
    2) My dictionary creation as shown will not compile. I did see an example like this, but.....

    Mike B

  3. #3
    Join Date
    Jul 2008
    Posts
    70

    Re: Data Table to Business Object Mapping.

    I'm not sure about .NET but with Hibernate on Java you use annotations for the mapping.

    Looks something like this


    class Foo
    {
    @Id
    @Generated
    private int Id;

    }

    This keeps the mapping with the code and I find it pretty useful when compared to the alternative xml mapping.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Data Table to Business Object Mapping.

    This is java's equivalent of attributes.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Data Table to Business Object Mapping.

    Quote Originally Posted by MikeB View Post
    2) My dictionary creation as shown will not compile. I did see an example like this, but.....

    The example is OK, it is syntaxt of initializer introduced in C#3.0
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Data Table to Business Object Mapping.

    Why not use static methods? Because they are not flexible. You can use them for your proprietary solution, but they are not suitable for frameworks, where high flexibility is the must. (Personally, I try to avoid static methods at all). I think that XML files approach is the best because 1. language independant (fits C# as well as VB.NET), 2. it can be easily created and processed by third party tools.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

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

Featured