1

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.

Arseni Mourzenko
138k32 gold badges355 silver badges540 bronze badges
asked Apr 2, 2013 at 11:52
2
  • 2
    Might help to amend the question so that we know what legacy database format you're using. Commented Apr 2, 2013 at 12:14
  • It's a sql database, but the business components are being phased out so column names and datatypes are about to be changed. Commented Apr 2, 2013 at 12:38

2 Answers 2

0

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.

answered Apr 2, 2013 at 13:30
0
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

answered Apr 2, 2013 at 13:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.