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

Disclaimer

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

© Copyright 2010

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 (48) | 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.

Busby SEO Test us

Monday, January 26, 2009 9:08 PM

Busby SEO Test

Hi King thank you man

King Wilder us

Tuesday, January 27, 2009 2:03 AM

King Wilder

Busby SEO,

I'm glad these examples help you.

computer IT us

Thursday, March 05, 2009 4:36 AM

computer IT

great article, thank you

movers bh

Wednesday, March 18, 2009 10:52 AM

movers

love this post
great
lol

famous funny quotes us

Wednesday, March 18, 2009 6:05 PM

famous funny quotes



Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of presentation.

jazz us

Friday, March 20, 2009 6:59 PM

jazz

nice article man! thanks..

Budgie us

Saturday, March 21, 2009 11:15 PM

Budgie

Great tutorias..

magos ar

Sunday, April 05, 2009 11:28 PM

magos

Very rich tutorial.

I wish everyone wrote stuff like you.

Thanks!

jammer gr

Monday, April 06, 2009 9:02 AM

jammer

lovely thinking
lovely making
lovely handling

gmat gr

Monday, April 06, 2009 9:02 AM

gmat

hi
nice 1
thanks alot

Louisiana movers mo

Monday, April 06, 2009 9:03 AM

Louisiana movers

great article, thank you

tukang nggame us

Wednesday, April 08, 2009 12:09 AM

tukang nggame

Nice post

Franchise Information gb

Thursday, April 09, 2009 12:28 AM

Franchise Information

Hey, just checking out the blogengine.net platform... Seems pretty nice.

What is the backend like?

Cheers

Matthew

King Wilder us

Thursday, April 09, 2009 2:47 AM

King Wilder

Matthew,

The back end is clean and concise. It's free. You should download it and give it a try.

King Wilder

convoy jammer eg

Friday, April 10, 2009 1:51 AM

convoy jammer

made me smile
made me cry
love it so much

Wichita Movers jp

Friday, April 10, 2009 1:52 AM

Wichita Movers



Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of presentation.

Alabama Movers cn

Friday, April 10, 2009 1:53 AM

Alabama Movers

love this post
great
lol

md5 hash us

Thursday, April 16, 2009 4:26 PM

md5 hash

thanks for share

tukang nggame gb

Saturday, April 25, 2009 3:55 PM

tukang nggame

very nice information

Louisiana movers qa

Sunday, April 26, 2009 7:04 AM

Louisiana movers

we want more stuff
thanks

Leadership Styles gb

Sunday, April 26, 2009 11:28 PM

Leadership Styles

Thanks for writing this great post. Its a nice writing style you use - something I couldn't replicate on my own blog even if I tried! Thanks again.

part time franchises gb

Monday, May 11, 2009 5:26 AM

part time franchises

nice post, thanks. Great blog too, love the blogengine platform

Minnesota movers fo

Saturday, May 16, 2009 9:27 AM

Minnesota movers

great pot
love u all

movers pe

Saturday, May 16, 2009 9:27 AM

movers

u made me love u even more

franchise gb

Wednesday, May 20, 2009 10:31 PM

franchise

Hey - nice blog, just looking around some blogengine.net sites, seems a pretty nice platform. I'm currently using Wordpress for a few of my sites but looking to change one of them over to blogengine.net as a trial run. Anything in particular you would recommend about it? Cheers, Matthew

Admin us

Thursday, May 21, 2009 3:08 AM

Admin

Well first of all, it matters what platform you are being hosted on. Wordpress is written in PHP, which means it can work on Linux/Unix or Windows server. BlogEngine.net is written in ASP.NET which means it can generally work on a Windows server, unless the Mono platform is on a Linux server.

Otherwise, they are both pretty comparable I think. BlogEngine.net can work off of XML as it's data store which makes it easy to move than if you use a database. It's also fairly easy to skin. But you should go to the site for more information, http://www.dotnetblogengine.net.

I hope that helps.

King Wilder

lissa us

Thursday, May 21, 2009 8:29 PM

lissa

Thanks for the shoutout admin...

Israel democracy ph

Saturday, May 23, 2009 12:57 AM

Israel democracy

love this post
great tthinking

belajar seo us

Saturday, May 30, 2009 7:45 AM

belajar seo

thanks for sharing the link to asp.net it help me in configure and use the ASP.Net

yazılım gb

Sunday, May 31, 2009 1:45 AM

yazılım

This is great

Israel institute uz

Sunday, June 07, 2009 5:09 AM

Israel institute

thank u sir
made my day

Franchise News us

Wednesday, June 10, 2009 11:23 PM

Franchise News

It's interesting, the blog engine platform seems very variable in form. My design skills are not so good as my C coding though, I would be interested in seeing what additional skins you can get for it. Nice blog btw, best wishes for it and keep up the posts. Smile Kind regards, Peter sims.

RUSLI zainal sang Visioner us

Friday, July 03, 2009 11:21 AM

RUSLI zainal sang Visioner

Very Nice Information

RUSLI zainal sang Visioner us

Friday, July 03, 2009 11:22 AM

RUSLI zainal sang Visioner

Nice post buddy

Online Poker us

Monday, July 06, 2009 4:39 PM

Online Poker

Thanks a lot for providing the code as well

Online nursing degree us

Monday, July 06, 2009 9:25 PM

Online nursing degree

Very Nice Information

work experience degree us

Monday, July 06, 2009 9:26 PM

work experience degree

My design skills are not so good as my C coding though, I would be interested in seeing what additional

stop dreaming start action

Tuesday, July 07, 2009 11:18 PM

stop dreaming start action

Great post

on line degrees us

Friday, July 10, 2009 4:04 PM

on line degrees

design skills are not so good as my C coding though, I would be interested in seeing what additional

stop dreaming start action id

Tuesday, July 14, 2009 4:05 PM

stop dreaming start action

thanks for post i like it

internet marketing us

Tuesday, July 14, 2009 9:25 PM

internet marketing

How to use microsoft express to built a web page in asp.net?

London Torus gb

Tuesday, July 21, 2009 12:13 AM

London Torus

I didnt realise MVC was so popular. Nice post.

Stop Dreaming Start Action us

Friday, July 24, 2009 12:00 AM

Stop Dreaming Start Action

This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

reimage

Friday, July 24, 2009 11:52 PM

reimage

Great I do love this.

sulumits retsambew us

Monday, July 27, 2009 3:09 PM

sulumits retsambew

nice info. thanks