MVC Starter Kits

MVC Starter Kits for ASP.NET

About the author

King Wilder, I'm an ASP.NET developer and I run and own a small web hosting company called Gizmo Beach.
E-mail me Send mail

Pages

Recent comments

Authors

Categories


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

ASP.NET MVC Northwind Demo using SubSonic - Part Tres

In Part 1, I built a simple Northwind ASP.NET MVC application using Entity Spaces and maintained the Entity Spaces references throughout the application.  This is not a loosely coupled application, but it does allow for slick and easy Entity Spaces relationship mapping in the View and in other layers of the application.  The downside is that a reference to the Entity Spaces DLL's need to be made in each layer.

In Part Deux, I refactored the application to be loosely coupled.  This helps promote a very flexible and extendable application and maintain the separation of concerns.  In other words, the View or presentation layer, has no idea what kind of data layer is sending the data.

Ok, now what?  Well I stated I was going to try and implement a version of the application for SubSonic, and here it is! 

I'm doing this to make a point.  My goal was to test whether the "loose coupling" pattern holds true and it does, apart from a few changes I needed to make because of changing from Entity Spaces to SubSonic.  These changes are not critical or life changing, but I needed to do it because of the differences in the way SubSonic handles entity naming from Entity Spaces.

And the changes just reflect that you can find out whether your application can stand up to change, by trying to change something and see how much you need to do to handle that change.  In this case, I did need to make some minor changes, but not much.  And in the long run, I feel that the application is even more flexible than version two because of the changes I've made.  I'll explain...

What I mean is this:

In the Northwind database there are table names such as:

  • Categories
  • Territories
  • Employees
  • etc...


These are all plural.  There is nothing wrong with that, and Entity Spaces maintains the naming convention.  It will remove any "underscores" or "dots" that are contained in the table name and just squeeze the words together.  But SubSonic changes the plural names to singular names.

Let me add a disclaimer here, that I am not endorsing SubSonic or Entity Spaces.  But I personally prefer Entity Spaces because it seems to me to be more straight forward in the architecture, and it has saved me hours and hours of work building my application.  I have used SubSonic a little but I am not an expert so the code I will be showing you, may or may not be the most performance enhanced version.  You may know a better way of querying and that's fine.  I just wanted to show that by simply changing the data provider in the Repository, the higher layers need not be affected.

SO WHAT DOES THE TABLE NAME HAVE TO DO WITH ANYTHING?


The reason I brought this up, is that with Entity Spaces, since it keeps the naming convention of the table names intact, I created a separate set of model classes (in the singular) for each table I needed in the application.

So if there was a "Categories" table in the database, I created a "Category" class for the model that would be sent to the View.  This helps promote "loose coupling". 

The reason this had to be address for the SubSonic version is that there was a namespace collision between the generated SubSonic classes and the Model classes.

The SubSonic classes were given the namespace as such:

namespace ESNorthwind.MVC.Data

But the model classes already had that namespace. And when SubSonic makes all plural table names singular, that's where the collision occurred.

solution explorer

 You'll see in this image, the model classes are in the Model folder, and the SubSonic classes are in the Generated folder, and they have the same names.  If they are in the same namespace, they will collide.

So what I did, was refactor a bit so that it would work for either Entity Spaces or SubSonic, or for any other ORM classes that are generated for the application.  I moved the model classes into their own namespace, ESNorthind.MVC.Data.Model.

Of course this will break in many places all over the application, so I did a quick refactor, everywhere so that the model classes now point to the new namespace.

So anywhere that a model class existed in code like this:

Product product = service.GetProductById(id);

... it was changed to this...

ESNorthwind.MVC.Data.Model.Product product = service.GetProductById(id);

I included the full namespace to the class to prevent ambiguous naming.

The View code-behind went from this:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ESNorthwind.MVC.Data;

namespace ESNorthwind.MVC.Web.Views.Products
{
public partial class Categories : ViewPage< IList<Category> >
{
public void Page_Load()
{

}
}
}

... to this ...

 

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ESNorthwind.MVC.Data;

namespace ESNorthwind.MVC.Web.Views.Products
{
public partial class Categories : ViewPage< IList<ESNorthwind.MVC.Data.Model.Category> >
{
public void Page_Load()
{

}
}
}

Notice the change in the ViewPage generic base class.

Now if I want to change back to Entity Spaces, I just need to modify the code in the Repository object and I'm done!

I kept the Entity Spaces code in the Repository object so you can see the similarity in their object model, but also the subtle differences.

/// <summary>
/// Get all suppliers.
/// </summary>
/// <returns></returns>
public IList<ESNorthwind.MVC.Data.Model.Supplier> GetSuppliers()
{
/********************************************************************
* * Begin Entity Spaces Code
* *****************************************************************/

//SuppliersCollection suppColl = new SuppliersCollection();
//suppColl.LoadAll();

//Supplier supplier = null;

//List<Supplier> suppList = new List<Supplier>();
//foreach (Suppliers supp in suppColl)
//{
// supplier = new Supplier();
// supplier.SupplierID = (int)supp.SupplierID;
// supplier.CompanyName = supp.CompanyName;
// suppList.Add(supplier);
//}
//return suppList;

/********************************************************************
* * End Entity Spaces Code
* *****************************************************************/

/********************************************************************
* * Begin SubSonic Code
* *****************************************************************/

SupplierCollection suppColl = new SupplierCollection();
suppColl.Load();

ESNorthwind.MVC.Data.Model.Supplier supplier = null;

List<ESNorthwind.MVC.Data.Model.Supplier> suppList = new List<ESNorthwind.MVC.Data.Model.Supplier>();
foreach (Supplier supp in suppColl)
{
supplier = new ESNorthwind.MVC.Data.Model.Supplier();
supplier.SupplierID = (int)supp.SupplierID;
supplier.CompanyName = supp.CompanyName;
suppList.Add(supplier);
}
return suppList;

/********************************************************************
* * End SubSonic Code
* *****************************************************************/
}

To those developers interested in ASP.NET MVC and SubSonic, I hope this helps. Rob Conery (the creator of SubSonic) has more information on ASP.NET MVC using SubSonic, so check out his Blog.

Thanks for watching!

You can download the file here, ESNorthwind.MVC_Pt3.zip (6.77 mb)

King Wilder.

 

 

 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Admin on Tuesday, June 17, 2008 6:57 AM
Permalink | Comments (3) | Post RSSRSS comment feed

Related posts

Comments

Dragan Panjkov ba

Tuesday, June 17, 2008 8:27 AM

Dragan Panjkov

What is next? Will you now unplug SubSonic and try LINQ? It would be really nice to see. I am looking for an approach that uses repository pattern and linq and that can be used in both asp.net and asp.net mvc projects. Until now, I only suceeded to reproduce approach used by phil haack in his example (northwind and mvc) but i need comprehensive guidance on that.

King Wilder

Tuesday, June 17, 2008 10:25 AM

King Wilder

Dragan,

The original project used Linq, but yes it wasn't in the site structure that I'm using. I probably could create a version like that. I probably can't do it until sometime next week, 6/23/2008. Check back then, I should have it up for you.

King Wilder us

Tuesday, July 08, 2008 5:36 AM

King Wilder

Sorry I haven't had time to do much work on this because I have a couple large paying jobs. But as soon as I have a moment, I'll get back to posting more projects.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Thursday, November 20, 2008 3:50 AM