Friday, May 27, 2011

Fluent Validation remote validation

Recently I was looking for a way to do remote validation with FluentValidation, for an asp.net mvc 3 application. But looking at this discussion I think there is no official way to do this right now. Although Jeremy does seem to have plan for this.

http://fluentvalidation.codeplex.com/discussions/258348


So, I had to create my own remote validator extension to FluentValidation. For those who cannot wait for the official remote validation for fluent validation. Here's how I did it.

//Create a validator, that does nothing on the server side.
public class RemoteValidator : PropertyValidator
{
  public string Url { get; private set; }
  public string HttpMethod { get; private set; }
  public string AdditionalFields { get; private set; }

  public RemoteValidator(string errorMessage, 
                         string action, 
                         string controller, 
                         HttpVerbs HttpVerb = HttpVerbs.Get, 
                         string additionalFields = "")
        : base(errorMessage)
  {
    var httpContext = HttpContext.Current;

    if (httpContext == null)
    {
      var request = new HttpRequest("/", "http://ubasolutions.com", "");
      var response = new HttpResponse(new StringWriter());
      httpContext = new HttpContext(request, response);
    }

    var httpContextBase = new HttpContextWrapper(httpContext);
    var routeData = new RouteData();
    var requestContext = new RequestContext(httpContextBase, routeData);

    var helper = new UrlHelper(requestContext);

    Url = helper.Action(action, controller);
    HttpMethod = HttpVerb.ToString();
    AdditionalFields = additionalFields;
  }

  protected override bool IsValid(PropertyValidatorContext context)
  {
    //This is not a server side validation rule. So, should not effect at the server side.
    return true;
  }
}

//Create a MVC wrapper for the validator.
public class RemoteFluentValidationPropertyValidator : 
                 FluentValidationPropertyValidator
{
  private RemoteValidator RemoteValidator
  {
    get { return (RemoteValidator)Validator; }
  }

  public RemoteFluentValidationPropertyValidator(ModelMetadata metadata, 
                                                 ControllerContext controllerContext, 
                                                 PropertyRule propertyDescription, 
                                                 IPropertyValidator validator)
        : base(metadata, controllerContext, propertyDescription, validator)
  {
    ShouldValidate = false;
  }

  public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
  {
    if (!ShouldGenerateClientSideRules()) yield break;

    var formatter = new MessageFormatter().AppendPropertyName(Rule.PropertyDescription);
    string message = formatter.BuildMessage(RemoteValidator.ErrorMessageSource.GetString());

    //This is the rule that asp.net mvc 3, uses for Remote attribute. 
    yield return new ModelClientValidationRemoteRule(message, RemoteValidator.Url, RemoteValidator.HttpMethod, RemoteValidator.AdditionalFields);
  }
}

Thats all. Now you'll have to register the Validator inside the applicaition_start in the global.asax file.

           //Note that I'm using NinjectValidatorFactory for injecting Fluent validation into the modelbinders. So, this configuration may differ a little on every system.
            FluentValidationModelValidationFactory validationFactory = (metadata, context, rule, validator) => new RemoteFluentValidationPropertyValidator(metadata, context, rule, validator);

            FluentValidationModelValidatorProvider.Configure(x => x.Add(typeof(RemoteValidator), validationFactory));

            NinjectValidatorFactory ninjectValidatorFactory = new NinjectValidatorFactory(kernel);
            var validationProvider = new FluentValidationModelValidatorProvider(ninjectValidatorFactory);
            validationProvider.Add(typeof(RemoteValidator), validationFactory);
            ModelValidatorProviders.Providers.Add(validationProvider);
            
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
            kernel.Load<FluentValidatorModule>();



How to apply the validation Rule?

Ok, now we will be setting rules for validation.
RuleFor(x => x.UserName).NotEmpty().SetValidator(new RemoteValidator("user name already exists", "ValidateUserName", "Account", System.Web.Mvc.HttpVerbs.Post));

Well, that was quite simple wasn't it?

Here is the code for ValidateUserName Action inside Account controller
        public ActionResult ValidateUserName(string username)
        {
            var user = UserRepository.FindSingleByUsername(username);
            if (user == null)
                return Json(true, JsonRequestBehavior.AllowGet);
            return Json("Username already exists", JsonRequestBehavior.AllowGet);
        }

Thats it folks. Your remote validation is ready to go.

N.B.
You can see that this is just a wrapper around Asp.net mvc's Remote Attribute, and works more or less on same way the Remote attribute would work.

