<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://eduncan911.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Eric Duncan : Asp.Net Mvc, Geek Stuff</title><link>http://eduncan911.com/archive/tags/Asp.Net+Mvc/Geek+Stuff/default.aspx</link><description>Tags: Asp.Net Mvc, Geek Stuff</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Managed Extensibility Framework (MEF) - Microsoft's Official Inversion of Control Container</title><link>http://eduncan911.com/blog/managed-extensibility-framework-mef-microsofts-official-inversion-of-control-container.aspx</link><pubDate>Tue, 16 Feb 2010 00:50:00 GMT</pubDate><guid isPermaLink="false">3cbf8099-f611-4197-a0f5-c5a9f8954971:6964</guid><dc:creator>Eric A. Duncan</dc:creator><slash:comments>10</slash:comments><comments>http://eduncan911.com/comments/6964.aspx</comments><wfw:commentRss>http://eduncan911.com/commentrss.aspx?PostID=6964</wfw:commentRss><wfw:comment>http://eduncan911.com/rsscomments.aspx?PostID=6964</wfw:comment><description>&lt;p&gt;This new Microsoft feature in .NET Framework 4 is a programming model for reusing components, very similar to those Inversion of Control container frameworks out there that we all know and love (Castle Windsor, Structured Map, their own Unity from the Patterns and Practices team and etc).&amp;nbsp; Now, they have built their own to address the pain points of these frameworks called the Managed Extensibility Framework, or MEF for short.&lt;/p&gt; &lt;p&gt;I was pleasantly surprised this weekend when reading the February 02010's issue of MSDN Magazine.&amp;nbsp; Getting up-to-speed on some new features in .NET Framework 4, I saw the headline "&lt;a href="http://msdn.microsoft.com/en-us/magazine/ee291628.aspx" target="_blank"&gt;Building Composable Apps in .NET 4 with the Managed Extensibility Framework&lt;/a&gt;" by Glenn Block.&amp;nbsp; Yeah, it just rolls off your tongue eh?&amp;nbsp; So I decided I had a few minutes while waiting for the water to boil and scanned the article.&amp;nbsp; I am glad I did.&lt;/p&gt; &lt;p&gt;In this post, I will try to give a very brief overview on how to use MEF coming from a background of using other Inversion of Control (IoC) containers, including some of the gotchas that you must be aware of during the switch.&amp;nbsp; Yeah, you will want to make the switch - the switch away from those other bulky frameworks.&amp;nbsp; I, for one, am officially announcing my abandonment of Castle and Unity for all future projects.&amp;nbsp; Because once you add-in .NET 4's new data annotations for seriously improved validation, mixed with MEF, the only other 3rd party component I am left with is Castle's Logger abstraction - which I am hoping I can find a replacement for with .NET 4's new features.&lt;/p&gt; &lt;h2&gt;MEF Terminology&lt;/h2&gt; &lt;p&gt;First and fore-most for those of you already using an IoC container, it's time for a quick review on what MEF calls a few things and concepts.&amp;nbsp; Below is a table I whipped up to help compare the two terminologies (the MEF portions largely taken from Glenn's excellent article).&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="2-15-2010 10-41-32 PM" border="0" alt="2-15-2010 10-41-32 PM" src="http://eduncan911.com/blog/thumbnail/ManagedExtensibilityFrameworkMicrosoftsO_A9C4/2152010104132PM_3.png" width="832" height="649"&gt; &lt;/p&gt; &lt;p&gt;Below is an image from Glenn's article that helps visualize the concepts above of MEF.&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="ee291628_Block_Fig1(en-us,MSDN_10)" border="0" alt="ee291628_Block_Fig1(en-us,MSDN_10)" src="http://eduncan911.com/blog/thumbnail/ManagedExtensibilityFrameworkMicrosoftsO_A9C4/ee291628_Block_Fig1enusMSDN_10.png" width="413" height="288"&gt; &lt;/p&gt; &lt;p&gt;You can see that the overall concept is called Composition.&amp;nbsp; A 'part' is the type or service you want to export.&amp;nbsp; The part (or parts) is marked for Export via Contracts (which are automatic).&amp;nbsp; And other parts that want to ask for another part can do so by Importing.&lt;/p&gt; &lt;h2&gt;Managed Extensibility Framework - Why they built it&lt;/h2&gt; &lt;p&gt;Glenn mentions in the article that they needed a way to compose of reusable types or components in the up coming Visual Studio 2010, Oslo, Acropolis and I am sure many more.&amp;nbsp; What is important to note is why they created one themselves, instead of using Unity or alike.&amp;nbsp; Glenn mentions some key points:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Third-party Extensibility (allowing vendors to generate plug-ins for Visual Studio, or your own application, with minimal work on your part now)  &lt;li&gt;Other frameworks were too heavyweight for a simple programming model.&amp;nbsp; &lt;li&gt;Or other frameworks required too much effort on the part of either the host or the extension developer. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Those last two is what struck a cord with me, and got my attention.&amp;nbsp; Try to sit back and think about how you would allow your applications to be extended with Castle, StructureMap or even Unity.&amp;nbsp; That's a lot of work, a lot of 3rd party assemblies to wire up and configure properly.&amp;nbsp; Especially if you want runtime changes, which MEF supports out of the box with no configuration.&lt;/p&gt; &lt;p&gt;That last point is particularly interesting.&amp;nbsp; All too often I see a released developer framework or contrib project by a group of inspired individuals.&amp;nbsp; This is great and all, but those projects quickly grow to be a large project requiring documentation, support, and even refactorings for performance.&lt;/p&gt; &lt;p&gt;It got me thinking a lot about what I see wrong with Castle - only two things, it's learning curve and size.&amp;nbsp; While Unity was very well documented, it comes with some significant bloat and only what I can refer to as hoop-jumping.&amp;nbsp; The earlier versions of Unity violated Dependency Injection pricipals and concepts (no ctor injection!).&amp;nbsp; Perfect example of refactoring.&lt;/p&gt; &lt;p&gt;MEF really strives to resolve all of these issues, with some extremely simple programming models that I will show you below.&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;h2&gt;MEF Programming Models - Straight Attributed Declarations&lt;/h2&gt; &lt;p&gt;Another rip from Glenn's article.&amp;nbsp;&amp;nbsp; But he did it so well.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Developers consume MEF through a programming model.&amp;nbsp; A programming model provides a means to declare components as MEF parts.&amp;nbsp; Out of the box, MEF provides an attributed programming model, which will be the main focus of this article.&amp;nbsp; That model is just one of many possible programming models that MEF enables.&amp;nbsp; MEF's core API is completely agnostic to attributes.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Yep, you heard that right.&amp;nbsp; MEF supports Attributes - for out-of-the-box functionality.&amp;nbsp; No configuration, no setup, nothing.&amp;nbsp; This is so much easier than you are even thinking.&amp;nbsp; So much so, I feel guilty by writing all of this text.&amp;nbsp; Let's just show you how it's done now.&lt;/p&gt; &lt;h2&gt;The Blog "Hello World" Snippets&lt;/h2&gt; &lt;p&gt;As what seems to be an invasion, the common Hello World for websites these days &lt;a href="http://invalidlogic.com/2008/12/22/blogging-apps-are-the-new-hello-world/" target="_blank"&gt;are blogs&lt;/a&gt;.&amp;nbsp; So without further delay, here's some code snippets.&lt;/p&gt; &lt;p&gt;The post object&lt;/p&gt; &lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
public partial class Post
{
	public Int32 PostId { get; set; }
	public String Title { get; set; }
	public String Description { get; set; }
	public String Body { get; set; }
	public Guid AuthorId { get; set; }
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;And the PostService that will service the post.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
[Export(typeof(IPostService))]
public class PostService : IPostService
{
	[Import]
	public IPostRepository PostRepository { get; set; }

	public Post FetchById(Int32 postId)
	{
		return
			(from p in PostRepository.GetAll()
			 where p.PostId == postId
			 select p as Post).FirstOrDefault();
	}
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Please note that the above code is not production-code.&amp;nbsp; You should always provide proper cache, concurrency and disposable patterns.&amp;nbsp; This code has been cleaned up for easier reading.&lt;/p&gt;
&lt;p&gt;Notice that the PostService has been marked to be exportable, using a specific contract type for IPostService. Now, you do not have to specify the contract type or name. You can simply use [Export]. But remember, we do want to allow for simply extensibility in the future by plugging in different components to be served during composition.&amp;nbsp; How to do that?&amp;nbsp; Easy, just specify the interface type as the contract.&amp;nbsp; Now how easy was that?&lt;/p&gt;
&lt;p&gt;Also, notice that we have a dependency on IPostRepository. This is marked with the simple [Import] attribute.&amp;nbsp; It does exactly as you think.&amp;nbsp; MEF composes the first part that matches the contract for IPostRepository and supplies it &lt;/p&gt;
&lt;p&gt;Now, some will cry fowl here since I am not directly injecting IPostRepository, or not demanding it in the constructor. Yes, MEF fully supports constructor injection. But, there is a few gotchas you have to be aware of with MEF and constructor composition that I get into a little further down. There's also a new pattern I am designing, when mixed with .NET 4 and some nifty T4 templates I'm tinkering with. That's another article I'll write though. Check the comments, or leave a comment for more information on, "Managing BDD Contexts and Mocks Automatically with .NET 4, MEF, and T4 Templates." Nice. I just named my next blog post. &lt;/p&gt;
&lt;h2&gt;Constructor Dependency Injection - MEF Gotcha&lt;/h2&gt;
&lt;p&gt;As mentioned above, constructor injection is a bit tricky with MEF.&amp;nbsp; There are some rules to follow:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MEF assumes all parameters are imports, making the import attribute unnecessary.&amp;nbsp; I leave it up to the reader to figure out how to get around this. 
&lt;li&gt;Recomposition is not supported on constructor parameters.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;That last one is almost a deal breaker for me.&amp;nbsp; Recomposition is a feature of MEF that allows parts to automatically have their imports updated as new matching exports appear in the system.&amp;nbsp; Given, this isn't the case with most websites out there.&amp;nbsp; It's an issue for websites and applications that want to support dynamic (e.g. downloadable) plugins.&amp;nbsp; The plugins would overwrite, at runtime, certain contracts and therefore allow the parts to automatically be updated with the new plugin versions - all without an application restart!&lt;/p&gt;
&lt;p&gt;So with that said (and you will never be developing plugins right?), you can use constructor injection with MEF as follows.&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code&gt;
[Export(typeof(IPostService))]
public class PostService : IPostService
{
	private IPostRepository _postRepo;
	private IUserRepository _userRepo

	[ImportingConstructor]
	public PostService(IPostRepository postRepo, IUserRepository userRepo)
	{
		_postRepo = postRepo;
		_userRepo = userRepo;
	}

