Last active
January 27, 2016 14:23
-
-
Save plioi/9e51f1e86a2f5b552694 to your computer and use it in GitHub Desktop.
This file contains 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
namespace ContactList.Features.Contact | |
{ | |
using System; | |
using AutoMapper; | |
using ContactLists.Core; | |
using Core.Domain; | |
using FluentValidation; | |
using MediatR; | |
public class ContactEdit | |
{ | |
public class Query : IRequest<Command> | |
{ | |
public Guid Id { get; set; } | |
} | |
public class QueryHandler : IRequestHandler<Query, Command> | |
{ | |
private readonly ContactsContext _database; | |
public QueryHandler(ContactsContext database) | |
{ | |
_database = database; | |
} | |
public Command Handle(Query message) | |
{ | |
var contact = _database.Contacts.Find(message.Id); | |
return Mapper.Map<Contact, Command>(contact); | |
} | |
} | |
public class Command : IRequest | |
{ | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
public string Email { get; set; } | |
public string Phone { get; set; } | |
} | |
public class Validator : AbstractValidator<Command> | |
{ | |
public Validator() | |
{ | |
RuleFor(x => x.Name).NotEmpty(); | |
RuleFor(x => x.Email).EmailAddress(); | |
RuleFor(x => x.Name).Length(1, 255); | |
RuleFor(x => x.Email).Length(1, 255); | |
RuleFor(x => x.Phone).Length(1, 50); | |
} | |
} | |
public class CommandHandler : RequestHandler<Command> | |
{ | |
private readonly ContactsContext _database; | |
public CommandHandler(ContactsContext database) | |
{ | |
_database = database; | |
} | |
protected override void HandleCore(Command message) | |
{ | |
var contact = _database.Contacts.Find(message.Id); | |
Mapper.Map(message, contact); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment