1

I have a legacy application which contains a Class called CustomerInvoice. I need to relate these items to database tables which have less than friendly names (for example there is CUSTOMER_INV_HEAD, CUST_INV_LINES, CUST_INV_POSTS, and potentially many others).

First attempt was inheriting interfaces for the internal names and then use getters and setters to place the correct data in respective tables, the issue here is that the logic lives in the wrong layer of my application, and it all needs to be public which is not desirable - nevermind the maintenance aspect!

I have read lots about ORM and thought that AutoMapper may work but most of the examples that I have seen demonstrate where the names either side are the same. I also reviewed NHibernate which seems similar.

I need to do something like this:

public class CustomerInvoice 
{
    public string id; // lives in CUSTOMER_INV_HEAD
    public decimal netTotal; // lives in CUSTOMER_INV_HEAD called NET_INV_TOTAL
    public IEnumerable<InvoiceLine> Lines; // collection of CUST_INV_LINES but including the id
}

So what i need to be able to do is say...

List<CUST_INV_LINES> l = Map<CustomerInvoice.Lines, CUST_INV_LINES>();

But how can i define that relationship in a clever, maintainable way without hand coding the relationships in special methods?

Dominic Cotton
  • 789
  • 10
  • 34
  • 1
    you can map properties with different names though with automapper: http://stackoverflow.com/questions/14777601/how-to-specify-mapping-rule-when-names-of-properties-differ – thumbmunkeys Oct 06 '16 at 12:01

1 Answers1

0

You can add Attributes to your class properties to give their underlying database names. This is the approach taken by the frameworks XML serializer. You then only need one "special method" to extract the database name attribute from the property of a class, via reflection. Note on the following example link the override of the property name with an XML Element name - you could do the same thing for your database mappable properties with your own custom attribute.

https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx

PhillipH
  • 6,182
  • 1
  • 15
  • 25