Asp.net mvc 3 unobtrusive validation fires validation on every keyup event. So, I had to change jquery.validate.unobtrusive.js to fire validation only on blur events. I still could not find a reliable way to do this only on the elements with remote validation, since they(microsoft) are doing validation at the form level instead of element level. To be able to set individual settings, a massive rewrite of jquery.validate.unobtrusive.js may be required.

Update May 11, 2012

For all those looking for the change in the way unobtrusive validator works, I will point you to this stack overflow question: http://stackoverflow.com/questions/4798855/mvc-3-specifying-validation-trigger-for-remote-validation

I used the technique mentioned in this answer: http://stackoverflow.com/a/4836653

Thursday, April 21, 2011

Pluggable/Hookable C++ class

disclaimer
C++ gurus please don't shoot me if you find this one offensive.

I was trying to get a pluggable/hookable class that could let access to it's private members to other class which can hook into it. Found a nice enough way to do so using a template method at stack overflow.

http://stackoverflow.com/questions/424104/can-i-access-private-members-from-outside-the-class-without-using-friends

My requirements:
I'm working with the TTF file parser. The basic operations that I know of right now are:
1. read the file.
2. pack the read files into a valid TTF file.

The classes that represent the TTF table should be able to provide a way to other classes to hook into it and change it's state. This is because the font can require many unpredictable transformations. For example transform TTF font to other formats such as SVG, WOFF etc.

The way we would like to transform the font is quite unpredictable.

If anyone knows a better solution, you are always welcome.

Improvements
One area that I think we can improve the implementation is to have a constraint on the template method. So, that only a class inheriting some specific class can hook into the method.

Further note:
It also shows that you should be careful while designing your classes. You may unwittingly open a back door to the class, that may cause a havoc.

I'll try to post example codes in future.

Monday, March 14, 2011

Enums with string values

Many times I've wanted to have enums, that can be represented in both numeric(integer) form and string form. The problem with ToString method is that it is sometimes in not human friendly. For example if I've an enum called "Language" and "ZnCH" as one of its values. Now, lets suppose we want to show users the list of languages available in the site. Using in-built ToString method will just show "ZnCH", while we may be wanting then to see "Simplified Chinese".

Without boring you further let's see how to implement it. We will use System.ComponentModel.DescriptionAttribute to store the string value, and extension method to retrieve the string value. Here's the code
using System;
using System.ComponentModel;

public enum Language : int
{
  [Description("English")]
  EnUS = 1,
  [Description("Simplified chinese")]
  ZhCN = 2
}

public static class EnumExtensions
{
  public static string ToString(this Enum enumInput, bool showFromDescription)
  {
    if (showFromDescription)
    {
      DescriptionAttribute[] descAttributes = (DescriptionAttribute[])enumInput.GetType().GetField(enumInput.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
      if (descAttributes.Length > 0)
      {
        if (!string.IsNullOrWhiteSpace(descAttributes[0].Description))
        {
          return descAttributes[0].Description;
        }
      }
    }
    return enumInput.ToString();
  }
}

public class Program
{
  public static void Main(params string[] args)
  {
    Language lang = Language.ZhCN;
    Console.WriteLine(lang.ToString(true));//outputs "Simplified Chinese"
  }
}

Thursday, February 24, 2011

My idea about an ideal Project management system.

After using/studying few project management systems, activecollab, JIRA, Redmine to name few. I do clearly prefer Redmine, I still am wanting something more. There is a feeling like something is missing. I'm have figured out a list of features that I'd like to see in a project management system (although I cannot guarantee that there won't be another list if these were some how fulfilled). Let's see in detail what I mean.

Team:
Project management has always been a team effort and a team is composed of individuals. Almost all the project management system focus on the projects, tasks, due-dates etc. But there is least focus on team. I'm of opinion that a project management system must give equal focus to team along with other artifacts. In a team everyone may not have equal skill, there are days when people are absent. So, I think a PMS should make it easy for a project manager to manage by individual.

Social process
Project management is always a social process, and a project management system should be able to consider this fact. As a social process the PMS should meet somewhere near facebook. It is mainly about communication. But, by communication I’m not referring to just the communication between client/customer and the project manager. The communication here considers more dimensions. A team member may want to communicate with other team members; as a team member I may want to share some idea with others. I’d like more control over the privacy and public ness of a message being passed, not just a rudimentary control but rather a bit more elaborated and sophisticated control over the message privacy. Something similar to Facebook walls, inboxes, chats etc.

Version control
A project management system should have some sort of version control over the tasks, bugs etc, (mind you I'm not talking about the version control of codes). Almost every time I do some work on a project, I'll be pulling down the works that assigned to me and create a TO-DO for my personal purpose that I may or may not share with others. I do mark things as done and put some comment, for my personal reference, at end of the day I do select the important ones from my notepad (I generally use notepad, for my To-do) and update the project manager. I think it would be better if the project management software did allow me to create my To-do list and make as many changes as I'd like in the tasks assigned to me, without those changes getting reflected to others. At end of the day I'd select and commit the changes that I want others to see.

So, folks these are the three main things I'd like to have in a project management system, along with other general stuffs. So, in conclusion what I want from software project management is the features of Facebook + Redmine + GIT(git here is not for code revision control, but rather the tasks and activities, as I mentioned earlier), combined together.

Well I think I'm quite lazy(I'm not trying to build these things my self, at least not in a near future), and way too demanding. But then this is the human nature :)

Monday, February 14, 2011

Removing the NInject dependency from the Domain model and Entity Framework classes.

Hi, folks this post is continuation of my previous post Entity Framework CTP5 injecting with NInject. Let's have a brief revision. We have created a model "Member" class that has dependency on a MemberRepository and an AuthenticationService. We also have created a DbContext class that is capable of injecting the dependencies to created Entities.

One thing that I really don't like about the implementation is that, now the Model as well as the Datacontext is dependent on the NInject framework. Although I'm beginning to love NInject as a dependency framework, but I think the application as whole should be unaware of an IoC framework (As Nate says it is matter of opinion). Now, lets move ahead and try to remove dependency from the NInject framework.

I will first focus on the Member class, as I cannot specially bear to have a NInject dependency in my Domain model. One cool feature of NInject is that it provides the developers with ways to change the manner in which the dependencies are injected. That means although by default it uses "Inject" attribute we can change that behavior. Since, the name of method "InjectDependency" is quite self explanatory, I think it becomes quite unnecessary to decorate that method with extra attribute whose sole purpose is to point out "hey this method should be used to inject the external dependencies".

Here's how we do it. Below is a NInject heuristic that can decides whether to consider a method as an entry point for dependency injection or not. The decision is made based on a lamda expression.
public class NInjectMemberSelector : NinjectComponent, IInjectionHeuristic
{

