Matt Hinze will be leading this workshop. Matt is an expert in this topic and has previous presented at other conferences such as Houston Tech Fest and Austin Code Camp. Matt is a Microsoft MVP for C# and a frequent contributor to open source projects.
Relinquishing the responsibility of managing dependencies
Dependency Injection
A pattern
Constructor injection
Property injection
Some Benefits Enumerated
Testing: Makes it easy to isolate classes under test and to test interactions between classes by allowing the tester to provide a special implementation
Reuse: Enables code reuse and prevents duplication by enabling the sharing of functional parts
Simplicity: Since each component is only concerned with its responsibility, it can focus on that simple job
Construction velocity: Speed is quickened because of the above things. That’s what we’re here for, right?
Tuesday, December 15, 2009 2:00 PM to 4:00 PM
Microsoft hosts Headspring at: Microsoft Technology Center 9606 N Mo Pac Expy Austin, TX 78759 (map)
Short Answer: XP Mode (download it for Windows 7 here)
As a Microsoft MVP, I’m always getting emails asking me to try out {insert unstable pre-beta or beta build here}. A lot of the pre-release software (both public and private) can really hose your machine if there is something wrong. Those who know me know that I am not fond of installing machines from scratch. Because of that, I don’t install much pre-release software.
I have also done the VMWare thing with snapshots, and it works ok, but it is a pain to manage that (for me).
Windows 7 gives me a more streamlined option: Windows XP Mode. It gives me another machine embedded within Windows 7. I don’t have to install it. I didn’t have to set it up (besides running the setup wizard), and any programs I install in it appear in my regular Windows 7 start menu.
I opened it up today and installed Visual Studio 2010 Beta 2. I shut it down and then saw it appear in my normal Windows 7 start menu:
When I click on it, it doesn’t push me inside a VM. It opens this beta software in a regular windows on my Windows 7 desktop. You can tell it is running emulated in the XP VM by the blue title bar. This is exactly what Citrix has done for years, but now it is built in to the operating system for a single VM.
So go ahead: install all the pre-release software you want. When (not IF) you screw up the embedded OS, you can just uninstall and reinstall XP Mode, and you are back where you started. Seems pretty streamlined to me!
Relinquishing the responsibility of managing dependencies
Dependency Injection
A pattern
Constructor injection
Property injection
Some Benefits Enumerated
Testing: Makes it easy to isolate classes under test and to test interactions between classes by allowing the tester to provide a special implementation
Reuse: Enables code reuse and prevents duplication by enabling the sharing of functional parts
Simplicity: Since each component is only concerned with its responsibility, it can focus on that simple job
Construction velocity: Speed is quickened because of the above things. That’s what we’re here for, right?
Tuesday, December 15, 2009 2PM to 4PM
Microsoft hosts Headspring at: Microsoft Technology Center 9606 N Mo Pac Expy Austin, TX 78759 (map)
Headspring has developed and donated Portable Areas to the MvcContrib project. It currently resides in the MVC2 branch on the MvcContrib GitHub source control site. Eric Hexter has written a multi-piece tutorial on how to create and publish a Portable Area assembly.
A Portable Area builds on the concept of an ASP.NET control, and it takes it a step further. Whereas a control can encapsulate a certain part of the page to deliver rich functionality, a Portable Area can encapsulate an unlimited number of pages in a single assembly. The Portable Area in itself is an ASP.NET MVC 2 area that is packaged in a certain way into a single .Net assembly.
Component vendors have offerings of rich functionality packaged up into web controls. Now with MvcContrib layered on top of ASP.NET MVC 2, component vendors as well as you and I can package and distribute entire sections of functionality in a single binary. Think of comprehensive user administration and diagnostics. Think of executive dashboards and content management. All this rich front-end functionality can be packaged and distributed while at the same time allowing for the application to examine and process messages being sent from the Portable Area.
For example, a dashboard portable area might need 5 key metrics based on the type of dashboard used. It sends a QueryMessage to the application (via the Portable Areas bus) requesting these metrics. A query message handler that the application defines accepts the query message and provides the metrics for the portable area to consume and publish on the rich dashboard.
In this application, I want to demonstrate how Portable Areas, while being based on ASP.NET MVC 2, are a generic ASP.NET component model. This example, available via the MvcContrib CodePlex page, is a standard Web Forms application with the LoginPortableArea configured. This is just the first sample portable area that is included in MvcContrib’s MVC2 branch.
Enabling MVC routes and views in a Web Forms app.
In you web.config, you will need to add the following section in your <pages/> element:
Also, you need to add the UrlRoutingModule to the <httpModules/> element:
You will also need a Site.Master page in the page where the Portable Area expects it. This may be a future enhancement, but it must implement content placeholders for TitleContent and MainContent. Use it to adapt the sections of your existing master page so that views delivered by the portable area fit nicely within your own master page. Also notice the web.config file that must be in the Views folder (just copy it from an empty ASP.NET MVC project):
Registering the LoginPortableArea and handling messages sent by it:
In your Global.asax file, there are a few lines of code to register the portable area:
Notice that you are registering two classes of your own:
LoginHandler
ForgotPasswordHandler
We will explore LoginHandler, but download the code if you care to see the ForgotPasswordHandler. We register handlers with the bus so that we have classes that can be created and passed the messages. Here is what LoginHandler looks like:
Derive from MessageHandler<TMessage> or implement IMessageHandler fully. Putting the type of the message in the declaration will tell the bus that this handler knows how to handle that message type. It is possible to create a handler that can inspect ALL message types by implementing IMessageHandler fully.
There are three types of messages that a Portable Area might send:
IEventMessage : informational purposes
IQueryMessage : requesting some information
ICommandMessage : requestion the app to perform some action
All the message types are IEventMessage(s), and they all flow down the same bus. There is a handler factory abstraction you can set so that you are in control of the construction of event handler classes. The default that is built in is Activator.CreateInstance() – requires a no-arg constructor.
Once this is wired up, we can run our app and see the only page present, Default.aspx
This is our only Web Form in the application. It uses some built-in login controls to show who is logged in as well as links to login and log out. I have configured forms authentication in the web.config file. We will run the browser and step through the sequence of screens:
We see that we are not logged in. Let’s click the “Login” link . . .
Now we are in the Portable Area. This page is not defined in our app at all. Let’s type in a user name and password and click Submit. . .
Now it knows that I am logged in, and regular Web Forms gives me a handy “Logout” link.
Conclusion
This post has given us an example of how to consume an MvcContrib Portable Area from within a regular Web Forms app. Furthermore, it demonstrates how to run ASP.NET MVC pages right alongside Web Forms pages.
Call to action for component vendors
Please flood the marketplace with richer components that span multiple pages. The controls are nice and very useful, but there is lots of general functionality that can be bought instead of built by every development team. Portable Areas work in ASP.NET MVC as well as Web Forms, so while the development model is MVC, the consumption and customer market is the full breadth of ASP.NET developers.
“I want static behavior between assemblies/libraries/packages but dynamic abilities within.” – JeffreyPalermo.com
With C# 4.0, code within method can be dynamic because of the new dynamic keyword. This is a short post expressing what I want out of C#.
I recently listened to a great debate about the advantages and shortcomings of both dynamic and static languages, and the biggest pain of 100% dynamic languages is that when using a library, you really don’t know what is supports except if the documentation is liberal and flawless. There are no interface types to describe what properties and methods will be called once you pass the object in. If the documentation doesn’t cover every API, you are forced to write characterization tests.
The other big challenge is that when working with a dynamic language, you have to keep more of the system in your head in order to program. You have to remember which object is overriding which method, and what objects have new functionality attached to them.
Both of these points are only an issue between package boundaries. Within a package, everything is fine because you own all that code. All the conventions are yours. This dovetails with the main strength of a static language: All the APIs are self-describing. Every method declares types on the arguments. Objects explicitly implement interfaces.
The problem with static languages is that this rigor is extended within the package as well forcing the programmer to write every method body using the same compiler strictness of typing.
If we can have a dynamic keyword in C# so that code within a method body is dynamic, then some of this might already be achieved. We’ll have to see. Public interfaces that are at package boundaries need to be self-describing, from my point of view. After that, writing dynamic code within controlled boundaries can make things go smoother.
As a follow-up to some ASP.NET MVC comments by Dino Esposito, I want to provide my commentary on why the grassroots of the .Net developer community is so in love with ASP.NET MVC, even though Microsoft marketing isn’t really seeming to push it. It’s odd, too, because with Entity Framework, the message from Microsoft is clear: DataSets and Linq2Sql will still be supported. . . but . . . er. . .um, we recommend you start moving to Entity Framework.
Car vs. Motorcycle? Irrelevant analogy, I say
Because of the way the http://asp.net/mvc and other folks(Scott Hanselman) present a car vs. motorcycle analogy, the message is loud and clear: 9 of of 10 of you should be using Web Forms/car. The remaining 1 psycho with a death wish can ride the motorcycle/MVC. The problem is that reality is the following: Web Forms is the 1985 Lincoln TownCar (or even modern cars) where every single function is a physical electric toggle switch, plush comfortable seats and a sunroof. ASP.NET MVC is a 1950s Chevy. Every Chevy owner could understand how that car worked because of its simplicity. With the decked-out Lincoln, you needed to call in expert help if your electronic left mirror adjustment switch stopped working.
Whether Microsoft says it or not, ASP.NET MVC is a much superior IHttpHandler than Web Forms. I say it in this way because both use the ASP.NET runtime, which has proven very solid over the years. Because they are both linked to this core interface, they play nicely together. (In fact, stay tuned for an article about how to utilize MvcContrib’s Portable Areas feature by dropping in a DLL into a Web Forms app to instantly give that site a new section of urls and pages.)
Why is ASP.NET MVC superior? How can I say this when Web Forms has so many more controls, so many more features? I say this because it is a framework, not a product. I build products. I use frameworks to build products. I want my platform vendor to provide me with a simple and powerful framework so that I can build killer products for my clients. Crowding the framework with features actually hurts my ability to achieve my goal.
Web Forms will always have more features than ASP.NET MVC. ASP.NET MVC will never provide as many features as Web Forms. Chart control? Nope. How about the most basic grid? I certainly hope not. In fact, as one of the early and strongest community champions of ASP.NET MVC, I hope a grid never ships with it (MS should concentrate on the framework and let the community and component vendors ship grids). ASP.NET MVC has such grassroots excitement in the developer community because of the raw simplicity of it.
Simplicity will cause ASP.NET MVC to de facto replace Web Forms. I’ve said before that Web Forms will continue to power interesting web sites for decades to come. I am glad for that. It is a great framework, and I have delivered business critical applications with it. ASP.NET MVC is the way into the future for ASP.NET, though. After having a framework with tons of features and many strong control vendors who provide almost any functionality you can think of in a control, we (as in the average joe developer) long for a simpler way. ASP.NET MVC latches onto some of the strength of Rails and PHP. It is very easy to understand what is going on. Very simple. It doesn’t take an expert to decipher the flow of logic in the pages. This is simpler to the exercise equipment craze of the 1990s giving way to the rise in popularity of simple running and bicycling that we have seen in the U.S this decade.
Conclusion
Microsoft is not jumping up and down saying: “You should be using ASP.NET MVC for all your new web applications!” It’s just the folks who have given the framework a chance who are saying it.
Dino Esposito, from whom many of us have read and learned a great deal over the years, has recently expressed his love for ASP.NET MVC. Moreover, he has also notice that the message from Microsoft is very tentative about the framework. Because of the large market share of Web Forms, Microsoft seems hesitant to push ASP.NET MVC as THE NEW VERSION OF ASP.NET for fear that existing customers will worry about being left behind.
I have previously predicted that with Visual Studio 2010, the majority of new ASP.NET projects would use the MVC flavor over the Web Forms flavor. I stand by that because the IHttpHandler that is MVC is simpler to use and maintain than the System.Web.UI.Page (which is just an IHttpHandler).
That’s right, folks! For those of your coming to Microsoft’s Professional Developers’ Conference (PDC), Party with Palermo is going to kick the week off right. The conference starts on Tuesday, so Monday night, we are going to get down and party to the turntables of DJ Craig. If you were at the Party with Palermo at Tech Ed, you remember this DJ and how he had the uncanny ability to get folks like Ted Neward and Sara Ford dancing like it was 199. . . 2009!
Here is the skinny: Monday night – 7pm-10pm before PDC. Come to The Mayan, 6 blocks from the convention center in downtown L.A. Party with Palermo. RSVP now!
November 16, 2009 – Los Angeles, CA – 7:00PM – 10:00PM
The Mayan (map, website) 1038 South Hill St., Los Angeles, CA 90015 Ph: (213) 746-4287 – (six blocks from the convention center) Cover charge is 1 business card. This will get you in the door and register you for the grand prize drawings.