C# ORM – Mapping Legacy Databases Using Wrapper Classes

cdatabaseentity-frameworklegacyorm

I want to use an ORM with a legacy database, but I don't want to expose some of the underlying data types. For example, some of the columns are nullable doubles or floats and I want my domain model to use non nullable decimals.

It seems to be impossible to have Entity Framework automagically cast between these types, and maybe for good reasons. I also want to be able to use enums and so far I have not found any method that don't look like ugly hacks to make this possible.

My idea is simply to use wrappers, where I have table-like classes mapped to the database but wrap them in "real" domain classes for use in code. My question is simply if you have any thoughts about this approach or if you have any tips. I have no experience in mapping a legacy database to an ORM so any comments or suggestions are appreciated.

Note: I've only looked at Entity Framework so I have no knowledge about the capabilities of other ORMs, such as NHibernate.

Best Answer

I'd wrap your SQL data access calls as stored procedures then, you can expose a different datatype in the sproc results, casting within the procedure if required.

Then your data access becomes like an API, you won't need all the nasty ORM mapping stuff, and just use it to read and write the "DB API" keeping all the internal structure of the DB hidden.