1

I would like to build a new web application using ASP.NET MVC3 and MongoDB. I've seen many examples online and even built some working code myself, but I am wondering about how I should set up my application. In the MVC examples which use Entity Framework, they place everything in a Models folder. I think I will do the same but where should I put my queries etc. Should I abstract them to a better location. I'm somewhat new to making C# applications and the .NET world, so some of the "ways" are not clear to me yet. Also, does creating the database object (where I tell it mongo's server address) each time I need it have performance impacts? Can I just connect once and then talk through that object? Does it really reconnect every time I perform that action?

Thanks!

Asdfg
  • 11,362
  • 24
  • 98
  • 175
Greg Potter
  • 527
  • 1
  • 5
  • 18
  • http://stackoverflow.com/questions/5990773/how-asp-net-mvc-architecture-fits-into-the-traditional-multi-layered-architectur – Asdfg Aug 11 '11 at 18:53
  • Yes, I am familiar with the MVC concept and have done it before, but I am curious about what the optimal way to integrate it with MongoDB. – Greg Potter Aug 11 '11 at 19:33

2 Answers2

5

Normally, in your Model, you have an object model representing your domain.

With MongoDB, this does not change. Your objects in your model will still have properties and behaviors.

What will change, is that instead of storing each object in a table in a relationnal model, you will be storing a graph of objects. Let's say you have an invoice. You will store the Invoice, with all the lines of the invoice as a single record. That's about it, not really more complicated than that.

Martin
  • 5,954
  • 5
  • 30
  • 46
  • I have not used MongoDB myself, but I am a bit familiar with Document oriented databases. You might want to use a JSON library to serialize your objects, but I am not sure, it depends on how MongoDB integrates with .NET. – Martin Aug 11 '11 at 20:23
  • 1
    MongoDB integrates wonderfully with .NET. There is an official driver from 10gen themselves (makes of MongoDB) which takes care of the serialization of your objects to and from BSON. – Bryan Migliorisi Aug 12 '11 at 01:34
1

First of all, don't use your domain objects (these that you supposed to save to RDBMS using Entity Framework or to MongoDB) directly in ASP.NET MVC views! Use viewmodels instead. Then you will have Models folder in ASP.NET MVC project and separate project for your domain.

I didn't work with MongoDB before, but I suppose the best way to have database object per http request. Here is discussion on stackoverflow and here is video from 10gen about their C# driver.

Community
  • 1
  • 1
xelibrion
  • 2,220
  • 1
  • 19
  • 24