Hi I am using
EFMVC template in that i am using both mvc controllers and api controller in same application. but while creating updating and deleting it gives error: Command handler exception: CreateOrUpdateCommand not found in DEfaultCommandBus file.
Here is my DefaultCommandBus:
using System.Collections.Generic;
using System.Web.Mvc;
using EFMVC.CommandProcessor.Command;
using EFMVC.Core.Common;
namespace EFMVC.CommandProcessor.Dispatcher
{
public class DefaultCommandBus : ICommandBus
{
public ICommandResult Submit<TCommand>(TCommand command) where TCommand: ICommand
{
ICommandHandler<TCommand> 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
{
IValidationHandler<TCommand> handler = DependencyResolver.Current.GetService<IValidationHandler<TCommand>>();
if (!((handler != null) && handler is IValidationHandler<TCommand>))
{
throw new ValidationHandlerNotFoundException(typeof(TCommand));
}
return handler.Validate(command);
}
}
}
Unity Configuration.cs in App start folder:
using Autofac.Integration.Mvc;
using EFMVC.CommandProcessor.Command;
using EFMVC.CommandProcessor.Dispatcher;
using EFMVC.Data.Infrastructure;
using EFMVC.Data.Repositories;
using EFMVC.Domain.Commands;
using EFMVC.Domain.Handlers;
using EFMVC.Web.Core.Authentication;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Unity.Mvc4;
namespace EFMVC.Web.API.App_Start
{
public class UnityConfiguration
{
public static void ConfigureIoCContainer()
{
IUnityContainer container = new UnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
RegisterTypes(container);
}
private static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<ICommandBus ,DefaultCommandBus>(new TransientLifetimeManager());
container.RegisterType<IDatabaseFactory, DatabaseFactory>(new TransientLifetimeManager());
container.RegisterType<ICategoryRepository, CategoryRepository>(new TransientLifetimeManager());
container.RegisterType<IEventRepository, EventRepository>(new TransientLifetimeManager());
container.RegisterType<IPhotoRepository, PhotoRepository>(new TransientLifetimeManager());
container.RegisterType<INewsRepository, NewsRepository>(new TransientLifetimeManager());
container.RegisterType<INotificationRepository, NotificationRepository>(new TransientLifetimeManager());
container.RegisterType<IUserRepository, UserRepository>(new TransientLifetimeManager());
container.RegisterType<IUserDetailRepository, UserDetailRepository>(new TransientLifetimeManager());
container.RegisterType<IFormsAuthentication, DefaultFormsAuthentication>(new TransientLifetimeManager());
container.RegisterType<ICommandResults, CommandResults>(new TransientLifetimeManager());
container.RegisterType<IUnitOfWork, UnitOfWork>(new TransientLifetimeManager());
container.RegisterType<IDisposable, Disposable>(new TransientLifetimeManager());
container.RegisterType<IDependencyResolver, AutofacDependencyResolver >(new TransientLifetimeManager());
}
}
}
Post action in Category Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(CategoryFormModel form)
{
if (ModelState.IsValid)
{
Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>();
var command = Mapper.Map<CategoryFormModel, CreateOrUpdateCategoryCommand>(form);
IEnumerable<ValidationResult> errors = commandBus.Validate(command);
ModelState.AddModelErrors(errors);
if (ModelState.IsValid)
{
var result = commandBus.Submit(command);
if (result.Success) return RedirectToAction("Index");
}
}
if (form.CategoryId == 0)
return View("Create", form);
else
return View("Edit", form);
}
Can anyone help regarding this issue.
-Thanks sindhu