Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IHasResultCommand
- {
- Action SetResult { get; set; }
- }
- public class AddCategoryCommand : IHasResultCommand
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- public Action SetResult { get; set; } = () => { };
- }
- public class Category
- {
- public int Id { get; set; }
- public string Name { get; set; }
- }
- public class AddCategoryCommandHandler : ICommandHandler<AddCategoryCommand>
- {
- public void Handle(AddCategoryCommand command)
- {
- var cat = new Category
- {
- Name = command.Name
- };
- //ctx.Categories.Add(cat)
- command.SetResult = () =>
- {
- command.Id = cat.Id;
- };
- }
- }
- public class HasResultCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
- {
- ICommandHandler<TCommand> _handler;
- public HasResultCommandHandlerDecorator(ICommandHandler<TCommand> handler)
- {
- _handler = handler;
- }
- public void Handle(TCommand command)
- {
- _handler.Handle(command);
- if(command is IHasResultCommand cmd)
- {
- cmd.SetResult();
- }
- }
- }
- container.RegisterDecorator(
- typeof(ICommandHandler<>),
- typeof(TransactionCommandHandlerDecorator<>));
- container.RegisterDecorator(
- typeof(ICommandHandler<>),
- typeof(HasResultCommandHandlerDecorator<>));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement