Package | Last version |
---|---|
CQRSlight | |
CQRSlight.Db |
Simple abstractions of the CQRS pattern
Branch | Build status |
---|---|
master | |
dev |
AppVeyor development Nuget project feed: https://ci.appveyor.com/nuget/cqrslight-v185sxsmoqs3
Project | Dependency |
---|---|
CQRSlight | Ether.Outcomes |
CQRSlight.Db | Ether.Outcomes, DbConn.DbExecutor.Abstract |
CQRSlight is available as a NuGet packages:
To install it, run the following command in the Package Manager Console:
Install-Package CQRSlight
Install-Package CQRSlight.Db
Contains multiple main components:
IChecker
IQuery
ICommand
ICommandWithResult
and it's async versions:
ICheckerAsync
IQueryAsync
ICommandAsync
ICommandWithResultAsync
- Install the package
CQRSlight
to your project - Create your some
Query
:
public class BlockedUserQuery : IQuery<List<User>>
{
public List<User> Get()
{
// ... return users that was blocked
}
}
- Create your some
Command
:
public class CreateUserCommand : ICommand<CreateUserCommandRequest>
{
private readonly PasswordHasher _passwordHasher = new PasswordHasher();
public IOutcome Execute(CreateUserCommandRequest commandRequest)
{
var email = commandRequest.Email;
var password = commandRequest.Password;
var passwordHash = _passwordHasher.GetHash(password);
try
{
// ... some logic for create user
return Outcomes.Success();
}
catch(Exception ex)
{
return Outcomes.Failure().FromException(ex);
}
}
}
- For your commands you may needs an
Checker
- some component, that must contains validation logic:
public class CreatingUserEmailChecker : IChecker<User>
{
public IOutcome Check(User creatingUser)
{
if(string.isNullOrWhiteSpace(creatingUser.Email))
return Outcomes.Failure().WithMessage($"Email is required.")
// ... Some other validation logic if you need
return Outcomes.Success();
}
}
Contains components:
DbChecker
DbQuery
DbCommand
DbCommandWithResult
and it's async versions:
DbCheckerAsync
DbQueryAsync
DbCommandAsync
DbCommandWithResultAsync
- Install the package
CQRSlight.Db
to your project - Create your
Query
without any input parameters:
public class BlockedUserQuery : DbQuery<List<User>>
{
public BlockedUserQuery(IDbExecutor dbExecutor) : base(dbExecutor)
{}
public override List<User> Get()
{
var sql = $@"
select u.*
from dbo.User u
where u.IsBlocked = 1
";
var blockedUsers = dbExecutor.Query<User>(sql).ToList();
return blockedUsers;
}
}
Or create your Query
with some input parameters:
public class UserByEmailQuery : DbQuery<string, User>
{
public UserByEmailQuery(IDbExecutor dbExecutor) : base(dbExecutor)
{}
public override User Get(string email)
{
var sql = $@"
select u.*
from dbo.User u
where u.Email = '{email}'
";
var user = dbExecutor.Query<User>(sql).FirstOrDefault();
return user;
}
}
- Also you can create some
Command
:
public class ResetUserPasswordCommand : DbCommand<int>
{
public ResetUserPasswordCommand(IDbExecutor dbExecutor) : base(dbExecutor)
{}
public override IOutcome Execute(int userId)
{
try
{
var userByIdQuery = new UserByIdQuery(DbExecutor);
var user = userByIdQuery.Get(userId);
if(user != null)
{
// ... Reset user password
var sql = $@"exec dbo.ResetPassword @userId = {userId}";
DbExecutor.Execute(sql);
// ... Send email
return Outcomes.Success();
}
return Outcomes.Failure().WithMessage("User not found");
}
catch(Exception ex)
{
return Outcomes.Failure().FromException(ex);
}
}
}
Checkers (classes that implements IChecker
interface or abstract class DbChecker
)
must be usefull (also as Query) inside of Commands. For example:
public class AddNewAccountToUserCommand : DbCommand<Account>
{
public AddNewAccountToUserCommand(IDbExecutor dbExecutor) : base(dbExecutor)
{}
public override IOutcome Execute(Account userAccount)
{
var accountChecker = new UserAccountChecker(DbExecutor);
var checkAccountResult = accountChecker.Check(userAccount);
var accountIsValid = checkAccountResult.Success();
if(!accountIsValid)
return checkAccountResult;
try
{
// ... Creating new account for user
var sql = $@"
insert into dbo.UserAccount ...
";
DbExecutor.Execute(sql);
DbExecutor.Commit(); // If DbExecutor was created as transactional
return Outcomes.Success();
}
catch(Exception ex)
{
DbExecutor.Rollback();
return Outcomes.Failure().FromException(ex);
}
}
}