Created
July 9, 2013 13:09
-
-
Save yemrekeskin/5957190 to your computer and use it in GitHub Desktop.
Command Processor Class - Command Query Responsibility Separation with Command Processor in asp.net mvc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ProductAddHandler | |
| : ICommandHandler<ProductAddCommand> | |
| { | |
| private readonly IProductRepository _productRepository; | |
| public ProductAddHandler(IProductRepository productRepository) | |
| { | |
| this._productRepository = productRepository; | |
| } | |
| public ICommandResult Execute(ProductAddCommand command) | |
| { | |
| var product = new Models.Product | |
| { | |
| CreateDate = DateTime.Now, | |
| Decription = command.Decription, | |
| ProductName = command.ProductName, | |
| SerialNo = command.SerialNo, | |
| UnitPrice = command.UnitPrice | |
| }; | |
| _productRepository.Create(product); | |
| return new CommandResult(true); | |
| } | |
| } | |
| public class ProductController : Controller | |
| { | |
| private ICommandBus _commandBus; | |
| public ProductController(ICommandBus commandBus) | |
| { | |
| this._commandBus = commandBus; | |
| } | |
| public ActionResult Create() | |
| { | |
| ProductAddCommand cmd = new ProductAddCommand() | |
| { | |
| ProductName = "MAC Pro", | |
| Decription = "high tech. industry", | |
| SerialNo = "ASd43Er8#233W", | |
| UnitPrice = 1300 | |
| }; | |
| IEnumerable<ValidationResult> errors = _commandBus.Validate(cmd); | |
| ModelState.AddModelErrors(errors); | |
| if (ModelState.IsValid) | |
| { | |
| var result = _commandBus.Submit(cmd); | |
| if (result.Success) | |
| { | |
| } | |
| else | |
| { | |
| } | |
| } | |
| return View(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment