Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to use NHibernate for the official "ASP.Net MVC 3 Getting Started"-Tutorial

Clarified Updated Question - Start


In the official MVC 3 Getting Started-tutorial it seems to me that all we have to do to get ORM working are two steps.

First adding the simple MovieDBContext-code as described at the end of part 4 ..

public class MovieDBContext : DbContext 
{
    public DbSet<Movie> Movies { get; set; } 
}

.. and second in the beginning of part 5, with a simple right-click on the Controllers folder we can auto-generate a MoviesController that implements CRUD()-functionality using Entity Framework by simply telling which Model to use.
"Add Controller"-Form Now when using the web-application we can already write and read from the database.

What would be the simplest (or a simple) way to get this done for our Movie-Model with NHibernate instead of using Entity Framework?


Clarified Updated Question - End





Original question (only for additional background-info):


I'm trying to create an ASP.Net MVC 3 application that uses NHibernate and Postgres.

Background Info

Development is done on Windows with Visual Web Developer Express, the production environment will be/should be Linux+Mono.

Steps that have worked so far:

  • An ASP.Net Dynamic Data Entities Web Application using Npgsql and Postgres as the DB. Successfully run on Windows development machine. (Following this tutorial)
  • An ASP.Net MVC 3 application without using a database/model yet: Succesfully run on Windows development machine and deployed to Linux production environment using Mono and Nginx. (Only as a proof of concept for myself not as a web app used by the public.)
  • An ASP.Net MVC 3 application with a model using SQL Server Express as the DB. Successfully run on my Windows development machine. (Following the MVC 3 Getting Started-tutorial)

Question

So far I managed to get Postgres to work with a "Dynamic Data Entities Web Application" but with an MVC 3 Web app I'm stuck on where/how to start. For the last mentioned MVC-3-Movie-Webapp I want to switch the DB from SQL Server Express to Postgres using NHibernate and Npgsql (NHibernate since Mono doesn't support Entity Framework).

When you look at the end of part 4 there's the simple MovieDBContext-code

public class MovieDBContext : DbContext 
{
    public DbSet<Movie> Movies { get; set; } 
}

and in the beginning of part 5, we autogenerate CRUD-stuff using Entity Framework by simply telling which Model to use. (MoviesController.cs, Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml, and Index.cshtml)

So I have that working with Entity Framework and SQL Server Express, but how would I achieve the same result by using NHibernate? (doesn't have to be with postgres immediately, sticking with SQL-Server as a first step would be fine) (Hopefully with similar simplicity, but getting the result itself would be great)
I found a lot of old stuff and how I would manually map things, but what would be a good-up to date standard way of achieving this with NHibernate for MVC 3?

(The closest thing I found was the source code mentioned in this thread, but it's 64 MB unzipped I got several "Projects not loaded successfully"-errors and the author said he uses MVC 2 so I think it's a little over my head for being a complete NHibernate noob.)

I think showing how this is done could be very useful for others as well, since the original tutorial is very easy to follow and is linked as the official starting point for MVC 3 app-development on http://www.asp.net/mvc ("Your First ASP.NET MVC App"). So I think this would be a great up to date example about how to use NHibernate with MVC 3.

like image 438
Jennifer Owens Avatar asked Nov 05 '22 17:11

Jennifer Owens


2 Answers

Actually, those automated things haven't helpful enough in real world applications. We have to separate concerns and by using DataContext in UI Layer is not a good practice because that dependency will cause problems like lack of test-ability, violation of best practices. I think you need to have following things of your project

  1. Separation of Concern (Layered Architecture - UI Layer, Servie Layer, Domain Layer, Infrastructure Layer)
  2. Generic Repository and Unit of Work wrapping (Database functionalities, ORM - EF, NHibernate, etc
  3. In your Service Layer process repositories and unit of work processings and expose Data Transfer objects or your domain objects (POCOs) to UI Layer
  4. Use IOC to inject dependencies will help you to minimize dependencies
  5. Create Unit test and Integration tests
  6. Use Continuous Integration and Source control prefer (Distributed: Mercurial)

Useful References:

  1. (Sharp Architecture) http://sharparchitecture.codeplex.com/
  2. (IOC Container) http://www.castleproject.org/container/
  3. (Generic repository) http://code.google.com/p/genericrepository/
like image 63
marvelTracker Avatar answered Nov 09 '22 16:11

marvelTracker


NuGet is your friend. Here's a good example of using NuGet to automatically wire in your dependencies and configuration pretty much automatically.

Hope this helps.

like image 32
Lance Harper Avatar answered Nov 09 '22 17:11

Lance Harper