Answer the question
In order to leave comments, you need to log in
Where to open, save UnitOfWork?
Here I am trying to master the CQRS or Command/Query approach.
Internet code examples.
Example1.
public class CreateUserCommandHandler : EFCommandHandlerBase<CreateUserCommand, ApplicationDbContext>,
ICommandHandler<CreateUserCommand>
{
public CreateUserCommandHandler(ApplicationDbContext dbContext)
: base(dbContext)
{
}
public override void Execute(CreateUserCommand command)
{
Debug.WriteLine("CreateUserCommandHandler executed");
int id = DbContext.Users.Any() == false ? 1 : DbContext.Users.Max(x => x.Id) + 1;
User user = new User
{
Id = id,
FirstName = command.FirstName,
LastName = command.LastName,
Email = command.Email,
IsActive = false
};
DbContext.Users.Add(user);
DbContext.SaveChanges();
command.Id = user.Id;
}
}
public class CreateStaffCommand : ICommand<CreateStaffCommandContext>
{
private readonly IRepository<Staff> _repository;
public CreateStaffCommand(IRepository<Staff> repository)
{
_repository = repository;
}
public void Execute(CreateStaffCommandContext commandContext)
{
_repository.Add(new Staff(commandContext.Form.Name, commandContext.Form.Quantity));
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question