Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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>>();//Exeption causing returning null value 
            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>>();//Exeption causing returning null value 
            if (!((handler != null) && handler is IValidationHandler<TCommand>))
            {
                throw new ValidationHandlerNotFoundException(typeof(TCommand));
            }  
            return handler.Validate(command);
        }
    }
}

Unity Configuration.cs in App start folder:
C#
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:

C#
[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 fail
            if (form.CategoryId == 0)
                return View("Create", form);
            else
                return View("Edit", form);
        }

Can anyone help regarding this issue.
-Thanks sindhu
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900