This project is read-only.

Hooking up SA Areas into WCHM

Jan 6, 2010 at 7:33 PM

The WHCM sample app is configured by default to not use the SA Areas feature.

Due to the complexity of my app, I like to have Area support, so I hooked it up. Below is detailed the process, in case anyone else would like to do so!

If I've done something stupid, please do let me know!

 

Steps:

1) Modify ControllerRegistrar to not puke when finding more than one controller with the same name.

This fixes the issue where you have two Areas, both with a HomeController.

Instead of registering controllers by their type Name, we use their FullName instead (which includes the namespace).

ControllerRegistrar.cs

http://whocanhelpme.codeplex.com/sourcecontrol/changeset/view/53686?projectName=whocanhelpme#945488

Change:

        public void Register(IWindsorContainer container)
{
Assembly.GetAssembly(typeof(ControllersRegistrarMarker)).GetExportedTypes()
.Where(IsController)
.Each(type => container.AddComponentLifeStyle(
type.Name.ToLower(),
type,
LifestyleType.Transient));
}

to

                                          type.FullName.ToLower(),

 

2) Create a IComponentInitialiser for view engine registration:

Add: Web/Initialisers/ViewEngineInitialiser.cs:

    [Export(typeof(IComponentInitialiser))]
    public class ViewEngineInitialiser : IComponentInitialiser
    {
        public void Initialise()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new AreaViewEngine());
        }
    }

This will register the SA AreaViewEngine.

 

3) Set up your Areas accordingly in:

* RouteInitialiser.cs

* Your Controllers project

* Views folder

 

The only caveat I found here is that, for every area, your controllers will need to be in the base Area namespace. This goes against the default convention in WCHM, where each controller is in it's own namespace. For example:

Instead of:

Controllers.Home.HomeController in the "Root" Area, you will need to move it to Controllers.HomeController

Another example:

Controllers.AreaName.Home.HomeController will need to be Controllers.AreaName.HomeController (loose the "Home" part of the namespace).

 

 

4) Be aware of a potential SA Area bug:

I'm still not sure if this is an issue or just my bad, but here it is for your consideration:

http://groups.google.com/group/sharp-architecture/browse_thread/thread/65e5d1528d629b17#

 

Please let me know if you find this helpful!

Best regards,

Martin

Jan 7, 2010 at 4:42 PM
Edited Jan 7, 2010 at 4:43 PM

You should not need to modify the controller registrar, when you use CreateAreas you tell the routes which namespaces to use so they should not complain about two controllers with the same name as long as they are different namespaces and you make sure you call CreateArea with the namespace of each area's controllers.

I used SA areas as well in Graphite my blog engine and recently I have decided to get rid of them for a few reasons:

1. I am so far not very happy with the API for routing in ASP.NET MVC, I have probably wasted a total of 3-4 days of free time playing with a combination of MVC routing, SA Areas and setting up "restful" routes.  The area's and namespaces issues added to the complexity of this for me and the horrible routing API made it very slow to understand just how routing was working.  the MS team really should go back to the drawing board on this (you can see Oren's post on this too).

2. I really want restful routing and for that I have finally found a decent API that gives me 100% of the flexibility I want without feeling clunky: http://github.com/stevehodgkiss/restful-routing.  Steve deserves a big pat on the back for this and I would really like to see this become part of MvcContrib.  It could still use a bit of work, but I think you will like it.  I will do a blog post soon on how to use it (and even how to make it work with SA areas properly, if you don't want to wait I have it working with SA Areas here already: http://github.com/lucisferre/graphite

3. The new MVC 2 area is a very different implementation than SA areas.  Conceptually they do not appear to be compatible concepts.  At this point, I don't know if SA will keep their Areas or drop them in favor of the build in Areas of MVC 2.  If they don't keep SA areas I don't really want to tie myself to them and I don't want to use MVC 2 areas yet, I find the concept a bit clunky still (MS should have called them "modules" or something since that is really what they are).

 

 

Jan 8, 2010 at 6:36 AM

Hi Chris,

Thank you so much for the feedback.

chnicola wrote:

You should not need to modify the controller registrar, when you use CreateAreas you tell the routes which namespaces to use so they should not complain about two controllers with the same name as long as they are different namespaces and you make sure you call CreateArea with the namespace of each area's controllers.

You are correct that, as far as the routes are concerned, they will not complain as you mention.

What I ran into, however, was the following case:

Say I have two controllers defined:

Controllers.HomeController (Root Area)

Controllers.MyCoolArea.HomeController

Both these controllers have the same name, but live in a different namespace.

The default ControllerRegistrar code would crash in this case, as it was trying to use the same type name ("HomeController" in both cases) twice as a key.

The solution here is to change the key to use FullName, which includes the namespace, and therefore guarantees uniqueness.

 

Note that the WCHM app registers it's controllers differently compared to the Northwind SA sample app.

The Northwind SA sample app calls IWindsorContainer.RegisterControllers(typeof(HomeController).Assembly), which internally uses (correctly) the FullName of each controller, thereby not being affected by this problem:

http://github.com/mvccontrib/MvcContrib/blob/master/src/MvcContrib.Castle/WindsorExtensions.cs

 

As for your suggestions on Areas, thank you very much for the detailed info. I will take a closer look at both restful routing and your sample Graphite app, as I am not familiar with either.

In the mean time, as per your third point, we should both go bug the SA list to get a better idea of their plans for MVC2. There are a lot of question marks there that make it harder to decide on a future-proof path at this point!

Jan 8, 2010 at 10:24 AM
Thanks for the feedback guys, it's interesting to know how these things can be incorporated into the app. I haven't played with areas, either in MVC 2 or SA, so I can't comment on whether or not this is the best way, but please keep on letting us know how you get on with modifying the app to fit your requirements.

Thanks
Jon

2010/1/8 martin_aatmaa <notifications@codeplex.com>

From: martin_aatmaa

Hi Chris,

Thank you so much for the feedback.

chnicola wrote:

You should not need to modify the controller registrar, when you use CreateAreas you tell the routes which namespaces to use so they should not complain about two controllers with the same name as long as they are different namespaces and you make sure you call CreateArea with the namespace of each area's controllers.

You are correct that, as far as the routes are concerned, they will not complain as you mention.

What I ran into, however, was the following case:

Say I have two controllers defined:

Controllers.HomeController (Root Area)

Controllers.MyCoolArea.HomeController

Both these controllers have the same name, but live in a different namespace.

The default ControllerRegistrar code would crash in this case, as it was trying to use the same type name ("HomeController" in both cases) twice as a key.

The solution here is to change the key to use FullName, which includes the namespace, and therefore guarantees uniqueness.

 

Note that the WCHM app registers it's controllers differently compared to the Northwind SA sample app.

The Northwind SA sample app calls IWindsorContainer.RegisterControllers(typeof(HomeController).Assembly), which internally uses (correctly) the FullName of each controller, thereby not being affected by this problem:

http://github.com/mvccontrib/MvcContrib/blob/master/src/MvcContrib.Castle/WindsorExtensions.cs

 

As for your suggestions on Areas, thank you very much for the detailed info. I will take a closer look at both restful routing and your sample Graphite app, as I am not familiar with either.

In the mean time, as per your third point, we should both go bug the SA list to get a better idea of their plans for MVC2. There are a lot of question marks there that make it harder to decide on a future-proof path at this point!

Read the full discussion online.

To add a post to this discussion, reply to this email (whocanhelpme@discussions.codeplex.com)

To start a new discussion for this project, email whocanhelpme@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com