|
|
These blog entries are written by industry experts and leaders. We consider this content to be a good read for any software developer or web technologist.
July 2008 - Posts
-
The final two tutorials (#9 and #10) of my Master Pages Tutorial series are now available on www.asp.net. These tutorials explore two advanced master page scenarios:
- Specifying the Master Page Programmatically [VB | C#] - in most cases a content page's master page is specified statically through the MasterPageFile attribute in the @Page directive. However, it is possible to dynamically set a content page's master page, as this tutorial shows.
- Nested Master Pages [VB | C#] - much like how a content pages can bind to a master page, it is possible for master pages to bind to other master pages. Such nested master pages are quite useful in scenarios where there is an overarching look and feel to a site, but various portions of the site require a more customized and unique template. With nested master pages you can define the global common layout in a top-level master page and then use child master pages to create more specialized layouts for particular sections of the site.
Like my past tutorials, these tutorials are all available in C# and VB versions, include a complete working source code download, and are available to download as PDF, as well.
Enjoy! - http://asp.net/learn/master-pages/
|
-
I've been using Microsoft Virtual PC product for many years, posting separate VPCs for each client's work and for test beds for installing beta software or trying out products for my Toolbox column. A couple of months ago I started noticing that when in a VPC the Tab and Esc keys were being ignored. For example, when I would use Alt+Tab from within a VPC I would be toggling through the applications in the host environment. Likewise, if I hit Ctrl+Esc from within a VPC it would display the Start menu options in the host environment. Because I try to use the mouse as infrequently as possible and stick to keyboard shortcuts, having a nonfunctional Tab key seriously hindered by productivity. From Alt+Tab to moving from one form field to the next to inventing code, it's amazing how frequently I use the Tab key.
My initial assumption was that there was some problem with Virtual PC. I found information noting such problems when using UK keyboards, but nothing specific to my problem. My research revealed that a general rule of thumb was to check for programs in the host environment that might be capturing keystrokes and thereby "stealing" the keystrokes from the VPC. My approach was rather ham-fisted, but what I did was the following: I launched task manager and started killing off processes one at a time. After each process was killed, I would go over to the VPC and see if it now recognized the Tab and Esc keys.
The culprit ended up being FogCreekCopilotHelper.exe. Copilot is a desktop sharing tool from Fog Creek Software that I use now and then when providing telephone-based consulting services. This process is supposed to shut down automatically when the Copilot software closes. However, sometimes it does not cleanly die off; instead, it lingers around and seems to swallow up the Tab and Esc keys in Virtual PC.
I first detected that FogCreekCopilotHelper.exe was the source of my problems several weeks ago, but I held off posting a blog entry because I wanted to observe how exactly this problem arose. I've had several sessions with Copilot where the FogCreekCopilotHelper.exe process shutdown cleanly and did not interfere with my Virtual PC experience. In the times when it has caused trouble I noticed that mid-way during the Copilot session the Tab and Esc keys stopped working in Copilot! The same behavior I noted in Virtual PC after the fact. And, sure enough, when Copilot loses the Tab and Esc keys the FogCreekCopilotHelper.exe process does not cleanly die, but rather sticks around and steals the Tab and Esc keys from Virtual PC.
Long story short, if you lose functionality for certain keys in Virtual PC first look for the FogCreekCopilotHelper.exe process and kill it if it's floating around. If you do not use Copilot (or if this process is not present) then start to systematically kill off processes until you find the troublemaker. This approach of methodically killing processes is not very elegant, but can help detect the problem assuming you do not inadvertently kill essential processes during the genocide.
|
-
In my last blog entry, The CreateUserWizard and Validation ErrorMessages, reader Andrei Rinea commented:
I wish the whole Membership set of controls would be replaced with better ones. Very often I need to have an authentication/authorization mechanism that doesn't need a username but an e-mail. I need email verification and in case the user loses the password he/she should be able to recover it without knowing (a) username.
This scenario is possible, but requires some customizations of the Membership controls. For instance, you can configure Membership so that an Email is not required, and then customize the CreateUserWizard control so that it uses the text “E-mail:” in place of “Username:” (thereby storing the user's e-mail address as their username.
The CreateUserWizard control can also be customized to require that user's verify their email address before being able to log in. See these two articles for more information and to download a complete, working example:
Personally, I quite like the existing Membership controls. They provide a suitable out-of-the-box implementation that allows one to get started without having to write a lick of code, yet are highly customizable through properties, templates, and events, allowing for virtually unlimited scearios and workflows. And if a particular control doesn't cut the mustard you can always create your own UI that uses the Membership framework behind the scenes (via the Membership class).
|
-
The CreateUserWizard control introduced in ASP.NET 2.0 makes it easy to create a new user account on a website that uses the Membership framework. (See my Creating User Accounts tutorial for more information on using this control.) Out of the box, the CreateUserWizard control offers a fully functional interface for creating new user accounts. You don't have to write a lick of code or declarative markup. Instead, just set a few properties and you're off and running.
Teach Yourself ASP.NET 3.5 in 24 Hours reader Jason L. e-mailed me recently with an interesting observation about the behavior of the CreateUserWizard's validation functionality. The CreateUserWizard control's default user interface includes validation controls for the various input fields. For example, there are RequiredFieldValidator controls to ensure that the visitor enters values for the username, password, e-mail, and so on. The ErrorMessage property values of these validation controls can be customized via the CreateUserWizard control's properties, such as UserNameRequiredErrorMessage and EmailRequiredErrorMessage. Regardless of the values you use for the ErrorMessage properties, the RequiredFieldValidator controls have their Text properties values all set to the same thing, an asterisk (*). When a validation control is both its Text and ErrorMessage properties assigned, the value of the Text property is displayed in the location of the validation control in the event of invalid data; the value of the ErrorMessage property is displayed in a ValidationSummary control (if one exists on the page).
For whatever reason, the default CreateUserWizard control user interface does not include a ValidationSummary control. Consequently, the values specified in the UserNameRequiredErrorMessage and EmailRequiredErrorMessage properties (among the other ErrorMessage-related properties) are never displayed. (Granted, these property values are assigned to the tool tip of the validation controls, meaning that a user can hover her mouse over the asterisk and see a more detailed ErrorMessage description.) Interestingly, the CreateUserWizard's default interface does show a message if the password and confirmation password do not match. This behavior is due to the fact that the default interface includes a CompareValidator control that does not have its Text property set. Rather, only its ErrorMessage property is assigned (and this can be customized via the CreateUserWizard control's ConfirmPasswordCompareErrorMessage property).
So how do you get the ErrorMessage properties for the RequiredFieldValidator controls to display when using the default interface? One way is to add your own ValidationSummary control to the page. If you do this piece sure to set a ValidationSummary control's ValidationGroup property to the ID of the CreateUserWizard control. In other words, if the CreateUserWizard is named MyCreateUserWizard then set the ValidationSummary control's ValidationGroup property to "MyCreateUserWizard". However, if the user enters a password that does not match with the confirm password the corresponding error message will be displayed twice: once by the CompareValidator and again in the ValidationSummary control.
The only sure fire way to get the validation messages to display as expected is to customize the user interface created by the CreateUserWizard. The good news is that it is very easy to convert the CreateUserWizard into a template, at which point you have virtually unlimited customization capabilities regarding the validation logic and display. For more information on customizing the CreateUserWizard control's user interface, check out Customizing the CreateUserWizard Control and Storing Additional User Information.
|
-
Earlier this week I bought Dragon NaturallySpeaking version 9.5. It took me close to an hour to get the software install and configured properly on my system, primarily because I am using a 64-bit operating system. Dragon NaturallySpeaking does not support the 64-bit versions of Windows Vista or Windows Server 2008, but there is a workaround that allows you to install on a 64-bit operating system. Another challenge is that Dragon NaturallySpeaking has trouble with certain onboard sound cards. This blog entry highlights the challenges I faced when installing Dragon NaturallySpeaking provides workarounds.
If you try to install Dragon NaturallySpeaking a 64-bit operating system you will be stopped by the installer. After a bit of searching, I found this helpful post by VelvetPoodle:
The problem is the installer bundled with the software. This has code preventing it from being run on 64 bit vista. This can be edited using the free application orca.exe which is part of the much larger Microsoft core platform SDK. If you only want the application orca.exe do a google search for it. Make sure you get the 64 bit version though… Alternatively get it from here: http://blogs.msdn.com/astebner/archive/2004/07/12/180792.aspx.
1. Copy the entire contents of the DVD to a folder on your computer 2. Make a backup of the file Dragon Naturally Speaking 9.msi 3. Install Orca and open Dragon Naturally Speaking 9.msi from the installer directory 4. To the left under tables, scroll down and click on the “launch Conditions”. The error conditions will appear to the right. 5. Select the Condition “Not VersionNT64” 6. Right-Click and select “Cut Row(s)” 7. Save the edited "Dragon Naturally Speaking 9.msi" 8. Install as normal
While this method will certainly get Dragon NaturallySpeaking to install and work on 64-bit operating system, one thing to keep in mind is that NaturallySpeaking will not respond or respond very slowly when dictating in 64-bit applications. For example, if I try to dictate in Internet Explorer, Dragon NaturallySpeaking virtually hangs. Supposedly, the next version of Dragon NaturallySpeaking will support 64 bit operating systems.
After successfully installing Dragon NaturallySpeaking on my computer, the application started and I was greeted with a warning: "Audio Setup Wizard cannot find the sound system." This error threw me for a loop because my computer certainly does have a sound system - I can hear music coming from my speakers and am able to record voice from a microphone. Some online sleuthing turned up message board posts from other Dragon NaturallySpeaking users who encountered similar problems with onboard sound card systems not supported by Dragon NaturallySpeaking. My computer has an onboard RealTek sound card; other users with the same configuration had reported similar errors.
In the same post where VelvetPoodle shared how to install Dragon NaturallySpeaking 64-bit operating system, another poster named webbediva shared a workaround for the audio wizard problem that worked for me:
To get past the audio wizard error: With Naturally Speaking bar running, right-click on the audio.exe file using Windows Explorer in Program Files\Nuance\NaturallySpeaking9\Program. Under "Properties" click "Compatibility" tab and select "XP". Double-click to run.
To summarize, with Dragon NaturallySpeaking running need to set the compatibility level of the audio wizard program to be compatible with Windows XP and run this program from Windows Explorer. With this step out of the way, Dragon NaturallySpeaking (finally) worked! It took a bit of research and effort to get Dragon NaturallySpeaking properly installed and configured on my machine, but I haven't had any issues since (except for slow or nonexistent response times for 64 bit applications).
Read the post with the two workarounds: http://forums.cnet.com/5208-6035_102-0.html?forumID=44&threadID=280578&messageID=2744514
|
-
Earlier this week I broke my right hand playing basketball. I currently have a splint on, which renders my right hand useless. I can wiggle the tips of my fingers but I lack the control to use a mouse or type with this lame hand. I'm getting proficient typing with my left hand, but it is still significantly slower than typing with both. For instance, with both hands I can type in the neighborhood of 100-120 words per minute. With just my left, I type a shade under 40 words per minute, and even slower when keying in typical code syntax with its mix of punctuation and case changes. (Thank God for IntelliSense!)
On Thursday I bought Dragon NaturallySpeaking from my local Fry's. NaturallySpeaking a popular voice recognition software package. I opted for the Standard Edition, which set me back $100. Not having had experience with voice recognition software in the past, I was a bit skeptical at how accurate such software could be. I knew it would not suffice for coding, but my hope was that it would make a good substitute for writing e-mails and authoring articles. So far, I have been pleasantly surprised with the accuracy and ease of use. The marketing literature claims over 99% accuracy. I have no clue how accurate it truly is, but keep in mind that even with 99% accuracy that still means that there will be one error every 100 words you utter. And if you are as verbose as I am, 100 words comes very quickly. Every couple of sentences there are one or two corrections I have to go back and make with my keyboard or voice commands.
Speaking of using voice recognition software for coding, I did try writing a little bit of code with my voice. My only foray into this was with a simple "Hello, world" example. What I intended to write was:
Response.Write("Hello, world!");
I said: "response", "period", "write", "open parenthesis", "open quote", "cap hello", "comma", "world", "exclamation point", "close parenthesis", "semi-colon". What I got was:
Response. Right ("Hello, world!");
Close, but no cigar. One of the main problems is that the software injects spaces where it thinks they are needed when speaking English, not when being parsed by a programming language. Moreover, it has trouble with homonyms at times. Note how it chose to use "Right" instead of "write".
In addition to writing text with your voice you can also use Dragon naturally speaking to issue a computer command such as copy, paste, formatting, and other common commands. The copy and paste features are quite useful, but what can be frustrating is when you say a word to type that to also maps to a command, such as "print" or "close".
All in all, I'd recommend using Dragon NaturallySpeaking or some other voice recognition software for people with some sort of typing impairment who spend much of their day sitting in front of a computer responding to e-mails or writing articles or other documents. It takes some getting used to, and there certainly are errors that you have to put up with, but it is a lot more efficient than typing with one hand and allows you to keep that one good hand on the mouse most of the time.
One warning regarding Dragon NaturallySpeaking: the software does not natively support 64-bit operating systems (although there is a workaround), and certain onboard sound cards are not recognized by the software without a bit of a workaround. I'll discuss some of the challenges I had installing Dragon NaturallySpeaking, and the steps I took to surmount those challenges in: Installing Dragon Naturally Speaking (DNS) on Vista / Windows Server 2008 64-Bit.
|
-
-
If you subscribe to me through an RSS reader you should now see a small ad at the bottom of each feed item. I am testing out the new RSS advertising that I have added to The Lounge. I have tested them in Google Reader, NetNewsWire, and Feed Burner. If you see an issue in your feed reader please let me know, I want to make sure above all that I am not causing any problems with a specific reader that I have not tested with. Over the next week I will have a couple other sites roll out the ads to test it on a larger scale, my goal is to launch the .NET RSS Room August 1st.
RSS advertising has become more and more prevalent over the last couple of years with plenty of big players in the market like Google, FeedBurner, and Pheedo. My goal is to deliver more relevant ads as most of the ads you see on the big players are more general technology ads, with The Lounge RSS ads I will be able to focus in on .NET publishers and .NET advertisers.
I will also only support people adding my advertisements to full feeds, ads in partial feeds is just wrong. Right now you will see an ad in every feed item, but that will be changing before August 1st as I add a metering option so publishers can decide how often the ads should show up.
-James

|
-
I am thrilled to announce that Scott Hanselman will be joining The Lounge in August. It says a great deal about someone that they literally need no introduction, if you work with .NET or any Microsoft technology, you know who Scott is. Since taking over The Lounge getting Scott onto the network has been one of my goals and I finally think The Lounge is at the point where it can be a mutually beneficial partnership. We have agreed to a six month trial to make sure that the partnership works out for both us, but I am confident that it will.
I have also picked up a couple of other excellent bloggers this month, but I will save that for my normal end of the month update.
-James
|
-
i broke my right hand playing basketball tonight. splint on, prognosis is 4-6 weeks in a cast that will likely be high enough on my hand to greatly reduce finger mobility. as it stands now, i can only type w/my left hand and have moved the mouse to my left and switched the right and left buttons, but typing w/ one hand is excruciatingly slow and error-prone. i'm sure practice makes perfect but right now it is frustratingly slooooooooooow.
maybe best to ditch natural keyboard for a 'normal' one as the keys on the natural are so farther apart.
i wonder if there's a key mapping that's ideal for left handed only typing. like dvorak for lefties
any tips on how to get by on a computer with a busted hand?
thanks in advance
|
-
I’ve been asked a few times about how to optimize LINQ code. The first step in optimizing LINQ code is to take some measurements and make sure you really have a problem. It turns out that optimizing LINQ code isn’t that different from optimizing regular C# code. You need to form a hypothesis, make changes, and measure, measure, measure every step of the way. Measurement is important, because sometimes the changes you need to make are not intuitive. Here is a specific example using LINQ to Objects. Let’s say we have 100,000 of these in memory: public class CensusRecord { public string District{ get; set; } public long Males { get; set; } public long Females { get; set; } }
We need a query that will give us back a list of districts ordered by their male / female population ratio, and include the ratio in the query result. A first attempt might look like this: var query = from r in _censusRecords orderby (double)r.Males / (double)r.Females descending select new { District = r.District, Ratio = (double)r.Males / (double)r.Females };
query = query.ToList();
It’s tempting to look at the query and think - “If we only calculate the ratio once, we can make the query faster and more readable! A win-win!”. We do this by introducing a new range variable with the let clause: var query = from r in _censusRecords let ratio = (double)r.Males / (double)r.Females
orderby ratio descending select new { District = r.District, Ratio = ratio };
query = query.ToList();
If you measure the execution time of each query on 100,000 objects, however, you’ll find the second query is about 14% slower than the first query, despite the fact that we are only calculating the ratio once. Surprising! See why we need to take measurements?
Look At Time and Space
The key to this specific issue is understanding how the C# compiler introduces the range variable ratio into the query processing. We know that C# translates declarative queries into a series of method calls. Imagine the method calls forming a pipeline for pumping objects. The first query we wrote would translate into the following: var query = _censusRecords.OrderByDescending(r => (double)r.Males / (double)r.Females) .Select(r => new { District = r.District, Ratio = (double)r.Males / (double)r.Females });
The second query, the one with the let clause, is asking LINQ to pass an additional piece of state through the object pipeline. In other words, we need to pump both a CensusRecord object and a double value (the ratio) into the OrderByDescending and Select methods. There is no magic involved - the only way to get both pieces of data through the pipeline is to instantiate a new object that will carry both pieces of data. When C# is done translating the second query, the result looks like this: var query = _censusRecords.Select(r => new { Record = r, Ratio = (double)r.Males / (double)r.Females }) .OrderByDescending(r => r.Ratio) .Select(r => new { District = r.Record.District, Ratio = r.Ratio });

The above query requires two projections, which is 200,000 object instantiations. CLR Profiler says the let version of the query uses 60% more memory.
Now we have a better idea why performance decreased, and we can try a different optimization. We’ll write the query using method calls instead of a declarative syntax, and do a projection into the type we need first, and then order the objects. var query = _censusRecords.Select(r => new { District = r.District, Ratio = (double)r.Males / (double)r.Females }) .OrderByDescending(r => r.Ratio);
This query will perform about 6% faster than the first query in the post, but consistently (and mysteriously) uses 5% more memory. Ah, tradeoffs.
Moral Of The Story?
The moral of the story is not to rewrite all your LINQ queries to save a 5 milliseconds here and there. The first priority is always to build working, maintainable software. The moral of the story is that LINQ, like any technology, requires analysis and measurements to make optimization gains because the path to better performance isn’t always obvious. Also remember that a query “optimized” for LINQ to Objects might make things worse when the same query uses a different provider, like LINQ to SQL. 
|
-
I ordered a new sleeve for my MBP today and got the shipping notice:
Hi James,
Thanks very much for visiting us again. I just want to let you know that your order (#75387) will ship fresh from our San Francisco factory on the evening of 14-Jul-2008 via US Priority Mail. Your tracking number is 91014762139077056409 and you will eventually be able to track your order at www.usps.com.
The total charge to your credit card will be $46.00.
If there's anything else I can do for you, just let me know.
Best regards,
Gary
PS: By the way, this is what we're sending you:
1 -SleeveCase- -Horizontal Orientation- -Size 15-2-- Make:-Apple- Model:- MacBook Pro 15"- Battery: $39.00
Product Total: $39.00
Sales Tax: $0.00
Shipping: $7.00
Grand Total: $46.00
First off it is very personal which is a nice touch, the other thing I noticed is that it says "Thanks very much for visiting us again" even though you don't login to their site and I haven't ordered from them in at least 2-3 years. That means it at least checks to see if my email has been used before (since its a different physical address, credit card, etc).
I need to keep emails like this in mind when building sites, keep it personal instead of trying to be bigger than you are. 37signals has a good section on this in Getting Real called Be Yourself.
-James
|
-
The ASP.NET MVC team is in the final stages of finishing up a new "Preview 4" release that they hope to ship later this week. The Preview 3 release focused on finishing up a lot of the underlying core APIs and extensibility points in ASP.NET MVC. Starting with Preview 4 this week you'll start to see more and more higher level features begin to appear that build on top of the core foundation and add nice productivity. There are a bunch of new features and capabilities in this new build - so much in fact that I decided I needed two posts to cover them all. This first post will cover the new Caching, Error Handling and Security features in Preview 4, as well as some testing improvements it brings. My next post will cover the new AJAX features being added with this release as well. Understanding Filter Interceptors Action Filter Attributes are a useful extensibility capability in ASP.NET MVC that was first added with the "Preview 2" release. These enable you to inject code interceptors into the request of a MVC controller that can execute before and after a Controller or its Action methods execute. This enables some nice encapsulation scenarios where you can easily package-up and re-use functionality in a clean declarative way. Below is an example of a super simple "ScottGuLog" filter that I could use to log details about exceptions raised during the execution of a request. Implementing a custom filter class is easy - just subclass the "ActionFilterAttribute" type and override the appropriate methods to run code before or after an Action method on the Controller is invoked, and/or before or after an ActionResult is processed into a response. Using a filter within a ASP.NET MVC Controller is easy - just declare it as an attribute on an Action method, or alternatively on the Controller class itself (in which case it will apply to all Action methods within the Controller): Above you can see an example of two filters being applied. I've indicated that I want my "ScottGuLog" to be applied to the "About" action method, and that I want the "HandleError" filter to be applied to all Action methods on the HomeController. Previous preview releases of ASP.NET MVC enabled this filter extensibility, but didn't ship with pre-built filters. ASP.NET Preview 4 now includes several useful filters for handling output caching, error handling and security scenarios. OutputCache Filter The [OutputCache] filter provides an easy way to integrate ASP.NET MVC with the output caching features of ASP.NET (with ASP.NET MVC Preview 3 you had to write code to achieve this). To try this out, modify the "Message" value set within the "Index" action method of the HomeController (created by the VS ASP.NET MVC project template) to display the current time: When you run your application you'll see that a timestamp updates each time you refresh the page: We can enable output caching for this URL by adding the [OutputCache] attribute to the our Action method. We'll configure it to cache the response for a 10 second duration using the declaration below: Now when you hit refresh on the page you'll see that the timestamp only updates every 10 seconds. This is because the action method is only being called once every 10 seconds - all requests between those time intervals are served out of the ASP.NET output cache (meaning no code needs to run - which makes it super fast). In addition to supporting time duration, the OutputCache attribute also supports the standard ASP.NET output cache vary options (vary by params, headers, content encoding, and custom logic). For example, the sample below would save different cached versions of the page depending on the value of an optional "PageIndex" QueryString parameter, and automatically render the correct version depending on the incoming URL's querystring value: You can also integrate with the ASP.NET Database Cache Invalidation feature - which allows you to automatically invalidate the cache when a database the URL depends on is modified (tip: the best way to-do this is to setup a CacheProfile section in your web.config and then point to it in the OutputCache attribute). HandleError Filter The [HandleError] filter provides a way to declaratively indicate on a Controller or Action method that a friendly error response should be displayed if an error occurs during the processing of a ASP.NET MVC request. To try this out, add a new "TestController" to a project and implement an action method that raise an exception like below: By default when you point your browser at this URL, it will display a default ASP.NET error page to remote users (unless you've gone in and configured a <customErrors> section in your web.config file): We can change the HTML error displayed to be a more friendly end-user message by adding a [HandleError] attribute to either our Controller or to an Action method on our Controller: The HandleError filter will catch all exceptions (including errors raised when processing View templates), and display a custom Error view response when they occur. By default it attempts to resolve a View template in your project called "Error" to generate the response. You can place the "Error" view either in the same directory as your other Controller specific views (for example: \Views\Test for the TestController above), or within the \Views\Shared folder (it will look first for a controller specific error view, and then if it doesn't find one it will look in the shared folder - which contains views that are shared across all controllers). Visual Studio now automatically adds a default "Error" view template for you inside the \Views\Shared folder when you create new ASP.NET MVC Projects starting with Preview 4: When we add a [HandleError] attribute to our TestController, this will by default show remote users an html error page like below (note that it picks up the master page template from the project so that the error message is integrated into the site). You can obviously go in and customize the Error view template to display whatever HTML and/or friendlier customer error message you want - below is simply what you get out of the box: To help developers, the default Error view template provided by the new project template in Visual Studio is written to display additional error stack trace information when you are browsing the application locally: You can turn this off either by deleting the code from the Error view template, or by setting <customErrors> to "off" inside your web.config file. By default the [HandleError] filter will catch and handle all exceptions that get raised during the request. You can alternatively specify specific exception types you are interested in catching, and specify custom error views for them by specifying the "ExceptionType" and "View" properties on [HandleError] attributes: In the code above I'm choosing to display custom error views for SqlExceptions and NullReferenceExceptions. All other exceptions will then use the default "Error" view template. Authorize Filter The [Authorize] filter provides a way to declaratively control security access on a Controller or Action method. It allows you to indicate that a user must be logged in, and optionally require that they are a specific user or in a specific security role in order to gain access. The filter works with all types of authentication (including Windows as well as Forms based authentication), and provides support for automatically redirecting anonymous users to a login form as needed. To try this out, add an [Authorize] filter to the "About" action in the HomeController created by default with Visual Studio: Declaring an [Authorize] attribute like above indicates that a user must be logged into the site in order for them to request the "About" action. When non-logged-in users attempt to hit the /Home/About URL, they will be blocked from gaining access. If the web application is configured to use Windows based authentication, ASP.NET will automatically authenticate the user using their Windows login identity, and if successful allow them to proceed. If the web application is configured to use Forms based authentication, the [Authorize] attribute will automatically redirect the user to a login page in order to authenticate (after which they'll have access): The [Authorize] attribute optionally allows you to grant access only to specific users and/or roles. For example, if I wanted to limit access to the "About" action to just myself and Bill Gates I could write: Typically for all but trivial applications you don't want to hard-code user names within your code. Instead you usually want to use a higher-level concept like "roles" to define permissions, and then map users into roles separately (for example: using active directory or a database to store the mappings). The [Authorize] attribute makes it easy to control access to Controllers and Actions using a "Roles" property: The [Authorize] attribute does not have a dependency on any specific user identity or role management mechanism. Instead it works against the ASP.NET "User" object - which is extensible and allows any identity system to be used. AccountController Class I mentioned above that the [Authorize] attribute can be used with any authentication or user identity management system. You can write or use any custom login UI and/or username/password management system you want with it. To help you get started, though, the ASP.NET MVC Project Template in Visual Studio now includes a pre-built "AccountController" and associated login views that implement a forms-authentication membership system with support for logging in, logging out, registering new users, and changing passwords. All of the views templates and UI can be easily customized independent of the AccountController class or implementation: The Site.master template also now includes UI at the top-right that provides login/logout functionality. When using forms-based authentication it will prompt you to login if you are not currently authenticated: And it displays a welcome message along with a logout link if you are authenticated on the site: Clicking the Login link above takes users to a Login screen like below that they can use to authenticate: New users can click the register link to create new accounts: Error handing and error display is also built-in: The AccountController class that is added to new projects uses the built-in ASP.NET Membership API to store and manage user credentials (the Membership system uses a provider API allowing any back-end storage to be plugged-in, and ASP.NET includes built-in providers for Active Directory and SQL Server). If you don't want to use the built-in Membership system you can keep the same AccountController action method signatures, View templates, and Forms Authentication ticket logic, and just replace the user account logic within the AccountController class. For the next ASP.NET MVC preview release we are planning to encapsulate the interaction logic between the AccountController and the user identity system behind an interface - which will make it easier to plug-in your own user storage system (without having to implement a full membership provider) as well as to easily unit test both it and the AccountController. Our hope is that this provides a nice way for people to quickly get started, and enable them to have a working end to end security system as soon as they create a new project. Testing TempData One last improvement to touch on in this first preview 4 post is some improvements being made on the Controller class that allow you to more easily unit test the TempData collection. The TempData property allows you to store data that you want to persist for a future request from a user. It has the semantic of only lasting one future request (after which it is removed). It is typically used for MVC scenarios where you want to perform a client-side redirect to change the URL in the browser, and want a simple way to store scratch data. With previous ASP.NET MVC Previews you had to mock objects in order to test the TempData collection. With Preview 4 you no longer need to mock or setup anything. You can now add and verify objects within the Controller's TempData collection directly within your unit tests (for example: populate a controller's TempData property before calling its action method, or verify that the action updated the TempData after the action returned). The actual storage semantics of the TempData collection is now encapsulated within a separate TempDataProvider property. Conclusion Hopefully the above post provides a quick look at a number of the new features and changes coming with ASP.NET MVC Preview 4. My next post on ASP.NET MVC Preview 4 will cover the new AJAX functionality that has been added, and demonstrate how to take advantage of it. Hope this helps, Scott 
|
-
I recently had some time on airplanes to read through Bitter EJB, POJOs in Action, and Better, Faster, Lighter Java. All three books were good, but the last one was my favorite, and was recommended to me by Ian Cooper. No, I’m not planning on trading in assemblies for jar files just yet. I read the books to get some insight and perspectives into specific trends in the Java ecosystem. 
It’s impossible to summarize the books in one paragraph, but I’ll try anyway:
Some Java developers shun the EJB framework so they can focus on objects. Simple objects. Testable objects. Malleable objects. Plain old Java objects that solve business problems without being encumbered by infrastructure and technology concerns.
That’s the gist of the three books in 35 words. The books also talk about patterns, anti-patterns, domain driven design, lightweight frameworks, processes, and generally how to write software. You’d be surprised how much content is applicable to .NET. In fact, when reading through the books I began to think of .NET and Java as two parallel universes whose deviations could be explained by the accidental killing of one butterfly during a time traveling safari.
The focus of this post is one particular deviation that really stood out.
From Objects To ORMs
The Java developers who focus on objects eventually have to deal with other concerns like persistence. Their object focus naturally leads some of them to try object-relational mapping frameworks. ORMs like Hibernate not only provide these developers with productivity gains, but do so in a relatively transparent and non-intrusive manner. The two work well together right from the start as the developers understand the ORMs, and the ORMs seem to understand the developers.
From DataSets to ORMs
.NET includes includes DataSets, DataTables, and DataViews. There is an IDE with a Data menu, and a GUI toolbox with Data tab full of Data controls and DataSources. It’s easy to stereotype mainstream .NET development as data-centric. When you introduce an ORM to a .NET developer who has never seen one, the typical questions are along the lines of:
How do I manage my identity values after an INSERT?
... and ...
Does this thing work with stored procedures?
Perfectly reasonable questions given the data-centric atmosphere of .NET, but you can almost feel the tension in these questions. And that is the deviation that stood out to me. On the airplane, I read about Java developers who focused on objects and went in search of ORMs. In .NET land, I’m seeing the ORMs going in search of the developer who is focused on data. The ORMs in particular are LINQ to SQL (currently shipping in Visual Studio) and the Entity Framework (shipping in SP1). Anyone expecting something like “ADO.NET 3.5” is in for a surprise. Persistent entities and DataSets are two different creatures, and require two different mind sets.
Will .NET Developers Focus On Objects Now?
It’s possible, but the tools make it difficult. The Entity Framework, for instance, presents developers with cognitive dissonance at several points. The documentation will tell you the goal of EF is to create a rich, conceptual object model, but the press releases proclaim that the Entity Framework simplifies data-centric development. There will not be any plain old CLR objects (POCOs) in EF, and the object-focused implicit lazy-loading that comes standard in most ORMs isn’t available (you can read any property on this entity, um, except that one – you’ll have to load it first).
LINQ to SQL is different. LINQ to SQL is objects all the way down. You can use plain old CLR objects with LINQ to SQL if you dig beyond the surface. However, the surface is a shiny designer that looks just like the typed DataSet designer. LINQ to SQL also needs some additional mapping flexibility to truly separate the object model from the underlying database schema – hopefully we’ll see this in the next version.
What To Do?
If you are a .NET developer who is starting to use an ORM –any ORM, you owe it to yourself and your project to reset your defaults and think differently about the new paradigm. Forget what you know about DataSets and learn about the unit of work pattern. Forget what you know about data readers and learn how an ORM identity map works. Think objects first, data second. If you can’t think of data second, an ORM might not be the technology for you. 
|
-
Tutorials #6, #7, and #8 in my Master Pages Tutorial series on www.asp.net have been published. These tutorials examine the interaction between the master page and its content pages:
- Interacting with the Master Page from the Content Page [VB | C#] - there may be times when the content page needs to programmatically interact with its master page. This tutorial looks at common scenarios where this type of interaction is needed and shows various techniques for accomplishing this type of interaction.
- Interacting with the Content Page from the Master Page [VB | C#] - the most typical type of master page and content page interaction originates from the content page. However, there may be times when the master page needs to signal the content page to take some action. This tutorial looks at such scenarios and shows a technique for this style of interaction.
- Master Pages and ASP.NET AJAX [VB | C#] - web pages that use Microsoft's ASP.NET AJAX framework must include the ScriptManager control on the page. This control can be placed in the master page. This tutorial looks at the effects of adding the ScriptManager to the master page. It also walks through a complete AJAX example and shows how to use the ScriptManagerProxy control to add custom script references in a content page.
Like my past tutorials, these tutorials are all available in C# and VB versions, include a complete working source code download, and are available to download as PDF, as well.
There are two remaining tutorials in this series that should be published by the end of this month. The two remaining tutorials look at specifying a content page's master page at runtime and using nested master pages.
Enjoy! - http://asp.net/learn/master-pages/
|
-
Matt Podwysocki invited me to speak at the D.C. alt.net meeting next Thursday evening (July 24th). The topic is LINQ. Matt specifically requested a code-heavy presentation, so expect two slides followed by plenty of hot lambda and Expression<T> action. Hopefully, Matt doesn’t blackout the neighborhood like he did at the nearby RockNUG meeting this week. The White House is two blocks away and the people inside get a little jumpy about blackouts. DateTime: 7/24/2008 - 7PM-9PM
Location: Cynergy Systems Inc. 1600 K St NW Suite 300 Washington, DC 20006 Show Map 
|
-
In my ten year software developer career I've only worked for two companies, and both job experienced pre-dated the 21st century and my graduation from college. At both jobs, I only had one monitor, albeit a rather large one for the time (21” at both companies, if I remember correctly). After graduating, I decided to strike out on my own as a freelance writer, consultant, and trainer, and haven't looked back since. Not knowing any better, I used a solitary monitor until April 2006, when I finally updated to a dual monitor setup. I bought a second video card, picked up the same brand/model of monitor, and graduated into the 21st century. (What prompted me to upgrade to two monitors was, primarily, stories of enhanced productivity from my wife, who was, at the time, working for a company that supplied dual monitors to all its devs. Bill Gates is also a big fan of multiple monitors.)
Two monitors are superior to one for many development- and writing-related tasks. Having Visual Studio in one window and the browser in another and being able to see both simultaneously nets huge performance gains. Likewise, when writing, having Microsoft Word up on one screen and code or a browser is a great time-saver. Today I have a hard time functioning when at a client's site and am reduced to using a single monitor setup. But just how much better are two monitors than one?
Anyone who tells you that they are twice as productive with two monitors than with one is probably over exaggerating. For development- and writing-related activities I find that two monitors allows me to accomplish somewhere on the order of 25%-50% more work in a given unit time. That's a wet finger in the air estimate, but there have been more formal studies. Microsoft Research reports that multiple monitors “can increase your productivity by 9 to 50 percent.” A small study performed by Darrell Norton shows productivity as lines of code per day increased by 10% and defect levels decreased by 26% (hat tip Jeff Atwood). What doesn't get measured in these studies is the increased quality of worklife. Even if the actual productivity gain is nominal, having a second monitor feels better. It lets you see more screen real estate at a given point in time. It lets you compare two documents side by side rather than incessantly Alt-Tabbing.
A couple of months ago I upgraded to a new computer. The only components I salvaged from my old computer was a 350 GB S/ATA hard drive and my two monitors; everything else was bought new. While investigating parts and formulating my shopping list I debated whether to upgrade to three monitors. Jeff Atwood is a big fan of three monitors:
As good as two monitors is, three monitors is even better. With three monitors, there's a "center" to focus on. And 50% more display area. While there's certainly a point of diminishing returns for additional monitors, I think three is the sweet spot. Even Edward Tufte, in the class I recently attended, explicitly mentioned multiple monitors. I don't care how large a single display can be; you can never have enough desktop space.
I didn't think a third monitor would make me any less efficient, but I questioned as to whether the improvements in efficiency (or quality of worklife) were as grandiose as Jeff made them out to be. In the end I decided to give it a whirl. I bought two GeForce 8600 GT video cards for the new system. These have dual DVI outputs meaning that I could upgrade to a fourth monitor if I were so inclined. I kept the two monitors from my previous setup - 17 inch monitors from Sharp - and bought a new Samsung SyncMaster 2053 BW monitor, which is a wide screen that has about the same visible height at the Sharps, but is about 33% wider. The two Sharps run at 1280x1024, the Samsung at 1680x1050.

I've been using the three monitor setup for a couple of months now and regret to say that I have not seen the same productivity benefits or improvement of worklife that Jeff espouses or that I enjoyed when going from one monitor to two. For certain tasks I am more productive with three monitors than two, a prime example being if I need to review a client's email while bug bashing. I can have the email open that explains the error in one window, Visual Studio in another, and the web application running in the third. However, for most other activities the third monitor does not add too much value. Consequently, it's not uncommon for one of the three to sit unused for long stretches of time.
Three monitors also have some detriments that weren't present with two. Because of the width of the three monitors, I frequenty tilt my neck to see the side monitors; with two monitors I could see both with when looking straight ahead by just turning my eyes, but not with three. The net result is that if I spend an inordinate amount of time focusing on one of the side monitors my neck becomes a bit stiff. To combat this I try to make myself swivel my chair when I start looking at a side monitor, but it's second nature for me to turn my head and I don't realize that I'm not swiveling until my neck starts to bother me.
In the end, three monitors are better than two, just like I imagine four would be better than three, but the returns on multiple monitors quickly diminish as the number of monitors grow (at least for my line of work and from my personal work habits). Knowing what I know now, if I had to choose between a third monitor and some other upgrade (perhaps a super high-end hard drive or a high-end office chair), I'd stick with two monitors and take my luck with what's behind Door Number 1.
|
-
This is the first in a series of posts about building The Lounge Advertising Network. My goal here isn't to try and tell people how to build applications, but rather just to explain how I have refactored/built this application.
First a little backstory. The Lounge was started by Kevin Fricovsky in 2007. In December of 2007 I took over The Lounge and began the process of refactoring it and building upon what Kevin had built. When Kevin turned The Lounge over to me it was built using ASP.NET 2.0 and used SubSonic as the ORM to talk to SQL Server 2005. I also want to make sure it is clear that I did not have any problems with Kevin's code, the Lounge worked perfectly for me from Day 1.
Because I was starting with a production application I had to prioritize technical improvements vs. new features. I wanted to start out with a refactoring and testing release. Release 1.1 only included two tickets:
I am a big fan of having rich models that contain as much business logic as possible. This makes the front-end pages very light and easy to modify, and it makes the DB simple and easy to work with. It also helps me practice TDD and get good test coverage as I can focus my testing time and effort on the Models. So the first step with The Lounge was to refactor as much logic as possible into the Models and to wrap those with tests.
SubSonic makes it fairly easy to do this style of development as long as you structure it correctly. What I did was in my Model project create a subfolder called SubSonic where the SubSonic models are generated. To generate them I use the External Tools trick in Visual Studio which makes it easy to quickly re-generate the subsonic models when I make a change. I don't want to put any logic in these models though since they get blown away when you regenerate. Thankfully SubSonic makes all of these models partial classes, so in the root of my Model project I have my own model objects with their logic. This is actually very Rails/ActiveRecord like since my models just have logic and don't have to have the data properties. Yes I know that my models do not have PI, I am OK with that.
Part of putting as much logic as possible in my model means moving any validation to the model and not keeping it in the code behind or UI. In the past I have used a custom attribute based system (that turned into EVIL) and I have also been enamored with ActiveRecords validations.. but neither was really an option since I didn't want to add anything to the SubSonic models. I ended up writing a simple little ValidationEngine that just gives me some standard ways of checking for conditions and throwing a standard error message, using my little engine I can override the SubSonic Validate() method with something like this:
public override bool Validate()
{
ValidationEngine valEngine = new ValidationEngine();
valEngine.CheckRequiredString(Title, "Website Title");
valEngine.CheckRequiredString(Url, "URL");
valEngine.CheckValidURL(Url, "URL");
validationErrors = valEngine.Errors;
return valEngine.IsValid;
}
Its pretty simple but it keeps all of my validation in the model, in one place, and it makes sure all my error messages are standardized across the models. I try to keep everything in my models, so even simple things like this go in a model so they stay out of the view:
public bool IsPayPal
{
get
{
return PaymentOption == "PayPal";
}
}
For the unit tests I use MbUnit, at one point I was close to 100% coverage but lately it has crept down a little bit. I feel another hardening release in my future. One part of my refactoring was tweaking how the ads were served, I made a couple changes that I hoped would have a positive impact on performance, but before I did that I wanted to get an idea of how long it took. To do that I wanted to test more than just one ad being served... so I decided to write a test like so:
[Test]
[Repeat(100)
public void GetNextPassTest()
{
Pass pass = Pass.GetNextPass("127.0.0.1", "DOTNET");
Assert.IsNotNull(pass);
Assert.IsTrue(pass.RoomCode == "DOTNET");
}
This uses the nice MbUnit Repeat function to get 100 Passes (a Pass is basically a campaign). I can run this test and see how long it takes, make my changes then run it again. It isn't as precise as using something like ANTS Profiler or dotTrace, but this was a way to quickly run my test and make sure I wasn't negatively impacting performance. I don't remember the exact numbers but I know that I cut the time in half that it took to get 100 passes. (MbUnit also has lets you assert on run times, but I just wanted a metric to keep track of and didn't have a strict goal I was shooting for)
At the end of the release I deployed and didn't really deliver a single new feature, but I got alot of housekeeping out of the way and was ready to move on to the most important feature to my advertisers at the moment.... Reporting.
-James
|
-
I have long used FireFox as my default browser for surfing the web, but I typically test my ASP.NET pages on Internet Explorer for a couple of reasons:
- Some of the larger software projects I work on are developing intranet applications, where all users' browsers are fixed (IE 6 for one client, IE 7 for another).
- For book and article and tutorial screen shots I typically use IE as my browser of choice because IE is still, after all, the most used browser.
- Until Visual Studio 2008, Visual Studio used IE as the browser when debugging an ASP.NET application (even if FF was setup as the default). Because all of my clients are still on ASP.NET 1.x or 2.0, I don't use VS 2008 too often.
However, I have recently been working on some AJAX material and have been using VS 2008 more frequently. Visual Studio 2008 kindly launches my default browser (Firefox) when I debug my web application. The problem was I noticed that Firefox was loading pages very slowly. I'd visit a site through localhost (via the ASP.NET Development Web Server) and pages would take several seconds to load.
To help analyze the problem I used the excellent Firefox extension Firebug (which I reviewed in the March 2008 issue of my Toolbox column). Firebug will show you the total number of network resources downloaded when visiting a page, along with the time it took to download each.

Each resource - from the 4KB HTML fiel to the 7KB JPG file, was taking close to a FULL second to retrieve! When requesting this same page from a remote, production web server, download times were 5 to 10 times faster per resource. What was going on here?
I next opened Internet Explorer - the same pages loaded instantaneously. I fired up Fiddler, an excellent HTTP Debugger for IE, and noted that the resources were being downloaded in 0.02 seconds, nearly 500 times faster than from Firefox. By now I was seriously stumped. I then did what I should have done from the getgo - I searched the web. Duh!
Turns out that there is a known issue with Firefox's IPv6 support and the ASP.NET Web Development Server that introduces these lengthy delays. To quote from the Mozilla Wiki:
IPv6 was designed in part to solve the problem IPv4 will soon be facing: the exhaustion of all possible IP addresses. Mozilla implemented IPv6 support in early 2000, but that support did not receive widespread testing until recently as IPv6-capable OSs and network software/equipment became more common. One particular bug that has appeared exists not in Mozilla, but in IPv6-capable DNS servers: an IPv4 address may be returned when an IPv6 address is requested. It is possible for Mozilla to recover from this misinformation, but a significant delay is introduced.
...
Two approaches were taken to work around these bugs: first, a preference to globally disable IPv6-based DNS lookups browser-wide (this preference [network.dns.disableIPv6]), and a blacklist of domains which should never have IPv6-based DNS lookups performed (network.dns.ipv4OnlyDomains).
Consequently, to fix this problem of slow response times when visiting localhost using Firefox, either set the network.dns.diableIPv6 configuration property to true or set the network.dns.ipv4OnlyDomains configuration property to “localhost”. My search led me to many pages around the Web suggested that you set the nework.dns.disableIPv6 property to true, but this would disable IPv6 support for all sites visited by the browser (at least according to the Wiki documentation). Therefore I set the network.dns.ipv4OnlyDomains property to “localhost”.
To set these properties, fire up Firefox and type about:config in the address bar. Next, locate the appropriate setting and change its value to the desired one. With that small change the debug-time experience in Firefox is greatly enhanced. And make no mistake, Firefox's rich library of extensions make it a great browser for web development. (I'll share some of my favorite Firefox web development extensions in a future blog post.)
Lesson for the day: If you bump up to a problem, search for a solution online first. If your search is fruitless then take the time to investigate it further, otherwise you may spend 15 minutes troubleshooting a problem that is already well-known and has a simple solution.
|
-
In the BI space I’ve seen a lot of SQL queries succumb to complexity. A data extraction query adds some joins, then some filters, then some nested SELET statements, and it becomes an unhealthy mess in short order. It’s unfortunate, but standard SQL just isn’t a language geared for refactoring towards simplification (although UDFs and CTEs in T-SQL have helped). I’ve really enjoyed writing LINQ queries this year, and I’ve found them easy to keep pretty. For example, suppose you need to parse some values out of the following XML: <ROOT> <data> <record> <field name="Country">Afghanistan</field> <field name="Year">1993</field> <field name="Value">16870000</field> <!-- ... --> </record> <!-- ... --> </data> </ROOT>
A first crack might look like the following: var entries = from r in doc.Descendants("record") select new { Country = r.Elements("field") .Where(f => f.Attribute("name") .Value == "Country") .First().Value, Year = r.Elements("field") .Where(f => f.Attribute("name").Value == "Year") .First().Value, Value = double.Parse (r.Elements("field") .Where(f => f.Attribute("name").Value == "Value") .First().Value) };
The above is just a mass of method calls and string literals. But, add in a quick helper or extension method… public static XElement Field(this XElement element, string name) { return | |
|