  public bool ShouldInject(System.Reflection.MemberInfo member)
  {
    return member.MemberType == MemberTypes.Method && _predicate(member);
  }

  private readonly Func<MemberInfo, bool> _predicate;

  public NInjectMemberSelector(Func<MemberInfo, bool> predicate)
  {
    _predicate = predicate;
  }

}
Now let's create a NInject module that uses this heuristics.
public class InjectAllMethodsNamedInjectDependencyModule : NinjectModule
{
  public override void Load()
  {
    var selector = Kernel.Components.Get<ISelector>();
    //specify that a method with name "InjectDependency",should be considered as an entry point for Injection.
    var heuristic = new NInjectMemberSelector(member => member.Name.Equals("InjectDependency"));
    selector.InjectionHeuristics.Add(heuristic);
  }
}
This module now needs to be registered to the NInject Kernel.
  IKernel kernel = new StandardKernel();
  kernel.Load(new InjectAllMethodsNamedInjectDependencyModule());
Now, a simple way to avoid NInject dependency from the DbContext. Let's create an Interface named IInject and a class NInjectAdapter that implements the interface
public interface IInject
{
  void Inject(object objectToInject);
}

public class NinjectAdapter : IInject
{
  private IKernel _kernel;

  public NinjectAdapter(IKernel kernel)
  {
    _kernel = kernel;
  }

  public void Inject(object objectToInject)
  {
    _kernel.Inject(objectToInject);
  }
}
Now, let's use our custom IInject interface instead of instead of the IKernel
public class NInjectDataContext : DbContext
{
  //modified IKernel to IInject
  public NInjectDataContext(DbConnection dbconnection,  IInject kernel): base(dbconnection)
  {
    this.Kernel = kernel;
    (this as IObjectContextAdapter).ObjectContext.ObjectStateManager.ObjectStateManagerChanged += ObjectStateManagerChanged;
  }

  //other thing remains as it is.....
  //...

