NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 2

 
In my last post, I have given an introduction to MongoDB and NoRM using an ASP.NET MVC demo app. I have updated the demo ASP.NET MVC app and a created a new drop at codeplex. You can download the demo at http://mongomvc.codeplex.com/

In my last post, we have discussed to doing basic CRUD operations against a simple domain entity. In this post, let’s discuss on domain entity with deep object graph.

The below is our domain entities

 

public class Category

{

 

    [MongoIdentifier]

    public ObjectId Id { get; set; }

 

    [Required(ErrorMessage = "Name Required")]

    [StringLength(25, ErrorMessage = "Must be less than 25 characters")]

    public string Name { get; set;}

    public string Description { get; set; }

    public List<Expense> Expenses { get; set; }

 

    public Category()

    {

        Expenses = new List<Expense>();

    }

}

 

 

public class Expense

{

    [MongoIdentifier]

    public ObjectId Id { get; set; }

    public Category Category { get; set; }

    public string  Transaction { get; set; }

    public DateTime Date { get; set; }

    public double Amount { get; set; }

 

}

 

 We have two domain entities - Category and Expense. A single category contains a list of expense transactions and every expense transaction should have a Category.

 

The MongoSession class

 

internal class MongoSession : IDisposable

{

    private readonly MongoQueryProvider provider;

 

    public MongoSession()

    {

        this.provider = new MongoQueryProvider("Expense");

    }

 

    public IQueryable<Category> Categories

    {

        get { return new MongoQuery<Category>(this.provider); }

    }

    public IQueryable<Expense> Expenses

    {

        get { return new MongoQuery<Expense>(this.provider); }

    }

    public MongoQueryProvider Provider

    {

        get { return this.provider; }

    }

 

    public void Add<T>(T item) where T : class, new()

    {

        this.provider.DB.GetCollection<T>().Insert(item);

    }

 

    public void Dispose()

    {

        this.provider.Server.Dispose();

    }

    public void Delete<T>(T item) where T : class, new()

    {

        this.provider.DB.GetCollection<T>().Delete(item);

    }

 

    public void Drop<T>()

    {

        this.provider.DB.DropCollection(typeof(T).Name);

    }

 

    public void Save<T>(T item) where T : class,new()

    {

        this.provider.DB.GetCollection<T>().Save(item);           

    }

 

 

}   

 

ASP.NET MVC view model  for Expense transaction

 

public class ExpenseViewModel

{

    public ObjectId Id { get; set; }

 

    public ObjectId CategoryId { get; set; }

 

    [Required(ErrorMessage = "Transaction Required")]       

    public string Transaction { get; set; }

 

    [Required(ErrorMessage = "Date Required")]       

    public DateTime Date { get; set; }

 

    [Required(ErrorMessage = "Amount Required")]   

    public double Amount { get; set; }

 

    public IEnumerable<SelectListItem> Category { get; set; }

}

 

Let's create an action method for Insert and Update a expense transaction

 

[HttpPost]

public ActionResult Save(ExpenseViewModel expenseViewModel)

{

    try

    {

        if (!ModelState.IsValid)

        {

            using (var session = new MongoSession())

            {

                var categories = session.Categories.AsEnumerable<Category>();

                expenseViewModel.Category = categories.ToSelectListItems(expenseViewModel.CategoryId);   

            }

            return View("Save", expenseViewModel);

        }

 

        var expense=new Expense();

        ModelCopier.CopyModel(expenseViewModel, expense);

 

        using (var session = new MongoSession())

        {

            ObjectId Id = expenseViewModel.CategoryId;

            var category = session.Categories

                .Where(c => c.Id ==Id  )

                .FirstOrDefault();

            expense.Category = category;

            session.Save(expense);

        }

        return RedirectToAction("Index");

    }

    catch

    {

        return View();

    }

}

 

To save a expense transaction is very easy. You just need to create a Expense object and set the Category object for Category property. The session.Save method will do the all magic for you. If it is a new Expense traction, it will create a new record to MongoDB with associated Category object and update the Expense if it is existing Expense record.

 

Query with Expenses

using (var session = new MongoSession())

{

    var expenses = session.Expenses.

        Where(exp => exp.Date >= StartDate && exp.Date <= EndDate)

        .AsEnumerable<Expense>();

}

 

We are doing a LINQ query expression with a Date filter. We can easily work with MongoDB using NoRM driver and managing object graph of domain entities are pretty easy.


 Download the Source - You can download the source code from http://mongomvc.codeplex.com

Published Wednesday, April 21, 2010 10:30 PM by shiju
Filed under: , ,

Comments

# Twitter Trackbacks for NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 2 - Shiju Varghese's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 2 - Shiju Varghese's Blog         [asp.net]        on Topsy.com

# NoSQL with MongoDB, NoRM and ASP.NET MVC &laquo; Huy Nguyen&#039;s Blog

Pingback from  NoSQL with MongoDB, NoRM and ASP.NET MVC  &laquo; Huy Nguyen&#039;s Blog

# NoSQL with MongoDB, NoRM and ASP.NET MVC &laquo; Huy Nguyen&#039;s Blog

Pingback from  NoSQL with MongoDB, NoRM and ASP.NET MVC  &laquo; Huy Nguyen&#039;s Blog