	public Post FetchById(Int32 postId)
	{
		return
			(from p in _postRepo.GetAll()
			 where p.PostId == postId
			 select p as Post).FirstOrDefault();
	}
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Don't fret too much though about the all-or-nothing constructor.&amp;nbsp; You have a Factory option later on in this post.&lt;/p&gt;
&lt;h2&gt;Bootstrapping MEF for Application Startup&lt;/h2&gt;
&lt;p&gt;Glenn nailed it right on the head when he called this procedure "bootstrapping."&amp;nbsp; I've always struggled with the proper terminology for this composition process with other Inversion of Control containers.&amp;nbsp; Bootstrapping.&amp;nbsp; Yep, just like my hacked MP3 players have.&lt;/p&gt;
&lt;p&gt;To get things up and running, you will need to to insert some bootstrapping code for your application.&amp;nbsp; This code is required with any type of IoC container, and with MEF as well.&lt;/p&gt;
&lt;p&gt;Below is a small code snippet from a typical ASP.NET MVC 2 global.asax file.&amp;nbsp; Notice I added the two using blocks at the end of the Application_Start().&amp;nbsp; This process would be the same for any type of application you have.&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code&gt;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

protected void Application_Start()
{
	AreaRegistration.RegisterAllAreas();

	RegisterRoutes(RouteTable.Routes);

	using (var catalog = new DirectoryCatalog(@".\"))
	{
		using (var container = new CompositionContainer(catalog))
		{
			container.ComposeParts(this);
		}
	}
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now, this code is running from with the website assembly.&amp;nbsp; If you, like me, create a seperate assembly to hold your Domain and Repository models, then you simply reference the assembly by replacing the 'this' keyword with Assembly.GetAssembly(typeof(MyProject.Domain)).&amp;nbsp; And yes, you can reference multiple catalogs and assemblies and build up your list as well.&lt;/p&gt;
&lt;p&gt;Note that I included the namespaces here.&amp;nbsp; This is important as normally this bootstrapping code is in the Hosting namespace.&amp;nbsp; What they don't tell you is there is an extension in the namespace one higher for CompositionContainer, that enables the ComposeParts() extension method in the System.ComponentModel.Composition namespace.&lt;/p&gt;
&lt;p&gt;There are tons of options here with the composition containers.&amp;nbsp; Please refer to the .NET 4 documentation on MEF, as well as Glenn's article for a few more tips.&amp;nbsp; You can specify an assembly to reference for the catalog.&amp;nbsp; You can have aggregated catalogs for grouping/namespacing purposes.&amp;nbsp; And yet, you can still have that aged-old configuration file if you really really really want to manually register (export) each type.&lt;/p&gt;
&lt;h2&gt;All Parts are Singletons, out of the box&lt;/h2&gt;
&lt;p&gt;Yep.&amp;nbsp; All of your part's instances are composed and referenced throughout all of your code as singletons (or "statics").&amp;nbsp; This is a great solution for that stubborn developer you may have working in your group that just won't let go of static managers.&amp;nbsp; Make it instance based, and slap an [Export] on it.&amp;nbsp; Done, now use [Import] wherever you like.&lt;/p&gt;
&lt;p&gt;This is important to note because Castle and StructuredMap both default to transient composition (Unity uses whatever you configured the container to use as default).&amp;nbsp; So, make sure you are coding to be thread-safe if you are making the switch over to MEF.&lt;/p&gt;
&lt;p&gt;But do not fret, it's easy to specify the lifestyle within the contract definitions.&amp;nbsp; There are three possible configurations you can set with the [PartCreationPolicy()] attribute:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CreationPolicy.Shared (singleton, default) 
&lt;li&gt;CreationPolicy.NonShared (transient) 
&lt;li&gt;CreationPolicy.Any&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The "Any" option is interesting as it leaves it up to the context configuration of the host and/or the configuration of the [Import] contract to specify.&amp;nbsp; You can use RequiredCreationPolicy on the [Import] contract definition to specify the preference.&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code&gt;
[PartCreationPolicy(CreationPolicy.Any)]
[Export(typeof(IUserAccountService))]
public class UserAccountService : IUserAccountService
{
	...
}

public class Post
{
	public Int32 PostId { get; set; }
	public String Title { get; set; }
	...

	[Import(RequiredCreationPolicy=CreationPolicy.NonShared]
	public IUserService UserService { get; set; }
}

&lt;/pre&gt;&lt;/code&gt;
&lt;p&gt;There is a problem with this concept though.&amp;nbsp; With a current project we are working on at the moment, we identified the need to have a security context around our services to ensure the current WebRequest has the correct security credentials.&amp;nbsp; With this, we elected to use Castle's WebRequest lifestyle feature.&amp;nbsp; Sadly, this is not possible (yet) with the PartCreationPolicy.&amp;nbsp; So it is left up to the implementer to handle custom composition on their own with a Factory pattern (see below).&lt;/p&gt;
&lt;h2&gt;Lazy Loading within Entities&lt;/h2&gt;
&lt;p&gt;Those of you that are wanting Lazy Loading within your entities, but are struggling with a solution that is compatible with Inversion of Control, fear not!&amp;nbsp; The answer is now possible with MEF.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
public partial class Post
{
	[Range(0, Int32.MaxValue)]
	public Int32 PostId { get; set; }
	
	[Required, StringLength(64, MinimumLength = 5)]
	public String Title { get; set; }

	[Required, StringLength(1024, MinimumLength = 5)]
	public String Description { get; set; }

	[Required, StringLength(Int32.MaxValue, MinimumLength = 5)]
	public String Body { get; set; }

	[Required]
	public Guid AuthorId { get; set; }

	[Import]
	public IUserService UserService { get; set; }

	private User _user;
	public User Author
	{
		get
		{
			if (_user == null)
				_user = UserService.FetchById(this.AuthorId);
			return _user;
		}
	}
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With our updated Post entity, notice we now have a dependency on IUserService marked with the [Import] attribute.&amp;nbsp; Yep, MEF composes that for you, and you have the UserService to access your lazy objects as needed.&lt;/p&gt;
&lt;h2&gt;Using MEF as a Static Wrapper&lt;/h2&gt;
&lt;p&gt;Time and time again I find myself writing wrapper classes around static members of a 3rd party component, just so I can unit test my code without having to rely on that static class.&amp;nbsp; Using MEF, if you haven't guesses already, is just as easy as you might think.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
public class Loggerpart
{
	[Export]
	public ILogger Logger
	{
		get
		{
			return LogManager.GetLogger("LoggerInstance", "config");
		}
	}
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This pattern allows you to make a wrapper around any 3rd party or legacy code.&lt;/p&gt;
&lt;h2&gt;MEF Composition with a Factory Pattern&lt;/h2&gt;
&lt;p&gt;Here's another one not in Glenn's article.&amp;nbsp; How to use MEF with a factory pattern to initiate a complex type.&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code&gt;
public class UserService : IUserService
{
	public UserService(ISecurityContext securityContext)
	{
		...
	}
}

public class UserServiceFactory
{
	[Export(typeof(IUserService))]
	[PartCreationPolicy(CreationPolicy.Shared)]
	public IUserService Instance
	{
		get
		{
			var context = HttpContext.Current;
			var securityContext = 
				SecurityContextProvider.Setup(context)
			return new UserService(securityContext);
		}
	}
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Notice how the UserService is not exported?&amp;nbsp; Instead, we designate a property member of UserServiceFactory called Instance as the Export composition location.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Remember, you are only Exporting for Composition at runtime.&amp;nbsp; Nothing gets composed during your unit tests, as you are mocking them.&amp;nbsp; So anywhere you use [Import] will be still be mockable for any of your unit tests with this pattern.&lt;/p&gt;
&lt;h2&gt;Exporting with Multiple Contracts&lt;/h2&gt;
&lt;p&gt;Here's a nice trick, you can specify multiple Export contracts for multiple types.&amp;nbsp; Why would you do this?&amp;nbsp; If you are a big DDD follower, you may be using IUserService and IAccountService combined to give you an UserAccountService part.&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code&gt;
[Export(typeof(IUserService))]
[Export(typeof(IAccountService))]
public class UserAccountService : IUserService, IAccountService
{
	...
}&lt;/code&gt;&lt;code&gt;
&lt;/pre&gt;&lt;/code&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;MEF Does Not Blow Up on Rejection&lt;/h2&gt;
&lt;p&gt;This one is going to take some getting used to.&amp;nbsp; As Glenn mentions, MEF does not throw exceptions if a part cannot be satisfied.&amp;nbsp; It simply will not exist in the object graph for MEF to return - you'll get a null, and will most likely see a NullReferenceException in your containing code that is trying to use the part you wanted to import.&lt;/p&gt;
&lt;p&gt;MEF will simply reject the request for the Import of a part for a number of reasons.&amp;nbsp; For example, if there is no part defined as the correct Export contract type.&amp;nbsp; Say you wanted to import IUserService, but only exported UserService with [Export] and no contract type of IUserService was defined.&amp;nbsp; That was the typical one I found myself forgetting to do.&amp;nbsp; As recommended above, always include the Contract Type when designated a part as Export with [Export(typeof(IUserService))].&lt;/p&gt;
&lt;p&gt;I agree that MEF's Rejection policy is a power feature because it stabilizes the code and allows debugging.&amp;nbsp; Glenn links to a good article on why to ensure application stability: &lt;a title="http://blogs.msdn.com/gblock/archive/2009/08/02/stable-composition-in-mef-preview-6.aspx" href="http://blogs.msdn.com/gblock/archive/2009/08/02/stable-composition-in-mef-preview-6.aspx"&gt;http://blogs.msdn.com/gblock/archive/2009/08/02/stable-composition-in-mef-preview-6.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But in short, if you get Rejection happening quite often, check your Export contract definition on your part.&lt;/p&gt;
&lt;h2&gt;In Summary&lt;/h2&gt;
&lt;p&gt;As you can see, it is dead simple to use MEF.&amp;nbsp; The Export functionality is what was missing with Unity, and completely with all other IoC containers.&amp;nbsp; And, it's what gives MEF such great, simplistic power.&amp;nbsp; I highly recommend reading through Glenn's complete article, as he covers a few other concepts such as using the new &lt;strong&gt;Lazy&amp;lt;T&amp;gt;&lt;/strong&gt; for importing lazy exports and metadata.&lt;/p&gt;
&lt;p&gt;Some additional things Glenn covers is the very strong support for debugging and tracing, things you want to be aware of if you use MEF even moderately.&amp;nbsp; He also hints at the up-n-coming support for MEF with IronRuby.&amp;nbsp; He also covers some external links, which I will list here (for my own archiving purposes):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx" target="_blank"&gt;Customizing Container Behavior Part 2 of N - Defaults&lt;/a&gt; for Facilities 
&lt;li&gt;&lt;a href="http://mef.codeplex.com/releases/view/33536" target="_blank"&gt;MEF Analysis Tool (mefx) for .NET 4.0&lt;/a&gt; for debugging those contracts in large projects 
&lt;li&gt;&lt;a href="http://blogs.msdn.com/dsplaisted/archive/2009/06/08/a-crash-course-on-the-mef-primitives.aspx" target="_blank"&gt;A Crash Course on the MEF Primitives&lt;/a&gt; for the underlying "quantum universe of MEF, its über extensibility points" as Glenn says&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Once concept I am tinkering with is utilizing the [Import] attribute in the attributed programming model of MEF to define the complete contexts of my BDD tests for me with all dependencies already mocked up and ready, with T4 templates largely driving that effort.&lt;/p&gt;
&lt;p&gt;MEF wacked me upside the head.&amp;nbsp; It does everything right, and even allowed me to step back and realized, "Dang. I was bloating my code."&amp;nbsp; Yes, they have done it right.&amp;nbsp; So right that the next project I am starting this week will be on Visual Studio 2010 RC - not even released yet.&amp;nbsp; &lt;/p&gt;&lt;img src="http://eduncan911.com/aggbug.aspx?PostID=6964" width="1" height="1"&gt;</description><category domain="http://eduncan911.com/archive/tags/Geek+Stuff/default.aspx">Geek Stuff</category><category domain="http://eduncan911.com/archive/tags/Computer+Programming/default.aspx">Computer Programming</category><category domain="http://eduncan911.com/archive/tags/Asp.Net+Mvc/default.aspx">Asp.Net Mvc</category><category domain="http://eduncan911.com/archive/tags/MSpec/default.aspx">MSpec</category><category domain="http://eduncan911.com/archive/tags/Inversion+of+Control/default.aspx">Inversion of Control</category><category domain="http://eduncan911.com/archive/tags/MEF/default.aspx">MEF</category></item><item><title>Html.RenderAction for ASP.NET MVC 1.0</title><link>http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx</link><pubDate>Thu, 24 Sep 2009 16:04:00 GMT</pubDate><guid isPermaLink="false">3cbf8099-f611-4197-a0f5-c5a9f8954971:6792</guid><dc:creator>Eric A. Duncan</dc:creator><slash:comments>8</slash:comments><comments>http://eduncan911.com/comments/6792.aspx</comments><wfw:commentRss>http://eduncan911.com/commentrss.aspx?PostID=6792</wfw:commentRss><wfw:comment>http://eduncan911.com/rsscomments.aspx?PostID=6792</wfw:comment><description>&lt;P&gt;The &lt;A href="http://eduncan911.com/blog/type-safety-with-asp-net-mvc-futures.aspx"&gt;ASP.NET MVC 1.0 Futures assembly&lt;/A&gt; (that is not included with ASP.NET MVC 1.0) has a powerful HtmlHelper extension method called RenderAction().&amp;nbsp;&amp;nbsp; It sounds similar to the Html extension method called RenderPartial() for a good reason.&amp;nbsp; RenderAction() executes an action on a controller, allowing you to move your logic out of the views.&lt;/P&gt;
&lt;H2&gt;The problem with Html.RenderPartial()&lt;/H2&gt;
&lt;P&gt;There is no direct problem with this.&amp;nbsp; It is just that developers are left with only the ability to render a Partial View.&amp;nbsp; What if you want that partial view to act on model properties you pass into that partial view?&amp;nbsp; What if you need to access the ambient values in the Routes collection to render some specifics?&amp;nbsp; Unfortunately, developers are only left with RenderPartial() which only gives you access to a partial view.&amp;nbsp; If you really need this logic, you have no choice but to put it into the partial view.&amp;nbsp; Or, to put it into your controller’s action method that calls the view that calls the partialviews.&amp;nbsp; Needless to say, that’s a bit hokey.&lt;/P&gt;
&lt;H2&gt;What Html.RenderAction&amp;lt;TController&amp;gt;() Resolves&lt;/H2&gt;
&lt;P&gt;It gives you the power of moving that complex View and Partial View logic to a Controller’s Action, where it belongs!&amp;nbsp; Think of it as a “Render Partial Action” method of where you can call back into a controller to render some logic. This gives you the ability to clean up your partial views now by removing that logic and placing it on a controller’s action, that renders the partial view when done. The syntax looks like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;lt;% Html.RenderAction&amp;lt;ProductController&amp;gt;(c =&amp;gt; c.RenderProductResults()); %&amp;gt;&lt;BR&gt;&lt;/CODE&gt;&lt;CODE&gt;&amp;lt;!-- Or... //--&amp;gt;&lt;BR&gt;&amp;lt;%= Html.RenderAction("RenderPartialResults", "Product"); %&amp;gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Put simply, this renders an action on a controller directly in your view.&amp;nbsp; You may be thinking “big deal”, but I assure you this is a big deal in large complex MVC sites.&amp;nbsp; Having the power to abstract or breakup your controller’s actions into multiple &lt;EM&gt;partial-actions&lt;/EM&gt; and multiple views is very powerful.&amp;nbsp; Instead of relying on the 1 controller' action to wire up all of the data for all of the views, partial views, and logic for the views.&amp;nbsp; Now, you can just focus on that one section - and abstract the rest into reusable parts.&lt;/P&gt;
&lt;P&gt;You may be wondering where this extension method is with your ASP.NET MVC project.&amp;nbsp; As mentioned above, it is part of the &lt;A href="http://eduncan911.com/blog/type-safety-with-asp-net-mvc-futures.aspx"&gt;Futures extension of the ASP.NET MVC project at codeplex&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;ASP.NET MVC Sidebar Widget Example&lt;/H2&gt;
&lt;P&gt;The current pattern suggests you use Html.RenderPartial to render those partial views as your sidebar widgets.&amp;nbsp; What if you want those widgets to be more complex?&amp;nbsp; What if you want those widgets to act on the the current Route?&amp;nbsp; Well, you are left with little options with RenderPartial.&amp;nbsp; Instead, you want to use RenderAction to call an action on a controller to handle that logic.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Assume we are viewing a blog post entry and on the right, we want a sidebar widget for related posts. To do this, first create an action on a controller called RelatedPosts().&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;public ActionResult RelatedPosts(Int32 postID)
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;  // some complex logic, or simple logic, can go here now…&lt;BR&gt;  //&lt;BR&gt;  &lt;BR&gt;  if (postID &amp;lt; 1)&lt;BR&gt;    return PartialView(“NoRelatedPosts”);&lt;BR&gt;&amp;nbsp; var relatedPosts =&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _postService.FetchRelatedPosts(postID);&lt;BR&gt;  &lt;BR&gt;&amp;nbsp; if (relatedPosts.Count &amp;gt; 0)&lt;BR&gt;&amp;nbsp;&amp;nbsp; return PartialView("RelatedPosts", relatedPosts);&lt;BR&gt;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return PartialView(“NoRelatedPosts”);
}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice how the logic here accounts for empty or no results, and returns an a different partial view?&amp;nbsp; Using RenderPartial(), this is logic could only be reflected with inline IF ELSE brackets within your PartialView - and ugly spaghetti mess.&amp;nbsp; Also, how would you even retrieve the related posts collection?&amp;nbsp; You have no choice but to obtain that collection back on the post entry view action - which should not be concerned about our little sidebar widget.&amp;nbsp; All it should do is wire up the post.&amp;nbsp; But, that is not the case with RenderPartial() - you have to wire up all of your data ahead of time in one large ViewModel with multiple entities dangling off of it.&lt;/P&gt;
&lt;P&gt;No no.&amp;nbsp; Let’s do it with RenderAction() and instead put this logic into the Controller (where it belongs, so we can test for it); and then, we can call it with the RenderAction() extension.&amp;nbsp; We can do this now with a simple call within our larger post view like so:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;lt;% Html.RenderAction&amp;lt;PostController&amp;gt;(c =&amp;gt; c.RelatedPosts(Model.PostID)); %&amp;gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;We instead kind of render a &lt;EM&gt;partial action&lt;/EM&gt; by calling an action on a controller that handles the logic that we would otherwise have to put in the view.&amp;nbsp; Now, our main post view action doesn’t have to be concerned about wiring up our sidebar partial view.&amp;nbsp; We can just call RenderAction() in the view.&lt;/P&gt;
&lt;H2&gt;ASP.NET MVC AJAX Example using RenderAction&lt;/H2&gt;
&lt;P&gt;Taking an example from the book &lt;A href="http://www.amazon.com/gp/product/0470384611?ie=UTF8&amp;amp;tag=eduncan911com-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0470384611"&gt;Profressional ASP.NET MVC 1.0&lt;/A&gt;, we’ll use the submitting a Form Using Ajax example to replace the limited RenderPartial() function with a richer RenderAction() from a Controller’s action to process the results more finely.&lt;/P&gt;
&lt;P&gt;In the ProductController, you would add a new method with the signature RenderProductResults(IList&amp;lt;Product&amp;gt; products):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;public ActionResult RenderProductResults(IList&amp;lt;Product&amp;gt; products)
{
    // insert some custom logic here, maybe even switch partial views, etc
    //
    if (products.Count &amp;gt; 0)
        return PartialView("ProductSearchResults", products);
    else
        return EmptyResult();
}    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, you can update their ajax example to render this new action instead of the RenderPartial() from their example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;lt;h1&amp;gt;Product Search - jQuery&amp;lt;/h1&amp;gt;
&amp;lt;form action="&amp;lt;%= Url.Action("ProductSearch") %&amp;gt;" method="post" id="jform"&amp;gt;

  &amp;lt;%= Html.TextBox("query", null, new { size=40 }) %&amp;gt;
  &amp;lt;input type="submit" id="jsubmit" value="go" /&amp;gt; 

&amp;lt;/form&amp;gt;

&amp;lt;div id="results2"&amp;gt;
  &amp;lt;% Html.RenderAction&amp;lt;ProductController&amp;gt;(c =&amp;gt; c.RenderProductResults(Model.Results)); %&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;script src="http://eduncan911.com/Scripts/jquery-1.3.2.js" type="text/javascript"&amp;gt; &amp;lt;/script&amp;gt;&lt;BR&gt;&amp;lt;script src="http://eduncan911.com/Scripts/jquery-form.js" type="text/javascript"&amp;gt; &amp;lt;/script&amp;gt;&lt;BR&gt;&amp;lt;script&amp;gt; &lt;BR&gt;  $(function() { &lt;BR&gt;    $('#jform').submit(function(){ &lt;BR&gt;      $('#jform').ajaxSubmit({ target: '#results2' }); &lt;BR&gt;      return false; &lt;BR&gt;    }); &lt;BR&gt;  });&lt;BR&gt;&amp;lt;/script&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Look for the Html.RenderAction line above.&amp;nbsp; What this does is instead of rendering a partial view in the id=”results2” location, you can now render a “partial Action” with the Html.RenderAction() call.&amp;nbsp; Notice that how we are also passing strongly typed parameters directly into the method?&amp;nbsp; Yep, fully supported.&lt;/P&gt;
&lt;P&gt;By using the RenderAction instead of RenderPartial, you have much more control over what happens with that rendering of the partial (now, rendering of the action).&amp;nbsp; &lt;EM&gt;&lt;U&gt;This gives you an excellent opportunity to remove that complex view logic you may have, and place it in an Action where it belongs&lt;/U&gt;&lt;/EM&gt;!&lt;/P&gt;
&lt;H2&gt;Custom ViewEngines and Extensions&lt;/H2&gt;
&lt;P&gt;Now, you can also resolve this logic issue by creating custom ViewEngines, or extensions that expand upon the ViewEngine, HtmlHelper, or UrlHelper.&amp;nbsp; But, I see those methods as more application-wide common logic (like pagers and server controls for display name).&amp;nbsp; I do not see that as a solution for your one-off partial view for the ajax response to something.&amp;nbsp; Think of how bloated your ViewEngine would get, or how many different ViewEngines you’d have to choose from.&amp;nbsp; No, ViewEngine extensions have a place which I will post about as well.&lt;/P&gt;
&lt;H2&gt;Summary&lt;/H2&gt;
&lt;P&gt;When your views start getting complex and messy, it may be time to switch to a RenderAction to handle that logic.&amp;nbsp; I know I have cleaned up quite a lot using it.&amp;nbsp; Also, custom view engines have a place in their own right to abstract more global/common logic across the entire site.&lt;/P&gt;&lt;img src="http://eduncan911.com/aggbug.aspx?PostID=6792" width="1" height="1"&gt;</description><category domain="http://eduncan911.com/archive/tags/Geek+Stuff/default.aspx">Geek Stuff</category><category domain="http://eduncan911.com/archive/tags/Computer+Programming/default.aspx">Computer Programming</category><category domain="http://eduncan911.com/archive/tags/Asp.Net+Mvc/default.aspx">Asp.Net Mvc</category><category domain="http://eduncan911.com/archive/tags/Type+Safety/default.aspx">Type Safety</category></item><item><title>Type Safety with ASP.NET MVC Futures</title><link>http://eduncan911.com/blog/type-safety-with-asp-net-mvc-futures.aspx</link><pubDate>Tue, 22 Sep 2009 17:02:29 GMT</pubDate><guid isPermaLink="false">3cbf8099-f611-4197-a0f5-c5a9f8954971:6788</guid><dc:creator>Eric A. Duncan</dc:creator><slash:comments>3</slash:comments><comments>http://eduncan911.com/comments/6788.aspx</comments><wfw:commentRss>http://eduncan911.com/commentrss.aspx?PostID=6788</wfw:commentRss><wfw:comment>http://eduncan911.com/rsscomments.aspx?PostID=6788</wfw:comment><description>&lt;p&gt;In this post, I will show you how to strongly type some parts of MVC to get rid of those “magic strings.”&amp;#160; I will also publish a few extension methods that expand upon this concept later on.&amp;#160; So, this is a continuation of my &lt;a href="http://eduncan911.com/blog/asp-net-mvc.aspx"&gt;Mvc series&lt;/a&gt; that you can subscribe to.&lt;/p&gt;  &lt;h2&gt;ASP.NET MVC 1.0 Futures&lt;/h2&gt;  &lt;p&gt;You can download ASP.NET MVC 1.0 RTM from codeplex.&amp;#160; You can even download the source code and step through what the framework is doing.&amp;#160; What a lot of people overlook is an additional assembly available for download at these locations called ASP.NET MVC 1.0 Futures.&amp;#160; You can download it from Codeplex from below:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471" target="_blank"&gt;Download ASP.NET MVC 1.0 Futures from CodePlex&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This is a darling of an assembly using the namespace &lt;strong&gt;Microsoft.Web.Mvc&lt;/strong&gt;.&amp;#160; Usually these Microsoft “Futures” releases are developer code that did not get approved for RTM release.&amp;#160; This assembly is no exception and includes a plethora of strongly typed extensions.&amp;#160; The reason some of this code did not get approved was because some of it did not conform to Medium Trust requirements for the 1.0 release.&amp;#160; So if you have a strict medium trust application, you may not be able to use the some of the code contained within the assembly.&lt;/p&gt;  &lt;p&gt;I am not going to cover all of the extensions within this assembly.&amp;#160; You can do that on your own with &lt;a href="http://www.red-gate.com/products/reflector/index.htm" target="_blank"&gt;.NET Reflector&lt;/a&gt;.&amp;#160; Instead, I am going to cover just a couple that you will be using on a daily basis.&lt;/p&gt;  &lt;p&gt;To get started, go ahead and added a reference to the Microsoft.Web.Mvc assembly to your project.&amp;#160; In addition, to make things a lot easier, go ahead and add a few namespaces to your web.config.&lt;/p&gt;  &lt;pre&gt;&lt;code&gt;&amp;lt;namespaces&amp;gt;
  &amp;lt;add namespace=&amp;quot;System.Web.Mvc&amp;quot;/&amp;gt;  
  &amp;lt;add namespace=&amp;quot;System.Web.Mvc.Ajax&amp;quot;/&amp;gt;
  &amp;lt;add namespace=&amp;quot;System.Web.Mvc.Html&amp;quot;/&amp;gt;
  &amp;lt;add namespace=&amp;quot;System.Web.Routing&amp;quot;/&amp;gt;
  &amp;lt;add namespace=&amp;quot;System.Linq&amp;quot;/&amp;gt;
  &amp;lt;add namespace=&amp;quot;System.Collections.Generic&amp;quot;/&amp;gt; 

  &amp;lt;add namespace=&amp;quot;AspNetMvcTypeSafety.Controllers&amp;quot;/&amp;gt;
  &amp;lt;add namespace=&amp;quot;AspNetMvcTypeSafety.Models&amp;quot;/&amp;gt;
  &amp;lt;add namespace=&amp;quot;Microsoft.Web.Mvc&amp;quot;/&amp;gt; &amp;lt;!-- Add this assembly --&amp;gt;  
&amp;lt;/namespaces&amp;gt;&lt;/code&gt;&lt;p&gt;&lt;/p&gt;&lt;/pre&gt;

&lt;p&gt;Hit CTRL-SHIFT-B to do a quick compile of your code (so the assembly is copied to your /bin). ASP.NET MVC 1.0 Futures is now available for your project.&amp;#160; You’ll also note that I add my controllers and models’ namespaces.&amp;#160; This is because you want them available from your views and controllers.&lt;/p&gt;

&lt;h2&gt;Strong Typed Html ActionLink&amp;lt;TController&amp;gt;&lt;/h2&gt;

&lt;p&gt;If you are already coding in ASP.NET MVC, you are using strings to reference controllers, actions, and views.&amp;#160; An example to access to the MostRecent() action on a PostController would be something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%-- // Old way--%&amp;gt; &lt;br /&gt;&amp;lt;%= Html.ActionLink(Model.DisplayName, &amp;quot;MostRecent&amp;quot;, new { Controller=&amp;quot;Post&amp;quot; }) %&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You have to create an anonymous type and reference a controller with “Post”, for the action method “MostRecent”.&amp;#160; Using the Futures assembly, you’ll see a few new extension overloads for Html.ActionLink:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Html.ActionLink&amp;lt;TController&amp;gt;(&lt;br /&gt;    Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, String linkText)
Html.ActionLink&amp;lt;TController&amp;gt;(&lt;br /&gt;    Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, String linkText, object attributes)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, now you can re-write the first example to get the MostRecent() action like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%-- // New way --%&amp;gt;&lt;/code&gt;&lt;code&gt;&lt;br /&gt;&amp;lt;%= Html.ActionLink&amp;lt;PostController&amp;gt;(c =&amp;gt; c.MostRecent(), Model.DisplayName)%&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Isn’t that much tidier?&amp;#160; To break it down, TController is the controller you want to access an action method for.&amp;#160; With it assigned as the generic type, now you have intellisense for your controller as the image below demonstrates.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200981403PM.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="9-23-2009 8-14-03 PM" border="0" alt="9-23-2009 8-14-03 PM" src="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200981403PM_thumb.png" width="664" height="239" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This pattern even supports the parameters on your controller’s actions!&amp;#160; For example, say you want to pass in a username property for a PostController action with the signature of MyPosts(String username).&amp;#160; Using the old method, you have to pass it an anonymous type like so (again, using strings):&lt;/p&gt;

&lt;pre&gt;&amp;lt;%-- // Old way --%&amp;gt;&lt;code&gt;&lt;br /&gt;&amp;lt;%= Html.ActionLink(Model.DisplayName, &amp;quot;MyPosts&amp;quot;, new { Controller=&amp;quot;Post&amp;quot;, id=Model.Username }) %&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With the Futures assembly, it’s a simple as this:&lt;/p&gt;

&lt;pre&gt;&amp;lt;%-- // New way --%&amp;gt;&lt;code&gt;&lt;br /&gt;&amp;lt;%= Html.ActionLink&amp;lt;PostController&amp;gt;(c =&amp;gt; c.MyPosts(Model.Username), Model.DisplayName)%&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that you pass the Model.Username parameter directly into the method c.MyPosts(), instead as an anonymous type like the old way. You even get the intellisense as well, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200982013PM.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="9-23-2009 8-20-13 PM" border="0" alt="9-23-2009 8-20-13 PM" src="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200982013PM_thumb.png" width="631" height="121" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Html RenderAction&amp;lt;TController&amp;gt;&lt;/h2&gt;

&lt;p&gt;This new function works exactly like the ActionLink&amp;lt;TController&amp;gt; above.&amp;#160; No surprises.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;%-- // new way %&amp;gt;
&amp;lt;% Html.RenderAction&amp;lt;PostController&amp;gt;(c =&amp;gt; c.MyPosts(Model.Username));%&amp;gt;&lt;code&gt; &lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can &lt;a href="http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx"&gt;read more about RenderAction&amp;lt;TController&amp;gt; here&lt;/a&gt;.&amp;#160; &lt;/p&gt;

&lt;h2&gt;Strong Typed Html BeginForm&amp;lt;TController&amp;gt;&lt;/h2&gt;

&lt;p&gt;Again, using the ASP.NET MVC Futures assembly, this is already done for you.&amp;#160; This one is a bit tricky though.&amp;#160; You want to pass your values within the form’s scope, not the BeginForm method itself.&amp;#160; But, if you leave the parameters blank for your method, the view will not render and you will be an exception.&amp;#160; Intelliense actually gives you a hint by the red underline.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200990142PM.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="9-23-2009 9-01-42 PM" border="0" alt="9-23-2009 9-01-42 PM" src="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200990142PM_thumb.png" width="504" height="93" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;To get by this, you have to trick it and pass in some default values like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200990258PM.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="9-23-2009 9-02-58 PM" border="0" alt="9-23-2009 9-02-58 PM" src="http://eduncan911.com/blog/thumbnail/StronglyTypedMVCwithASP.NETFutures_C956/923200990258PM_thumb.png" width="536" height="101" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Yes, you can still pass a direct value into the method.&amp;#160; And just like normal, if a route’s parameter does not match the name, it will be tacked on as a querystring.&amp;#160; &lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;That’s it for now.&amp;#160; There are more to capitalize on within the Futures assembly such as the &lt;strong&gt;FileCollectionModelBinder&lt;/strong&gt; for multiple files, the &lt;strong&gt;CookieTempDataProvider&lt;/strong&gt; for setting a temp cookie only for the next request, or even the HtmlHelper extensions for &lt;strong&gt;Mailto()&lt;/strong&gt; that wraps those ever daunting subject, body, multiple emails, etc all into a neat HtmlHelper.&lt;/p&gt;

&lt;p&gt;For my next project, I will be developing heavy in Mvc across multiple sites.&amp;#160; At that point, I am sure I will have more strongly typed extensions for things like RedirectToAction() at the controller level.&amp;#160; I will post them as I run across them.&lt;/p&gt;&lt;img src="http://eduncan911.com/aggbug.aspx?PostID=6788" width="1" height="1"&gt;</description><category domain="http://eduncan911.com/archive/tags/Geek+Stuff/default.aspx">Geek Stuff</category><category domain="http://eduncan911.com/archive/tags/Computer+Programming/default.aspx">Computer Programming</category><category domain="http://eduncan911.com/archive/tags/Asp.Net+Mvc/default.aspx">Asp.Net Mvc</category><category domain="http://eduncan911.com/archive/tags/Type+Safety/default.aspx">Type Safety</category></item><item><title>ASP.NET MVC</title><link>http://eduncan911.com/blog/asp-net-mvc.aspx</link><pubDate>Tue, 22 Sep 2009 17:02:13 GMT</pubDate><guid isPermaLink="false">3cbf8099-f611-4197-a0f5-c5a9f8954971:6787</guid><dc:creator>Eric A. Duncan</dc:creator><slash:comments>1</slash:comments><comments>http://eduncan911.com/comments/6787.aspx</comments><wfw:commentRss>http://eduncan911.com/commentrss.aspx?PostID=6787</wfw:commentRss><wfw:comment>http://eduncan911.com/rsscomments.aspx?PostID=6787</wfw:comment><description>&lt;p&gt;&lt;img style="border-right-width:0px;margin:0px 0px 0px 20px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="funny-pictures-cat-does-not-like-puppies" border="0" alt="funny-pictures-cat-does-not-like-puppies" align="right" src="http://eduncan911.com/blog/thumbnail/ASP.NETMVC_C748/funnypicturescatdoesnotlikepuppies.jpg" width="364" height="484" /&gt; I am starting a new series that will be focusing on ASP.NET MVC design patterns.&amp;#160; What I will be focusing on is enterprise-ready patterns and practices that scales across cloud computing, coding with type safety, and abstractions of WebForms-like server controls – using the MVC pattern.&amp;#160;&amp;#160; &lt;a href="http://blog.codeville.net/" target="_blank"&gt;Steve Sanders&lt;/a&gt; and &lt;a href="http://haacked.com/" target="_blank"&gt;Phil Haack&lt;/a&gt; have about a year head start on everything-mvc, so I highly recommend reading up on their blogs for the basics and advanced solutions.&amp;#160; &lt;/p&gt;  &lt;p&gt;I will keep updating this post with each post for future references.&amp;#160; So, bookmark it or &lt;a href="http://eduncan911.com/archive/tags/asp.net+mvc/default.aspx" target="_blank"&gt;you can subscribe to the series here&lt;/a&gt;.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://eduncan911.com/blog/type-safety-with-asp-net-mvc-futures.aspx"&gt;Type Safety with ASP.NET MVC Futures&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx"&gt;Html.RenderAction&amp;lt;TController&amp;gt; for ASP.NET MVC 1.0&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You can also follow me on Twitter via eduncan911, if you can weed out my random thoughts from these postings.&lt;/p&gt;&lt;img src="http://eduncan911.com/aggbug.aspx?PostID=6787" width="1" height="1"&gt;</description><category domain="http://eduncan911.com/archive/tags/Geek+Stuff/default.aspx">Geek Stuff</category><category domain="http://eduncan911.com/archive/tags/Computer+Programming/default.aspx">Computer Programming</category><category domain="http://eduncan911.com/archive/tags/Asp.Net+Mvc/default.aspx">Asp.Net Mvc</category><category domain="http://eduncan911.com/archive/tags/Type+Safety/default.aspx">Type Safety</category></item></channel></rss>