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.
2
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.
0
I haven’t used EF much, but have used NH quite a bit.
NH typically works on a model (or entity if you prefer) per table basis. That is, there is one class per table in the database. Most ORMs have a way to map database types to C# types. For example, it would be possible to convert a varchar(max) column which contains ‘true’ and ‘false’ to a bool in a C# class.
You can also take this one step further: If your business model relies on Customers (the C# class), but the data is in one or more DB tables, you can use combine those mapped classes into the one model (CustomerInfo + AddressInfo = CustomerClass).
Creating and Testing a Custom NHibernate User Type