# NoSQL with MongoDB, NoRM and ASP.NET MVC &laquo; Huy Nguyen&#039;s Blog

Pingback from  NoSQL with MongoDB, NoRM and ASP.NET MVC  &laquo; Huy Nguyen&#039;s Blog

# NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 2 - Shiju Varghese's Blog

Thursday, April 22, 2010 4:07 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# The Morning Brew - Chris Alcock &raquo; The Morning Brew #585

Thursday, April 22, 2010 4:33 AM by The Morning Brew - Chris Alcock » The Morning Brew #585

Pingback from  The Morning Brew - Chris Alcock  &raquo; The Morning Brew #585

# Cheatsheet: 2010 04.19 ~ 04.25

Saturday, April 24, 2010 11:06 PM by gOODiDEA.NET

Web The Best of Steve: Performance at JSConf Seven JavaScript Things I Wish I Knew Much Earlier In My

# ASP.NET MVC Archived Blog Posts, Page 1

Saturday, April 24, 2010 11:10 PM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# NoSQL c MongoDB, NoRM и ASP.NET MVC - часть 2

Sunday, April 25, 2010 10:23 AM by progg.ru

Thank you for submitting this cool story - Trackback from progg.ru

# Updated Wiki: Home. &rsaquo; PHP App Engine

Thursday, May 13, 2010 2:57 AM by Updated Wiki: Home. › PHP App Engine

Pingback from  Updated Wiki: Home. &rsaquo;  PHP App Engine

# NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 1 - Shiju Varghese's Blog

Pingback from  NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 1 - Shiju Varghese's Blog

# ScottGu&amp;#8217;s May 20th Links: ASP.NET MVC, ASP.NET, .NET 4, VS 2010, Silverlight &#171; Code Name &quot;NitRiX Reloaded&quot;

Pingback from  ScottGu&amp;#8217;s May 20th Links: ASP.NET MVC, ASP.NET, .NET 4, VS 2010, Silverlight &#171; Code Name &quot;NitRiX Reloaded&quot;

# NoSQL with RavenDB and ASP.NET MVC - Part 1

Wednesday, May 26, 2010 11:25 PM by Shiju Varghese's Blog

A while back, I have blogged NoSQL with MongoDB, NoRM and ASP.NET MVC Part 1 and Part 2 on how to use

# NoSQL with RavenDB and ASP.NET MVC - Part 1

Wednesday, May 26, 2010 11:42 PM by Community Blogs

A while back, I have blogged NoSQL with MongoDB, NoRM and ASP.NET MVC Part 1 and Part 2 on how to use

# NoSQL with RavenDB and ASP.NET MVC &#8211; Part 1 | OOP - Object Oriented Programing

Pingback from  NoSQL with RavenDB and ASP.NET MVC &#8211; Part 1 | OOP - Object Oriented Programing

# NoSQL with RavenDB and ASP.NET MVC &#8211; Part 1 | OOP - Object Oriented Programing

Pingback from  NoSQL with RavenDB and ASP.NET MVC &#8211; Part 1 | OOP - Object Oriented Programing

# Resultado de ALT.NET Hispano VAN sobre NoSQL

Saturday, July 17, 2010 12:21 PM by Angel "Java" Lopez

Gracias a la gente de la comunidad ALT.NET Hispano, pude participar de una VAN (des-conferencia) sobre

# NoSQL Resources &laquo; Angel &#8220;Java&#8221; Lopez on Blog

Monday, July 19, 2010 5:57 AM by NoSQL Resources « Angel “Java” Lopez on Blog

Pingback from  NoSQL Resources &laquo; Angel &#8220;Java&#8221; Lopez on Blog

# NoSQL Daily &#8211; Wed Sep 29 &rsaquo; PHP App Engine

Wednesday, September 29, 2010 4:15 AM by NoSQL Daily – Wed Sep 29 › PHP App Engine

Pingback from  NoSQL Daily &#8211; Wed Sep 29 &rsaquo;  PHP App Engine

# Links Interesantes de .NET en general

Tuesday, October 12, 2010 6:27 AM by Phydelta

Links Interesantes de .NET en general

# Phydelta - Links Interesantes de .NET en general

Tuesday, October 12, 2010 6:27 AM by Phydelta - Links Interesantes de .NET en general

Pingback from  Phydelta - Links Interesantes de .NET en general

# re: NoSQL with MongoDB, NoRM and ASP.NET MVC - Part 2

Wednesday, January 05, 2011 10:22 AM by ben

cool article, but one thing , when update Category entity,  the category of the same id in expense collection don't change,  any deeper step to take for this, am 'i wrong?

# MongoDB vs. SQL Server 2008 Performance Showdown | fresh amblique

Pingback from  MongoDB vs. SQL Server 2008 Performance Showdown | fresh amblique

# MongoMvc &#8211; ASP.NET MVC 2 &amp; MongoDB | N.P Cad

Friday, September 02, 2011 4:13 AM by MongoMvc – ASP.NET MVC 2 & MongoDB | N.P Cad

Pingback from  MongoMvc &#8211; ASP.NET MVC 2 &amp; MongoDB | N.P Cad

Leave a Comment

(required) 
(required) 
(optional)
(required)