2

I'm just starting out in MVC world, and I've used a standard MVC template that's built into VS 2010. I've got a couple of controllers and views hooked up, and now I need to get to my database.

I've got an existing SQL Server 2005 database that is quite large, already defined by a 3rd party company. I'm trying to bolt on a reporting/admin interface to it for our specific company needs.

So, I'd like, initially to just pull a list of things out of a few tables from this DB. So...using MVC 3, what are some ORM tools that integrate nicely?

I'm also trying to use a Test Drive Design approach. I'm not sure what to do for tests that would require insert/update/delete of data. Is that were "Mocks" come into play?

Matt Dawdy
  • 19,247
  • 18
  • 66
  • 91
  • To help in choosing an ORM (as jfar said, any ORM should work with MVC): http://stackoverflow.com/questions/1377236/nhibernate-entity-framework-active-records-or-linq2sql/ – Michael Maddox Feb 08 '11 at 12:01
  • Wow, your answer to that question was great. Thanks for pointing me to it. I think I've read close to 50 SO questions on this topic, but hadn't reached that one yet. Thanks for the pointer. – Matt Dawdy Feb 08 '11 at 14:51

2 Answers2

4

Every ORM integrates nicely with Asp.net MVC. There is nothing in asp.net mvc that would make ORM integration hard.

Your biggest hurdle is using a legacy database. NHibernate and Entity Framework 4 are the only two free ORMs I'm aware of that map to legacy databases well. EF4 isn't too bad at mapping to legacy databases it just works better with green field development. In contrast NHibernate can map almost any scenario you can think of.

Testing ease is going to be mostly dependent on which data access pattern to use. The Repository pattern is popular because of how friendly it is to test with. No mocking is required.

John Farrell
  • 24,673
  • 10
  • 77
  • 110
1

I strongly suggest you to use the Castle components: Windsor, Dynamic Proxy and Active Record. You can basically mock the data since Active Record creates a model with attributes that allow you to manipulate it manually or through Dynamic Proxy's interceptors. Wrap up repositories and data services to gain more control of your data access.
Use the specifition pattern where applicable to allow flexiable querying easily.
Use Linq with Active Record in your data services in order to be able to pass IEnumerable<T> or specifications (that may or may not wrap NHibernate's abstract criteria and convert them to a DetachedCriteria or may contain the DetachedCriteria or an HQL or whatever you are encapsuling) as queries.
That way you can mock your database access easily and refactor and test more easily.

the_drow
  • 18,571
  • 25
  • 126
  • 193
  • ActiveRecord is much much harder to test than something like the Repository pattern. You can't say something is "easy to test" and then start talking about dynamic proxy interceptors. Jeeze. – John Farrell Feb 07 '11 at 19:00
  • @jar: I strongly disagree. Generating data for active record is much much easier than using NHibernate directly. – the_drow Feb 07 '11 at 19:02
  • I'm confused are we talking about physical or in-memory testing. With physical testing all you need to do with any ORM is create a bunch of new Person(){FirstName="test"} and persist them. If we are talking in memory all you need with a Repository is do the same thing. No hooking into interceptors required. Anytime your classes are tied directly to persistence in a static language testing is harder. This is simply fact and the reason why patterns such as the Repository exist. – John Farrell Feb 07 '11 at 19:32
  • @jar: Interceptors help overriding existing implementation and providing calcualtions over properties. Through Windsor it is possible to use an interceptor and selector to select the right properties in the model to override the default/persisted value or provide an alternative calculation. It can even provide wrong data. A repository can do the same but with much less flexability to fields. – the_drow Feb 07 '11 at 22:27