Runtime customizable model attributes in ASP.net MVC3

It can be because you want to implement I18N without the use of resources, or your model classes are outside of your MVC project, or any other reason you have that make you want not being tied to the inline (compile time) annotations on your model.
Whatever is the case, you can solve this by creating a custom class that help you to add these annotations dinamically. In this case, will be working on adding some validations defined on an XML file.

Create the CustomValidationProvider class

This CustomValidationProvider class, that will inherit from DataAnnotationsModelValidatorProvider, and it will help you adding all the validations that you need in the view accordingly.
public class CustomValidationProvider{
        private readonly string _validationsFile = "";

        public ConventionModelValidatorProvider(string validationsFile)
        {
            _validationsFile = validationsFile;
        }
}

When creating the instance of ConventionValidatorProvider, it needs to receive the name of the xml that will be used to load the validations. This instance is created in Global.asax file

ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(
 new CustomValidationProvider(ConfigurationManager.AppSettings.Get("validationsFile"))
);
It is important to notice that first we clear all the ModelValidatorProviders, in order to remove the one that comes with ASP.NET MVC by default.

Validations XML

As we said earlier, we will define the validations for our models in an xml file. This XML file has the following structure

  
    
      
        
          
 
        
      
 
    
  
 

In this structure we define the models and properties that will be validated. For each defined property one or more validations need to exist; these are the types that we will be using for this example

<validation type="Required" errorMessage="The product description is required" />

The field will be requierd and if not provided, the value on errorMessage attribute will be displayed

<validation type="StringLengthAttribute" min="5" max="10" errorMessage = "5-10 characters" />

The field will be treated as string and its length value needs to be greater or equal than value specified on min attribute and lower or equal to value specified in max atribute, otherwise the value on errorMessage attribute will be displayed

<validation type="RangeAttribute" min="5" max="10" errorMessage = "specify something between 5 and 10" />

The field will be treated as numeric and its value needs to be greater or equal than value specified on min attribute and lower or equal to value specified in max atribute, otherwise the value on errorMessage attribute will be displayed

<validation type="RegularExpressionAttribute" errorMessage = "Code should start with 0x and be followed by only digits or A-F letters">
<regex>
<![CDATA[
[0][x][0-9a-fA-F]+
]]>
</regex>
</validation>

The field will be tested against the specified regular expression; if it does not comply with the regex, the value on errorMessage attribute will be displayed

Adding the validations to the page

Every time a view is loaded, the GetValidators method from the ModelValidator class is called for each propety that we have in the form. By this, all the needed validations are added to the list that will be used by MVC to determine which fields have a specific constraint that needs to be satisfied before saving.
That said, we need then to create our own implementation of the GetValidators method by overriding whatever the base class have right now:
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
Before we go further, there are 4 important elements that we need to identify when this method is executed:

1. Action that it’s being executed

context.Controller.ControllerContext.RouteData.Values["action"].ToString();

2. Controller where this Action exists

context.Controller.ControllerContext.RouteData.Values["controller"].ToString();

3. Property that is being checked if will be validated or not

metadata.PropertyName

4. Model (class) where that property exists

metadata.ContainerType.Name

Once we know this elements, the rest is only read the XML file to determine if the the type and quantity of validations that the current property needs. In order to keep this post as clean as possible, I’ll ommit the code that is used to read the XML file. If you need information on how to do that, you can check this article or have a look on LinqToXML.

We need to filter the XML file that we previously defined to get the validations for the property of the model that is being checked. If at least on validation exists, we start looping on the list and we create the validations using a sort of simple factory. Each validation is created with the counterpart class defined on System.ComponentModel.DataAnnotations Namespace. In this example we’re only defining a few validation types. You can define more if needed.

switch (validationType)
{
 case "Required":
  attr = new RequiredAttribute();
  break;
 case "StringLengthAttribute":
  var attribute1 = validation.Attribute("max");
  var xAttribute2 = validation.Attribute("max");
  if (xAttribute2 != null)
  {
   int max = int.Parse(attribute1 != null && String.IsNullOrEmpty(attribute1.Value)
         ? "0"
         : xAttribute2.Value);
   var attribute2 = validation.Attribute("min");
   var xAttribute3 = validation.Attribute("min");
   if (xAttribute3 != null)
   {
    int min = int.Parse(attribute2 != null && String.IsNullOrEmpty(attribute2.Value)
          ? "0"
          : xAttribute3.Value);

    attr = new StringLengthAttribute(max);
    ((StringLengthAttribute)attr).MinimumLength = min;
   }
  }
  break;
 case "RegularExpressionAttribute":
  var regex = validation.Descendants().Single(a => a.Name == "regex").Value.Trim();
  attr = new RegularExpressionAttribute(regex);
  break;
 case "RangeAttribute":
  var attribute3 = validation.Attribute("max");
  var xAttribute4 = validation.Attribute("max");
  if (xAttribute4 != null)
  {
   double rangeMax = double.Parse(attribute3 != null && String.IsNullOrEmpty(attribute3.Value)
              ? "0"
              : xAttribute4.Value);
   var attribute2 = validation.Attribute("min");
   var xAttribute3 = validation.Attribute("min");
   if (xAttribute3 != null)
   {
    double rangeMin = double.Parse(attribute2 != null && String.IsNullOrEmpty(attribute2.Value)
               ? "0"
               : xAttribute3.Value);
    attr = new RangeAttribute(rangeMin, rangeMax);
   }
  }
  break;
 case "DataTypeAttribute":
  attr = new DataTypeAttribute(String.IsNullOrEmpty(attribute.Value)
           ? ""
           : attribute.Value);
  break;
}

The attribute instance that is created corresponds to the validation that will be performed on screen for that property. Each validation has its own instance, and we store each instance that we create in a list of List<Attribute> type. Once we’re done with the XML reading, the only thing we need to do is to call the base class to do the rest of the work and return the result.

return base.GetValidators(metadata, context, newAttributes);

Wrapping up

As we mentioned at the beginning of the post, using this approach you will gain a lot of flexibility on how you’re adding your model validations; also, you have a reusable way to add them to your model classes no matter where they are located.

The importance of profiling your application

It is well known that the ORM can help a lot in software development. In fact, they solve many problems that as developers we had in the past, specially talking about Opening and Closing Connections.
But as ORM frameworks are a powerful tool,they can also make a mess if they are not used in the proper way. Let me give you an example.
In the project I’m current working on, some users started experiencing some slowness when trying to load a page. I was assigned to investigate what was happening and propose a solution. I started by doing some profiling in the web application using Glimpse, adding some Trace messages to determine where was the problem and I noticed that the it was in a WCF service call that was taking 20 seconds to retrieve a single record with a lot of columns (because of all the needed joins).
This was by far unacceptable, so I started to doing further profiling on the Entity Framework using Entity Framework Profiler, and discovered that the slow service data was being fulfilled by a 4,000 lines query!.
I clearly understand that EF made a lot of extra and maybe unnecessary joins in order to automate the data exctraction, but, 4000 lines?! Really?!
After inspecting the long long query that EF Profiler showed me, I started noticing some duplicated SELECT statements. Then I went back to the service call and noticed that the code was something like this:

...
AsQueryable()
.Include("Account")
.Include("Account.Client")
.Include("Account.Client.Organization")
...

This seemed to be good on the first look, since we wanted to get the Account, the Client of that Account, and the Organization. But what EF is understanding is that we want to get the Account, and then AGAIN the Account and the Client, and then AGAIN the Account, AGAIN the Client and the Organization. This then was causing that duplicated SELECT statements I was talking before. So to fix the problem I deleted the extra lines and the code ended up like this

...
AsQueryable()
.Include("Account.Client.Organization")
...

and then by that, I reduced the lines of the resulting query, removed all the duplicated SELECT and reduced the execution time to 2 seconds!
So, as you can see, two extra lines can screw all the work you’re doing. So be careful of preventing this and do profile your applications before they go live.

Creating CodeCollaborator reviews using command line

Some months ago, I started using CodeCollaborator to create code reviews and ensure our developments have the desired quality.

We are at the end of our development sprint, so I tried to create some code reviews using the CodeCollaborator GUI with no success. Aparently, one of the latest Java VM updates “broke” my client; actually because we are using an old version of CodeCollaborator client (v 4) which cannot work under the latest versions of Java.

So if you run into the same problem, or just want to create your reviews using the command line follow the next steps described below. (Disclaimer: this was only tested on Windows and using SVN as source control, since that’s our current development environment),

  1. Open the command line prompt
  2. Go to your working copy folder, some thing like this
    cd “C:FolderFolderMyProjectWorkingCopyRoot”
  3. Login into your CodeCollaboration Server
    ccolab login  [ServerUrl] [UserName] [Password]

    You will get something like this

    Connecting to Code Collaborator Server https://[your.server.url]/
    Connected as: [Your name and username]
    
    New configuration worked - saving configuration settings
    Configuration key 'url' saved.
    Configuration key 'user' saved.
    Configuration key 'password' saved.
    Configuration key 'server-proxy-host' cleared.
    Configuration key 'server-proxy-port' cleared.
  4. You can upload specific files, changelists, etc. In my case I needed upload all the changes made on an specific revision, so I needed to pass a SVN diff as a parameter
    ccollab addsvndiffs new -r 27183:27190

    If you look I added the parameter “new” wich means I’m creating a new review (and will be named “Untitled Review”); you can also pass the review-id if you want to add files to an existing review.

With this, you can easily add files when creating or updating any CodeCollaborator review. For more information go to the CodeCollaboration command line reference

Overcome to Windows Azure free trial cancellation by adding a new plan

I decided to try Windows Azure more than 3 months ago, most likely because of it’s free 90-day offer. I can tell that I’m amazed on all the posibilies you have for free (Websites, Virtual machines, Mobile Services, etc.).

So then, I decided I wanted to continue with the service after my trial period ended by “acquiring” a Pay-As-You-Go plan (that as the name says, you pay only what you use only if you use it, so at the time of my purchase my total was $0.00).

Since I was swamped with many things at job, I didn’t payed much attention on the warning e-mails saying that my account was about to expire. As it might be expected, my account was cancelled 2 weeks ago. Last week I was trying to upgrade my account, so I signed up on the Pay-As-You-Go plan but after several tries and wating days, I still wasn’t able to use it to create new websites with it.

So this is the process that you need to follow in order to be able to use your new plan in your account:

  1. Acquire a new plan (Pay-As-You-Go, 6 or 12 month, etc.)
  2. Go to Preview Features page and select the feature that you want to enable for your new plan, in my case I wanted to be able to create websites again
  3. Click on “Try it now”
  4. Select the plan you want to associate (your new plan). In this case I’m showing an example with Media Services because I already joined to the Websites preview
After completing this, you will see a label on the screen saying that the feature is active
and you will be able to create websites (or any of the preview features that you selected) under your new plan
Sadly, my previous websites remain cancelled and it doesn’t seem to be possible to associate them with my new plan, or at least I couldn’t find a way to edit them on the manage portal.
So, if possible try to avoid your account cancellation, but if that already happened, this post might help you if your plan is not shown on some items on your account.

Solution to: “The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable”

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

Problem description

The error quoted above happened to me after I started using AutoMapper on my WCF service for updating my entities. Previously, I had a “manual map” to assing the properties that I received on my data contract to the entity that I got on my context and update it. When we decided to implement AutoMapper on our project, my mapping code started like this:

Mapper.CreateMap<MyDataContract, MyEntity>.IgnoreAllNonExisting();

Since all my properties were named the same on both classes, everything seemed to be easy peasy.

But sadly, I started gotting the error message

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

This message was driving me crazy, because of the text I was thinking that I was making something wrong with the FK relationships on the Database, or in someway the field was being passed null without being null on the contract, or somebody changed the table in someway (yeah sure, blame others :-P). After some deep look on the code (including debugging line by line all the updating process) I found the error.

Since I’m copying the modified values (from DataContract to Entity) when mapping, I’m passing all the values from one instance to the other. This sounds OK, right? I mean, I want all my changes to be persisted on the database.

Well, is not that easy. Since my contract comes from a web application, when saving there I don’t have all the navigation properties. When this is the case and I just map all, I’m also mapping and overwriting the values of this navigation properties, that in this case are null on my DataContract. This makes the Entity to update the ForeignKey Columns that belong to that navigation property to be null too. That is why the error says that you’re sending null values, because actually you are.

Solution

Since the problem is that we’re mapping null navigation properties, and we actually don’t need them to be updated on the Entity since they didn’t changed on the Contract, we need to ignore them on the mapping definition:

ForMember(dest => dest.RefundType, opt => opt.Ignore())

So my code ended up like this

Mapper.CreateMap<MyDataContract, MyEntity>
ForMember(dest => dest.NavigationProperty1, opt => opt.Ignore())
ForMember(dest => dest.NavigationProperty2, opt => opt.Ignore())
.IgnoreAllNonExisting();

And voilà! the problem is solved.

There’s no reason to be scared, anybody can code now. So just do it!!

I was thinking over this days on how the development tools have evolved. I think that anybody can just start doing code now, with only some little training or just following some of the courses that exist over the Internet. I’ve been working on software for several years, and as I can tell that I hadn’t a really hard time as the first Assembly or C coders did, things weren’t as easy as they are now.

Obviously, this doesn’t mean that anybody can be a Programmer, even less a Software Engineer. For getting into that, I think, there are more topics that need to be covered beyond how to code or use an IDE. Concepts of data structures, memory management, networking and OS internals are really important to do the things correctly, not just doing them.

But for that, there’s a lot of info too. Even more, there are some schools or private institutions that offer “converting” your non-software degree onto something pretty similar to a Bachelor on Computer Sciences.

Thanks to the new Visual Studio 2012, Nuget, Entity Framework, MVC3/4, WebApi, etc. starting on the .net world is much more easier than it was 6 years ago. Frameworks like jQuery and YUI are helping people to get into the Javascript world with much less difficulty, and the same has happened with Cake for PHP and Spring/SpringRoo on Java.

So there is no reason to be scared, anybody can code now. So if you have an idea, or you’ve wondered if can do coding as a hobby, just do it! Maybe you have a million-dollar idea 😉

 

Tic-Tac-R source code

After some tweaks, I’m releasing the source code of the Tic-Tac-R game. As you may find on the previous post, this game is an experiment on how to make a simple Tic-Tac-Toe game in a real-time web application using SignalR and host it on a cloud environment using Windows Azure.

I hope you find it useful for learning just as I did

Download the source code on Github

Updates to Tic-Tac-R. Now it works on Windows Azure =)

This is an update of my previous post

Update: Post with the source code of the game

After spending a lot of time testing and searching on the web, I wasn’t able to get the Tic-Tac-R game on my Windows Azure website. I tried a lot of things and can shameless say that I was totally lost, since my application was working fine on a shared hosting environment (GoDaddy) but not on my Windows Azure.

Thanks to an anonymous user that replied my question on StackOverflow I was able to know that this is a weird issue only happening there with some sort of caching by Windows Azure (neither really a bug on my app nor SignalR). The suggestion was changing enforce the transport to be Long Polling and with that the app started to work no the cloud. It is kinda little slow though, and I feel that some times the call that I make from the browser is lost, but is’s maybe just my imagination… Time and more testing will give us the answer… In the meanwhile, you play the game can be part of the beta testers of this simple game… It’s free!! =P

By the way, I updated SignalR to the just released 0.5.3 version and everything seems to keep working fine..

I hope you can share your opinion regarding Tic-Tac-R. It will be really appreciated it =)

Tic-Tac-R is alive!

After several weeks of work (and intensive learning) I’m releasing Tic-Tac-R for beta testing.

Tic-Tac-R is a little experiment of creating a traditional Tic-Tac-Toe game using SignalR, and it can be played here.

On the next days I will be posting the code on GitHub, once we’ve done all the changes and fixes that might come from the beta testing period.

So then, enjoy the game!

HealthyHarry: A healthy version of the traditional snake game over HTML5

As an experiment for learning HTML5 Canvas, I created this small game based on the popular “Snake” that many of us played on our old mobile phones (A Nokia 3320 in my case)

I deployed the game one of my Windows Azure websites and you can access it here. Initially I was thinking of buying a hosting account on a different place, but I think the cloud is a good place for host this kind of pet projects and Windows Azure is a great option, (IMHO) for 2 things: 1) Is extremely easy to use (saying creating websites, databases, deploying, etc) 2) It is free for 90 days, which is great if you are starting on the cloud “wave”. Let’s see what happen when the cloud bills begin to come.

So then, enjoy the game online for at least the next 60 days (when my free account expires :P)

The source code is hosted on Github. Feel free to do whatever you want with it