Using reflection for ORM RRS feed

  • Question

  • I'm currently using LINQ to SQL to develop a database application and I think I'm getting myself a bit confused over the whole object relational mapping issue. I could really use some tips from people who are a bit more experienced in this area.

    Basically I've gone and developed a set of simple entity objects in my domain and set up all my units tests etc, here's a simple one for a customer with two different addresses in the record:


    	public class Customer
    		public Address MailingAddress { get; set; }
    		public Address BillingAddress { get; set; }
    	public class Address
    		public string Street { get; set; }


    I've then gone and created a database which maps each concrete class to its own table. When I run SqlMetal over it I get something like this:


    	public class CustomerDB
    		public string MailingAddress_Street { get; set; }
    		public string BillingAddress_Street { get; set; }


    My problem now is how to do I do the object relational mapping between the two? I'm currently adhering to a strict naming convention in my database and using reflection to do the mapping automatically. Granted, it's not the fastest method in the world, but it works and it's good enough for my needs.

    But am I using LINQ to SQL "correctly" by doing this? I was always under the impression that LINQ to SQL was used to generate data store objects and then you would (or rather, "should") use ORM to map the classes to persistent entities in the domain layer. But reading various articles on the web it seems that many people consider LINQ to SQL to be an ORM solution in its own right. I can see that it has basic support for ORM, but even Microsoft's 2005 DLinq overview document states it's unlikely they'll ever support anything but the most basic mapping between objects and relational tables. Apart from obvious things like inheritance etc how would you do the mapping for non-native data types like my Address class using LINQ To SQLs current functionality?

    Am I doing the right thing by using LINQ to SQL for my data access layer and keeping entities in a separate domain layer? If so then is my automated reflection code a reasonable way of doing the ORM? How do most other people go about this?


    Wednesday, November 2, 2011 2:10 AM