  //modified IKernel to IInject
  public IInject Kernel
  {
    get;
    private set;
  }
}
This is it, we have removed the dependency on NInject from our domain model as well as the DbContext class. Hope this would help someone.

Saturday, February 12, 2011

Entity Framework CTP5 injecting with NInject

Hello, this is Nripendra Nath Newa. This is the first time I'm trying to blog some of my thoughts, and I think it's quite a difficult subject that I've choosen.

Now, that microsoft has made its move towards supporting code-first approach for its ADO.Net Entity Framework, and has released developer's preview CTP5, many developer has been using the framework and giving feedback.

I have been currently trying out to practically implement the DDD approach of software development, using the EF code-first ctp5. After studying few books and blogs on DDD and EF code-first, I'm now all set to get my hands dirty. While implementing the DDD approach, it is quite common to use various services or even repositories(though second one seems to be controversial), inside the domain object. So I was trying to find if EF CTP5 has any support for such dependency injection, but I was quite disappointed to see it did not. After a series of googling I did come across a good enough approach. Although we don't have any control over the object materialization, we do have some control as soon as the object has been materialized. And, it was just fine for me(something is better than nothing). It turned out that using NInject to inject already constructed object is a very trivial task :). Thanks a lot to Nate Kohari, the creator of wonderful NInject.

I know it is enough of ranting, and you may be more interested in looking at some code. So here we go, first a domain model called "Member"

public class Member
{
  //The dependencies
  private IMemberRepository MemberRepository { get; set; }
  private IAuthenticationService AuthenticationService { get; set; }

  public long MemberId { get; set; }
  public string UserName { get; set; }
  public string Password { get; set; }
  public string Email { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  //A member must be able to sign in.
  public IDictionary<string, list<string>> SignIn(bool remember)
  {
    var errors = new Dictionary<string, list<string>>();

    //Use of injected object
    var existingMember = MemberRepository.GetSingle(member => member.UserName == this.UserName && member.Password == this.Password);
    if (existingMember != null)
    {
      //Use of injected object
      AuthenticationService.SignIn(this.UserName, remember);
    }
    else
    {
       if (!errors.ContainsKey("UserName,Password"))
       {
         var errorMessages = new List<string>{WTDErrors.UsernameOrPassword};
         errors.Add("UserName,Password", errorMessages);
       }
    }
    return errors;
  }
  //end SignIn()

  //A visitor must be able to register to become a member
  public IDictionary<string, list<string>> RegisterToBecomeMember()
  {
    //even this could be injected, but I'm ok with this for now.
    var validator = new MemberValidator(MemberRepository);
    var errors = new Dictionary<string, list<string>>();

    //The validator objects will contain all the validation rules, so that the domain model doesn't get cluttered. But still the domain object enforces the business rules.
    var validationResult = validator.Validate(this);
    if (validationResult.IsValid)
    {
      MemberRepository.Add(this);
      MemberRepository.Save();
    }
    else
    {
      foreach (ValidationFailure failure in validationResult.Errors)
      {
        var errorMsg = failure.ErrorMessage;
        if (!errors.ContainsKey(failure.PropertyName))
        {
          errors.Add(failure.PropertyName, new List<string>{errorMsg});
        }
        else
        {
          errors[failure.PropertyName].Add(errorMsg);
        }
      }
    }
    return errors;
  }
  //end RegisterToBecomeMember()
}

Leaving all other details aside, the member model is dependent on two external objects, a member-repository, and an authentication service. It is always a good practice to decouple such concerns, from maintainability point of view. A standard way to decouple such external objects is to use dependency injection. The most popular and preferred approach to inject such dependencies would be to use a constructor, for example:

public Member(IMemberRepository memberRepository, IAuthenticationService authenticationService)
{
  this.MemberRepository = memberRepository;
  this.AuthenticationService = authenticationService;
}

But as, discussed earlier it is not possible to change how the objects are materialized in the Entity Framework, so we don't have an option to use the constructor to inject the dependency. Other popular techniques to do dependency injection, are to use property injection or the method injection. In this case I do find method injection more appealing, (note that it is just my personal preference, and property injection is also equally valid approach). So, I'll use a method named "InjectDependencies" as a point to inject the dependencies into the objects.
public InjectDependencies(IMemberRepository memberRepository, IAuthenticationService authenticationService)
{
  this.MemberRepository = memberRepository;
  this.AuthenticationService = authenticationService;
}

Now, NInject has a very easy way to notify that a method or property is injectable. It provides an attribute named "Inject". So, decorating the method "InjectDependencies" with an "Inject" attribute will assure that NInject knows how to inject the dependencies, for example:

[Inject]
public InjectDependencies(IMemberRepository memberRepository, IAuthenticationService authenticationService)
{
  this.MemberRepository = memberRepository;
  this.AuthenticationService = authenticationService;
}

Once we put the above method in the Member class, our domain-object is ready for the dependency injection. The final Member class will look something like this:

public class Member
{
  [Inject]
  public InjectDependencies(IMemberRepository memberRepository, IAuthenticationService authenticationService)
  {
    this.MemberRepository = memberRepository;
    this.AuthenticationService = authenticationService;
  }

