Software Mechanics
Why do we even have that lever?

Presenting - and a MVC sample update

November 14, 2008 05:24 by Chris

(I really should post this stuff before the actual event.)

Last Thursday, I went down to lovely Olympia Washington to present to the South Sound .NET Users group. I did an intro talk on the ASP.NET MVC framework which seemed to be pretty well received. I wanted to thank the group for having me, I had a great time (aside from the drive down ;-)).

This weekend was Seattle CodeCamp 4. While sitting at dinner watching Jason print up the schedule sheets for Sunday, I noticed (after he'd already printed about 100 copies) that there were empty slots in the schedule. Silly me, I figured I already had a talk and volunteered, and ended up re-presenting the MVC talk.

I used the demo wiki code that I originally wrote for my magazine article. In the process, I updated the code to the lastest ASP.NET MVC beta, and then promptly forgot to post it!

So, for those who are interested, here's the latest version. Actually, I've got two version - one "vanilla" and one that integrates the Unity DI container.

Enjoy!

-Chris

MVCMiniWiki.zip (170.51 kb)

MVCMiniWiki-Unity.zip (172.41 kb)


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .NET
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Magazine article obsolete? Not if I can help it!

March 15, 2008 16:50 by Chris

My first MSDN Magazine article, ASP.NET MVC: Building Web Apps Without Web Forms, just hit the stands in the March issue. Unfortunately, due to the slow pace of the publishing business (although, to their credit MSDN magazine is actually really quick on the production side) the article had to be done in January. With the release of the ASP.NET MVC Preview 2 bits at Mix '08, the sample code in the article was up to date for all of two days.

Not that I'm complaining, that's just how it works when writing about rapidly moving prerelease software. But I have to attempt to stay relevant, and as such, I've updated the code download. Until the MSDN site updates (these things usually take a few days) you can download the updated samples here. As new releases come out I'll try to keep this code updated as long as it's till relevant.

MVCPreview2UpdatedSamples.zip (64.51 kb)


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: .NET
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Using Unity and the ASP.NET MVC Preview 2

March 13, 2008 07:40 by Chris

The recent release of the ASP.NET MVC Framework made a change to the IControllerFactory interface at the request of users of dependency injection containers. Instead of passing the controller factory the type of controller desired, it now gets the string from the route, and the factory can now resolve that type however it wishes.

This fits with most other DI containers, since they have to have everything pre-configured anyway. However, Unity is a little different, in that you don't have to register concrete types ahead of time. This change requires Unity users to register types ahead of time like everyone else, or do reflection to find controller types at runtime. Kind of annoying.

However, there's another answer. Recent (ahem) discussions on the net has shown an obsession with interfaces, to the point that even when there's a useful base class that will solve the problem, many will go straight to the interface and duplicate a lot of work.

In this case, there's a simple way to hook Unity up to ASP.NET MVC preview 2. Rather than implement IControllerFactory, inherit from DefaultControllerFactory instead. There's a method in there, GetControllerInstance, which is called after the name has already been resolved to a type. In other words, the DefaultControllerFactory already does the reflection for you.

Here's the code:

    public class UnityControllerFactory : DefaultControllerFactory
    {
        IUnityContainer container;

        public UnityControllerFactory(IUnityContainer container)
        {
            this.container = container;
        }

        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException("controllerType");
            }
            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException("Type requested is not a controller", "controllerType");
            }

            return container.Resolve(controllerType) as IController;
 }

To hook this up, in global.asax.cs, do something like this:

            IUnityContainer container = new UnityContainer();

            // Configure container here

            IControllerFactory controllerFactory = new UnityControllerFactory(container);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);

Simple and easy! Hope this helps!

 


Currently rated 3.2 by 6 people

  • Currently 3.166667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: ,
Categories: .NET | Unity
Actions: E-mail | Permalink | Comments (7) | Comment RSSRSS comment feed