Skip to content

Instantly share code, notes, and snippets.

@plioi
Last active January 27, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plioi/9e51f1e86a2f5b552694 to your computer and use it in GitHub Desktop.
Save plioi/9e51f1e86a2f5b552694 to your computer and use it in GitHub Desktop.
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