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.
- if(ModelState.IsValid)
- {
- var command = new CreateOrUpdateCategoryCommand(form.CategoryId,form.Name, form.Description);
- IEnumerable<ValidationResult> errors= commandBus.Validate(command);
- if (errors.Count() > 0)
- {
- ModelState.AddModelErrors(errors);
- }
- if (ModelState.IsValid)
- {
- var result = commandBus.Submit(command);
- if (result.Success) return RedirectToAction("Index");
- }
- }
The following code is shown the Command Handler for command object CreateOrUpdateCategoryCommand.
- public class CreateOrUpdateCategoryHandler : ICommandHandler<CreateOrUpdateCategoryCommand>
- {
- private readonly ICategoryRepository categoryRepository;
- private readonly IUnitOfWork unitOfWork;
- public CreateOrUpdateCategoryHandler(ICategoryRepository categoryRepository, IUnitOfWork unitOfWork)
- {
- this.categoryRepository = categoryRepository;
- this.unitOfWork = unitOfWork;
- }
- public ICommandResult Execute(CreateOrUpdateCategoryCommand command)
- {
- var category = new Category
- {
- CategoryId = command.CategoryId,
- Name = command.Name,
- Description = command.Description
- };
- if (category.CategoryId == 0)
- categoryRepository.Add(category);
- else
- categoryRepository.Update(category);
- unitOfWork.Commit();
- return new CommandResult(true);
- }
- }
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.
- public class DefaultCommandBus : ICommandBus
- {
- public ICommandResult Submit<TCommand>(TCommand command) where TCommand: ICommand
- {
- var handler = DependencyResolver.Current.GetService<ICommandHandler<TCommand>>();
- if (!((handler != null) && handler is ICommandHandler<TCommand>))
- {
- throw new CommandHandlerNotFoundException(typeof(TCommand));
- }
- return handler.Execute(command);
-
- }
- public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand
- {
- var handler = DependencyResolver.Current.GetService<IValidationHandler<TCommand>>();
- if (!((handler != null) && handler is IValidationHandler<TCommand>))
- {
- throw new ValidationHandlerNotFoundException(typeof(TCommand));
- }
- return handler.Validate(command);
- }
- }
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