DDD ORM, CQRS. DDD, , , , DDD , , , , .
. CommandHandler CommandData. a CommandData, DTO. . CommandHandler , -, . :
public interface ICommandHandler<T>
{
T Handle(T command);
}
public class ContributeToPhaseCommandData
{
public Guid ContributionToPhaseId { get; set; }
public Guid ActivityId { get; set; }
public Guid PhaseId { get; set; }
public Participant Contributor { get; set; }
public DateTime ActivityDueDate { get; set; }
public bool Success { get; set; }
public string CommandResultMessage { get; set; }
public ContributeToPhaseCommandData( ) { }
}
public class ContributeToPhaseCommandHandler : ICommandHandler<ContributeToPhaseCommandData>
{
public ContributeToPhaseCommandHandler( )
{
}
public ContributeToPhaseCommandData Handle(ContributeToPhaseCommandData command)
{
return command;
}
}
Application Layer, - (- ). , ( ), , ( ), , requester (. ) .
, ? , , . , .
, agreggates, / . , / , , . .
, . , lighter , - ( ).
, (KISS) :
public SomeResponseToCaller ContributeToPhase(ICommandHandler<ContributeToPhaseCommandData> command, Guid phaseId, IPrincipal caller, IAuthorizationService authorizer)
{
if (!authorizer.authorizes(caller))
this.ExceptionHandler.Handle("Caller is not authorized! Shall we log this info?");
using(var db = new ActivitiesContext())
{
ContributeToPhaseCommandData data = db.Phases
.Select(p => new ContributeToPhaseCommandData()
{
ActivityId = p.ActivityId,
PhaseId = p.Id,
Contributor = p.Activity.Participants.SingleOrDefault(part => part.Name == caller.Identity.Name)
ActivityDueDate = p.Activity.DueDate
}).SingleOrDefault(p => p.Id == phaseId);
if (data == null)
this.ExceptionHandler.Handle("Phase not found");
if (data.Contributor == null)
this.ExceptionHandler.Handle("Caller is not a participant of this Activity!!!!");
data.ContributionToPhaseId = Guid.NewGuid();
var result = command.Handle(data);
db.SaveChanges();
return new SomeResponseToCaller() {
Success = result.Success,
ContributionId = result.ContributionToPhaseId,
Message = result.CommandResultMessage
};
}
}
ExceptionHandler - , IExcepionHandler, . Application. , AuthorizationService .
, .
CQRS. , , , Querying from Storing. :
... - , , , . , , CQRS .
, , - , . . DTO/ViewModels.
, , , , , -, " " . , /, , .
, , , . , , .
, NoSQL, ( ), . , , , .
, , , (, SQL EF). , . , , CQRS, , , . , - ().