EFMVC 1.0 Preview Released

A while ago, I have released EFMVC, a demo web app built using ASP.NET MVC 3, EF Code First and Unity. The EFMVC app has demonstrated many architectural patterns and practices such as Generic Repository, Repository pattern, Unit of Work pattern, Dependency Injection pattern and Application Service later.  Today I would like to announce the release of EFMVC 1.0 Preview. This release is an early preview release that has made some changes in the solution architecture.  The preview is written in ASP.NET MVC 3 version and will be migrated to ASP.NET MVC 4 with new features on later.

Changes in EFMVC 1.0

In EFMVC 1.0, I have made some changes on the solution architecture. I have completely re-written the Application Service layer where I have added some CQRS flavor on the Solution Architecture. But please keep in mind that this is not the implementation of CQRS pattern. Now every write operations (Create, Update and Delete) represent a command and these commands will be executed by using a Command Handler. These command handlers would be hooked by a command dispatcher based on the command type. The following code in the Controller class is creating a Command object and will submit to the Command Bus. Before submitting the Command, we validate the Command object using the validate method of Command Bus.


Code Snippet
  1. if(ModelState.IsValid)
  2. {
  3.     var command = new CreateOrUpdateCategoryCommand(form.CategoryId,form.Name, form.Description);
  4.     IEnumerable<ValidationResult> errors=  commandBus.Validate(command);
  5.     if (errors.Count() > 0)
  6.     {
  7.         ModelState.AddModelErrors(errors);
  8.     }
  9.     if (ModelState.IsValid)
  10.     {
  11.         var result = commandBus.Submit(command);
  12.         if (result.Success) return RedirectToAction("Index");                 
  13.     }                
  14. }

 The following code is shown the Command Handler for command object CreateOrUpdateCategoryCommand.


Code Snippet
  1. public class CreateOrUpdateCategoryHandler : ICommandHandler<CreateOrUpdateCategoryCommand>
  2. {
  3.     private readonly ICategoryRepository categoryRepository;
  4.     private readonly IUnitOfWork unitOfWork;
  5.     public CreateOrUpdateCategoryHandler(ICategoryRepository categoryRepository, IUnitOfWork unitOfWork)
  6.     {
  7.         this.categoryRepository = categoryRepository;
  8.         this.unitOfWork = unitOfWork;
  9.     }
  10.     public ICommandResult Execute(CreateOrUpdateCategoryCommand command)
  11.     {
  12.         var category = new Category
  13.         {
  14.             CategoryId = command.CategoryId,
  15.             Name = command.Name,
  16.             Description = command.Description
  17.         };
  18.         if (category.CategoryId == 0)
  19.             categoryRepository.Add(category);
  20.         else
  21.             categoryRepository.Update(category);
  22.         unitOfWork.Commit();
  23.         return new CommandResult(true);
  24.     }
  25. }

 The right Command Handler would be hooked by a Command Dispatcher object based on the command type. The following code is shown the implementation of Command Dispatcher object.


Code Snippet
  1. public class DefaultCommandBus : ICommandBus
  2. {
  3.     public ICommandResult Submit<TCommand>(TCommand command) where TCommand: ICommand
  4.     {    
  5.         var handler = DependencyResolver.Current.GetService<ICommandHandler<TCommand>>();
  6.         if (!((handler != null) && handler is ICommandHandler<TCommand>))
  7.         {
  8.             throw new CommandHandlerNotFoundException(typeof(TCommand));
  9.         }  
  10.         return handler.Execute(command);

  11.     }
  12.     public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand
  13.     {
  14.         var handler = DependencyResolver.Current.GetService<IValidationHandler<TCommand>>();
  15.         if (!((handler != null) && handler is IValidationHandler<TCommand>))
  16.         {
  17.             throw new ValidationHandlerNotFoundException(typeof(TCommand));
  18.         }  
  19.         return handler.Validate(command);
  20.     }
  21. }

 The current version is executing the command in synchronous way and future versions will be provide the support for executing the commands asynchronous way using a different architecture approach. The latest EFMVC version is using Autofac for dependency injection over Microsoft Unity.

Source Code

You can download the source code from http://efmvc.codeplex.com

Published Wednesday, October 12, 2011 8:01 AM by shiju


# re: EFMVC 1.0 Preview Released

Thursday, October 13, 2011 1:28 PM by Sean Kearon

The first link in the article is broken.

# re: EFMVC 1.0 Preview Released

Thursday, October 20, 2011 11:02 AM by Henk

Nice job mate :) Good learning stuff here!

# re: EFMVC 1.0 Preview Released

Monday, October 24, 2011 10:08 AM by Henk


I'm missing the explanation why you choose these new architecture changes over the 'old' one.

I'm very curious why you decided the make these changes. Did you encounter some problems with version 0.5? Or is it just that you wanna try some new patterns?

Would be great if you could give an explanation.

# re: EFMVC 1.0 Preview Released

Monday, October 24, 2011 6:50 PM by bulent

How can we do pagination in CategoryController.cs?

Thank you.

Leave a Comment