Archive for October 20th, 2009

MVC 2 Areas and Containers

Some projects use a container like StructureMap to completely replace MVC’s DefaultControllerFactory. They do by registering all controllers by name using StructureMap’s scanning feature during application startup.

ObjectFactory.Initialize(x =>
    x.Scan(s => {
        s.AssembliesFromPath("bin");
        s.AddAllTypesOf<IController>()
                 .NameBy(type =>
                     type.Name.Replace("Controller", ""));
    }));

A simple controller factory to make this work would look like this:

public class SimpleControllerFactory : 
    IControllerFactory
{
    public IController CreateController(
        RequestContext requestContext, string controllerName)
    {
        return 
            ObjectFactory.GetNamedInstance
                <IController>(controllerName);
    }

    // ...
}

As I mentioned in a previous post, using Areas in MVC 2 means you have the potential for multiple controllers with the same name. A Home controller in the parent project, and a Home controller in each sub-project, for example. Now the simple approach we are using in the code above breaks down. We could do some work to make sure we are registering and looking up types using the proper namespaces, but the logic to look at the namespace constraints is already embedded in MVC’s DefaultControllerFactory.

An easier approach is to use the DefaultControllerFactory to lookup controller types and only use StructureMap to instantiate the controller and resolve dependencies. Doing so means we don’t need to scan any assemblies. StructureMap (and most IoC frameworks) are capable of instantiating an unregistered type as long as the type is concrete. All we need to do is derive from DefaultControllerFactory and override the GetControllerInstance method.

public class BetterControllerFactory
    : DefaultControllerFactory 
{
    protected override IController GetControllerInstance(
        RequestContext requestContext, Type controllerType)
    {
        IController result = null;
        if (controllerType != null)
        {
            result = ObjectFactory.GetInstance(controllerType)
                as IController;
        }
        return result;                
    }
}

Moral of the story: Don’t automatically throw away the DefaultControllerFactory. You may find it has some conventions you can make use of!


Using SqlBulkCopy To Perform Efficient Bulk SQL Operations

Over the years I've worked on a number of projects that have shared a common requirement - the ability for users to quickly import large amounts of data into a back end SQL Server database. One such project was a web application used by teachers and other staff members. The software was initially purchased for just two schools in the district, but was soon expanded to encompass other schools. Every few months one or two new schools were brought into the fold; every time a new school was added an administrative user would have to create accounts for the new teachers and staff members so that they could sign into the site. Initially, the application offered a web page for the administrator to create new user accounts one at a time, but this interface quickly became tedious and impractical once larger schools with upwards of 100 users were brought online.

To allow school administrators to quickly import new users we created a web page from which a user could upload an Excel spreadsheet that contained the one row for every new user; the columns in the spreadsheet mapped to table columns in the database. After uploading this spreadsheet, the application would walk through each row and insert a record into the table. Through this mechanism an administrator could create the 100+ user accounts by first building an Excel spreadsheet (something many of the schools already had on file) and then upload that spreadsheet. This technique may sound familiar - in Importing an Excel Spreadsheet Using Typed DataSets and TableAdapters, author Nannette Thacker walked through building such an interface.

While the described approach works well when importing hundreds of records, it starts to seriously slow down when importing thousands or tens of thousands of records. The slowdown is due to the fact that each imported record sends its own INSERT statement to the database. This results in a lot of "chatter" between the web server and the database server. If you are importing data to Microsoft SQL Server the good news is that this process can be dramatically sped up using ADO.NET's SqlBulkCopy class. In my testing, importing 10,000 records using the one INSERT statement per import record took more than three seconds to complete; using SqlBulkCopy took a fraction of a second.

This article look at how to use the SqlBulkCopy class to efficiently execute bulk operations against Microsoft SQL Server. Read on to learn more!
Read More >


Mark Miller Will Beat You With a Guitar!

Check out this 8 minute Mark Miller video interview where he discusses:

  • How he got an Xbox Guitar working with Visual Studio
  • The coding challenge at the DevExpress booth during Microsoft’s PDC 2009

Click the image below and watch the video to learn why I’m so excited to see this in action:

Video: Guitars and Code With Mark and Mehul

PDC Challenge

The coding challenge is simple:

  • You both have a PC with a standard install of Visual Studio.
  • You get to use a standard keyboard and mouse.
  • Mark will use an Xbox guitar and CodeRush.
  • You battle against each other by writing a piece of code.
  • Finish first and win big prizes.

