I am writing some code using the new Entity Framework 4.1 code first model (where you simple objects to represent your database). I am using the System.ComponentModel.DataAnnotations to set rules such as required fields, validation with regular expressions, range validation, etc.

However, I could not find how you would implement the custom, “On[Property]Changing” patterns that is common in prior editions (using partial classes and partial methods). After a lot of looking, I stumbled upon the answer.

You use the new CustomValidation attribute. You can mark your property with this attribute as follows:

[CustomValidation(typeof(RegValidation), "ValidateAcceptTerms")]

public bool AcceptTerms { get; set; }

 

In this case, you are indicating that the validation code sits in the class, RegValidation inside the method, ValidateAcceptTerms.

 

You can then code this validation class as follows:

public class RegValidation

{

  public static ValidationResult ValidateAcceptTerms(bool isAccept)

  {

    if (isAccept)

    {

      return ValidationResult.Success;

    }

    else

    {

      return new ValidationResult(

          "You must accept the terms of service in order to continue.");

    }

  }

}

 

You return an instance of ValidationResult (either success or with an error message) to indicate the results. In this way, you can implement custom validation on your EF 4.1 code first properties.

Note: since your objects are just simple objects, you can also write the logic in your setter and raise a validation exception. This will prevent the object from saving. However, it will not automatically get your validation error into the view. The CustomValidation attribute will.


I had the pleasure of presenting to the Pittsburgh community today at Code Camp 2010.1. My talk was on extending Visual Studio for the community, packaging your extension (using VSIX), deploying that extension to the Visual Studio Code Gallery, and discovering that community content from within Visual Studio.

The talk went well—very interactive. Thanks to all those that participated! We did all of the following:

  1. Installed the Visual Studio 2010 SDK (http://msdn.microsoft.com/vstudio, bottom left)
  2. Wrote code: we wrote an MEF extension to the code editor for counting lines of code (including comments and whitespace). See below for a link to this sample code.
  3. Packaged the code: created a VSIX project to include the extension. See below for a link to this project too.
  4. Deployed to the Code Gallery: Created a page and deployed the VSIX project to the Visual Studio 2010 Code Gallery to make it discoverable to Visual Studio. See below for a link.
  5. Discovered the extension: used the Visual Studio Extension Manager (Tools | Extension Manager) to find and install the extension from the code gallery. See Image below:

 

Visual Studio Extension Manager

Key Downloads / Links for this Example:

 


More Cloud Container Pictures

Posted on November 30, 2009 12:33 by mikesnell

I posted earlier on the cloud container at PDC09. I wanted to add a few more pictures that I took of this awesome hardware.

The Front

Notice the slotted panels: this is where air comes in through the water-drip cooling filter.

cloudcontainerfront

The Back

Air is pushed out the top of the container. Not sure if these glass panels are just for the show piece or if they exist in all the containers.

cloudContainerBack

 

Inside

In this version, you can actually walk inside the unit in order to do maintenance. You can see the filter panels (left). You can also see the environment monitoring units hanging from the racks.

cloudcontainerinside


PDC09 Day 1 Thoughts

Posted on November 17, 2009 17:07 by mikesnell

The first, full day of PDC is almost over. I've been able to sit through the keynotes, some Azure breakouts, 1:1's, side meetings, and lunch. I thought I would blog a few points about what I've seen and heard on day one:

Codename “Dallas”

Microsoft site / product, codename "Dallas", is a new marketplace that enables the ability to share, discover, and consume data. This means people with large data sets can post their data here and developers can enable new applications that leverage this data. I think this is just brilliant. Enable a way for data providers to monetize the usage of their data while enabling developers to create applications that leverage the same.

http://pinpoint.microsoft.com/en-US/Dallas

PinPoint

Another marketplace, Microsoft PinPoint was announced. It is a site for sharing applications, bits of applications, and services. The key is a central location for sharing, discovering, and consuming applications and services. It seems Microsoft will be pushing this new marketplace heavily on many of their other sites when companies, consumers, and developers are looking for solutions. I really like where this is going in that it enable new opportunities for developers.

http://pinpoint.microsoft.com/en-US/default.aspx

Azure

Microsoft has really come a long with Azure since last year’s PDC. So far, in fact, that the production launch data has been announced (Jan 1, 2010) and a pricing model was presented.

Surprise! -- Azure now supports hosting application outside of .NET. This includes MySql, PHP, and more. In fact, anything you can run on computer hardware looks like it will be supported on this new cloud platform. This seems to put Microsoft at the center of the hosting world.

Microsoft announced the ability to mount a SQL blob as an NTFS drive and program against it. This allows you to write the same file I/O code you would if you were coding against a hard drive on a box or network.

The new service bus for Azure allows for securely connected applications over the internet, across networks, across organizations. Hope to see more on this.

Got an walkthrough of an data center container – WOW! Hope to post a picture soon.

http://www.microsoft.com/windowsazure/

SQL Azure

The SQL Azure stuff has just been awesome. What a great job the team seems to have done with this stuff. It really is a relational database as a service. For example, you can create a SQL Azure database and just count on the fact that replication just happens, failover just happens; it is not a hosted database but a service with an endpoint. Amazing.

I saw a good demo where an Azure team member had an existing application using SQL Server. They grabbed a connection string from a SQL Azure database service. The same code then connected and ran against SQL Azure. It really brought home the promise of using existing tools and code inside this new cloud!

http://www.microsoft.com/windowsazure/sqlazure/

Silverlight

We heard Ray Ozzie stress that Microsoft is focused on making Silverlight the single client solution on Windows, Mobile, and TV (Xbox, etc.). It seems that Silverlight has a ton of momentum … can’t wait to see Scott Gu’s stuff tomorrow on the Silverlight platform.


Creating Custom Controls in ASP.NET

Posted on July 24, 2009 10:05 by mikesnell

I have a second sample chapter from my book, “Microsoft .NET Framework 3.5—ASP.NET Application Development”. This sample is the full version of chapter 10, “Creating Custom Web Controls”; you can download it by clicking the link at the end of this post.

I wrote this chapter based on what I believe developers need to succeed when writing most Web applications inside of ASP.NET. For this reason, I first focused on creating basic user controls. This is a principal skill required by ASP.NET developers to enable fast, straightforward code-reuse and consistency across their Web application.

This first lesson of the chapter is all about user controls. This includes managing events, working with properties, consuming controls, and even dynamically loading user controls at runtime. I also covered templated user controls for those times when you need a custom layout for your user control’s UI representation.

The second lesson is the more advanced topic of custom Web server controls. These controls provide re-use across applications. They can be compiled separately, installed, versioned, and shared . You can use the techniques in this lesson to create UI controls that are similar to those you buy or use inside of Visual Studio. This includes features like Toolbox support, custom designers, default properties, and more.

I hope you enjoy this sample chapter. Like all chapters in the book, it includes detailed labs and questions designed to help you study for the certification exam. You can get access to the lab source code and answers to the sample questions by purchasing the book.

9780735625624_CH10.pdf (804.47 kb)