  //The dependencies
  private IMemberRepository MemberRepository { get; set; }
  private IAuthenticationService AuthenticationService { get; set; }

  public long MemberId { get; set; }
  public string UserName { get; set; }
  public string Password { get; set; }
  public string Email { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  //A member must be able to sign in.
  public IDictionary<string, list<string>> SignIn(bool remember)
  {
    var errors = new Dictionary<string, list<string>>();

    //Use of injected object
    var existingMember = MemberRepository.GetSingle(member => member.UserName == this.UserName && member.Password == this.Password);
    if (existingMember != null)
    {
      //Use of injected object
      AuthenticationService.SignIn(this.UserName, remember);
    }
    else
    {
       if (!errors.ContainsKey("UserName,Password"))
       {
         var errorMessages = new List<string>{WTDErrors.UsernameOrPassword};
         errors.Add("UserName,Password", errorMessages);
       }
    }
    return errors;
  }
  //end SignIn()

  //A visitor must be able to register to become a member
  public IDictionary<string, list<string>> RegisterToBecomeMember()
  {
    //even this could be injected, but I'm ok with this for now.
    var validator = new MemberValidator(MemberRepository);
    var errors = new Dictionary<string, list<string>>();

    //The validator objects will contain all the validation rules, so that the domain model doesn't get cluttered. But still the domain object enforces the business rules.
    var validationResult = validator.Validate(this);
    if (validationResult.IsValid)
    {
      MemberRepository.Add(this);
      MemberRepository.Save();
    }
    else
    {
      foreach (ValidationFailure failure in validationResult.Errors)
      {
        var errorMsg = failure.ErrorMessage;
        if (!errors.ContainsKey(failure.PropertyName))
        {
          errors.Add(failure.PropertyName, new List<string>{errorMsg});
        }
        else
        {
          errors[failure.PropertyName].Add(errorMsg);
        }
      }
    }
    return errors;
  }
  //end RegisterToBecomeMember()
}

Now, let's move forward and make our DbContext ready to inject the dependencies.

public class NInjectDataContext : DbContext
{
  public NInjectDataContext(DbConnection dbconnection,  IKernel kernel): base(dbconnection)
  {
    this.Kernel = kernel;
    (this as IObjectContextAdapter).ObjectContext.ObjectStateManager.ObjectStateManagerChanged += ObjectStateManagerChanged;
  }
  //end constructor

  public DbSet<member> Members { get; set; }

  private void ObjectStateManagerChanged(object sender, CollectionChangeEventArgs e)
  {
    if (e.Action != CollectionChangeAction.Add)
      return;
    var state = (this as IObjectContextAdapter).ObjectContext.ObjectStateManager.GetObjectStateEntry(e.Element).State;
    
    //discard all entities that are not loaded from database.
    if (state != System.Data.EntityState.Unchanged)
      return;
    ObjectContext_ObjectMaterialized(e.Element);
  }
  //end ObjectStateManagerChanged()

  private void ObjectContext_ObjectMaterialized(object element)
  {
    if (Kernel != null)
    {
      //This is the place where injection takes place.
      Kernel.Inject(element);
    }
  }
  //end ObjectContext_ObjectMaterialized()

  public IKernel Kernel
  {
    get;
    private set;
  }
}

This is it, now we have prepared a model that is dependency ready, and a DbContext that can inject dependencies after the objects are materialized. As of now following code will return a member with the dependencies injected:

var kernel = new StandardKernel(); var context = new NInjectDataContext(new SqlConnection(ConnectionStringProvider.ProductionConnection), kernel);
var queriedMember = context.Members.Where(member => member.Id ==1).SingleOrDefault();

Now, I just have a single concern that our Domain-model and the DbContext has yet another dependency upon, the NInject framework. But NInject is a very fantastic and power-full framework which has many extension points, so turning off the dependencies is quite a breeze.

This post has already become very long, so we will try to look at how the dependencies can be removed from NInject in some later posts. Any thoughts are very welcome.

References:
The code for DbContext, has been taken from (with some modifications).
http://rogeralsing.com/2009/05/30/entity-framework-4-entity-dependency-injection/

I was motivated to write this code, and in turn this blog, after reading this stack-overflow question:
http://stackoverflow.com/questions/4562276/entity-framework-ctp5-and-ninject-as-my-ioc

N.B.
I'm not a native English speaker, so I'd like to apologize if the above contents aren't very readable. Also, since this is my first blogging experience I'd be very thankful to anyone who points me towards the right direction.