Now, I’m a little skeptical whether Mark can pull it off. I mean that I’m basically crippled without my Microsoft Natural Ergonomic Keyboard 4000. But one thing I’ve learned is that you don’t underestimate Mark “the Millinator” Miller.

What do you think? Will he slaughter the competition or has he bitten off more than he can chew this time? Drop me a line below with your thoughts.

DevExpress PDC 2009 Platinum SponsorDXperience? What's That?

DXperience is a royalty-free tool suite for rapid business application development for WinForms and ASP.NET apps.

Instantly enhance your WinForms and ASP.NET apps by dropping in new feature sets encapsulated in components. DXperience contains:

  • IDE Productivity Tools - Make Visual Studio easier and more effective with IDE enhancements
    • DXCore - IDE tools extensible engine
    • CodeRush - Extensible swiss army knife of tools to make source code editing faster and easier, including code editing templates, code editing utilities (selection, navigation, clipboard), inline code visualizations and the upcoming unit test runner.
    • Refactor Pro - Code editing tools specifically geared for refactoring source code.
  • XAF - Business app framework for WinForms and ASP.NET
  • XPO – Object-relational mapping for .NET
  • Reporting - Reporting "platform" for WinForms and ASP.NET
  • UI Components for WinForms, ASP.NET, WPF and Silverlight

Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/


Calender Eightysix: JavaScript DatePicker Component

Calendar Eightysix is an unobtrusive developer friendly calendar and date-picker, offering a rich user experience for date related functionalities. Powered by MooTools, Calendar Eightysix is cross browser component and has been tested on: IE6+, Firefox 3+, Opera 9 – 9.5+, Safari 3.1+ and Google Chrome.

Calendar Eightysix has a variety of options to style and format the calendar to your needs. All options reside in a JavaScript object which is passed as an argument.

Features

  • Quick navigation by jumping back and forth between months, years and decades without drop-down boxes
  • Highly / easily customizable
  • Packed with three themes
  • Lightweight (9.5 kB compressed)
  • Pure JavaScript; AJAX-less and no PHP needed

Calendar Eightysix script is licensed under the Creative Commons Attribution- NonCommercial 3.0 License. What that means is: Use these files however you want, but don’t redistribute without the proper credits and do not use this for commercial purposes.

Calendar Eightysix requires a purchased commercial license when used commercially. You can find further information, demos and download on Calendar Eightysix Website.

Similar Posts:

You can also stay updated by following us on Twitter, becoming a fan on Facebook or by subscribing to our FriendFeed.


Introducing Guild 3 Software

Three of the Elegant Coders, myself among them, have an announcement to make. Jason Grundy, Jarod Ferguson, and I have all left good, stable jobs to do something we’ve all wanted to do for a long time.guild3_logo_green_white_B..

We’re starting a software company!

Specifically, we have partnered together to open the doors on Guild 3 Software. Readers of Elegant Code are probably familiar with our writing and interests, so hopefully you can guess what we’re about.

Jarod, Jason, and I are strong believers in the ideals set forth in the Manifesto for Agile Software Development and in the Manifesto for Software Craftsmanship. Our shared appreciation for the craft of software development has always drawn us into a huddle at user group meetings and other places where developers gather. We are excited to bring this passion to our customers and our products. Accordingly Guild 3 is founded on four core values:

Integrity, Craftsmanship, Agility, and Community.

You can read more about these, our founding values, here.

But, What Are You Doing?

Several things.

We are working on some exciting, super-secret product opportunities. Maybe they’ll be more on that another time.

In addition, Guild 3 is a professional services organization committed to bringing the ideals of software craftsmanship to our work. We are already working with a few clients and delivering software, of course!

Further, we are participating in community events, conferences, and other opportunities to learn from our peers.

Regarding Elegant Code

To be very clear, ElegantCode.com is not a business. It is a blogging community of professional software developers who simply enjoy each other’s contributions. We will all stay actively involved in ElegantCode.com and will retain our blogging sites here.

Wouldn’t have it any other way.

On a personal note, this doesn’t change my status as a Pluralsight instructor. That’s still a large part of my life. Hopefully I’ll get a chance to visit your company some day.

Shameless Appeal for Work

Surely you saw this coming!

If you have any software development projects you think might be a good fit for an intensely focused group of software craftsmen, give us a ring.


  • Sponsored Links

  •  

  • .

    Copyright © 1996-2010 Answer My Query. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress