When do domain event handlers come into play? - c#

I have a simple examle domain of two aggregate roots and one regular entity.
Tenant, UserGroup and User where in this particular sample the Tenant and User make up for the two AggregateRoots.
When a command is received from the UI/Service layer it reaches the command handler which manipulates the write only domain.
You could say that User isn't supposed to be an AggregateRoot at all but since it will be referenced by others, it cannot be a regular entity. (yes?)
These two AggregateRoots need to communicate. A User cannot be created without belonging to a UserGroup, which is an entity in the bounded context of Tenant. Presumably, we can create, since it is a simple constraint, a User through the constructor. User.Create(TenantId, UserGroupId)
It generates a DomainEvent with Date, AggregateVersion and AggregateId (of the user). Now we get to the blurry parts.
Open committing this event into the store, this event is broadcasted onto the bus (memory, whatever). It this the point where domain's event handlers, similar to command handlers, catch the user created and notify/manipulate the Tenant's UserGroup to add the UserId?
Are my thoughts about solving this going into the entirely wrong direction?

A Saga might be what you are looking for.
Simply put: A saga can be implemented as an event handler that listens for specific events and issues commands to different aggregate roots, or even across context boundaries.
In your case it might look like this:
public class RegisterUserSaga : Handles<UserCreated>
{
public void Handle<UserCreated>(UserCreated evnt) {
var tenantId = // you probably know how to find this
var groupId = // same here
var command = new RegisterUserForTenant(evnt.UserId, tenantId, groupId);
Bus.Send(command);
}
}
Read more about sagas in this article by Rinat Abdullin or watch "CQRS, race conditions, and sagas - oh my!" by Udi Dahan
Update:
After our extended discussion in the comments I'll try to show how this could work from a different angle (pseudo code ahead). This hopefully sheds some more light on a possible solution:
// Aggregates:
Tenant
Guid TenantId
List<Guid> UserGroups
UserGroup
Guid UserGroupId
List<Guid> Users
User
Guid UserId
Some more details
// Commands:
RequestRegistration(userId, userGroupId, user details)
CreateUser(userId, user details)
AddUserToGroup(userId, userGroupId)
// The initial command would be:
RequestRegistration (leading to a RegistrationRequested event)
// The Saga handles the RegistrationRequested and all subsequent events
UserRegistrationSaga
Handle(RegistrationRequested)
-> send CreateUser command (which eventually leads to a UserCreated event)
Handle(UserCreated)
-> send AddUserToGroup command (-> UserAddedToGroup event)
Handle(UserAddedToGroup)
-> Done

Related

CQRS for streamed data

I am using CQRS segregation and works very well for transactional commands or request-response from one node to remote node.
I have a use case where a command will be issued to a remote node, and this will result in a "stream" data (much like a remote command running, and the server giving us updates in text as it progresses):
// this is sent from requesting node to remote node to initiate the stream
public class LongRunningCommand: ICommand
{
Guid Session { get; set; } // the session ID to use
string CommandLine {get; set; } // the command the remote note will run
}
This data is then sent in a number of packets over a period of time from the remote node to the requesting node:
// this is sent from remote node to requestor in multiple updates over time
public class UpdateProgress: ICommand
{
Guid Session { get; set; } // possibility to multiplex sessions
int Sequence { get; set; } // de-dupe/resequencing out of order packets (lower QOS)
byte[] Payload { get; set; } // the data to be passed to the application
}
This is not really a command, nor is it a request-reply (as there are multiple replies) - it is a long running session of sorts, but I am not sure how this fits in with CQRS.
What would be the best way to order this? Could my requesting node have a Command Handler like the below (where UpdateProgress is the "command" being handled):
public class UpdateProgressCommandHandler : ICommandHandler<UpdateProgress>
{
public async Task HandleAsync(UpdateProgress message)
{
// resequence in handler or chained infrastructure - omitted for brevity
var window = GetWindowForSession(message.Session);
var updateFromServer = System.Text.Encoding.UTF8.GetString(message.Payload);
await window.WriteLine(updateFromServer);
}
}
The above works (and i think fairly well), but terminology seems a bit funky (the command name UpdateProgress is more of an event than a command).
Or am I better off dropping the notion of commands/queries all together, and go full event bus, and if I did, how would i handle the initial request as that is not an event its more of a command (which wouldn't make sense semantically on an event bus that deals with events - not commands or queries).
Or am i getting caught up in naming convention only? Since first time doing this, appreciate a best-practice view for the above use case.
I'm not sure if I understood you correctly, a Command where it needs to talk to a remote node does not appear to be part of your Domain. This is not say it is not a Command, you can still define that as Command but not within your Domain IMO. You could potentially look into integration events here.
Without having a full understanding of your domain, here is how you could define your process:
Execute Command to modify your domain (something like Status: Pending)
Raise an Integration Event from your CommandHandler into a separate worker/ Service Bus
The separate worker completes the process and then raises another integration event
Your worker subscribes to that event and updates the relevant pieces of your domain (eg. Status: Completed).

How to raise domain Event When I don't want to share actual domain model

I'm trying to implement DDD in my small project but Not able to understand how to raise domain event in below case.
Account Domain
public class Account : BaseEntity
{
public string PhoneNumber { get; set; }
public int OTP { get; set; }
public Account()
{
}
public Account(string phoneNumber, short otp)
{
this.PhoneNumber = phoneNumber;
this.OTP = otp;
CreatedDate = DateTime.Now;
RowKey = Guid.NewGuid().ToString();
PartitionKey = phoneNumber;
}
}
Account Service
public async Task<bool> GenerateOTP(string phoneNumber)
{
if (phoneNumber.Length != 10)
throw new ArgumentException(ApplicationConstraint.InvalidNumber);
var otp = Convert.ToInt16(new Random().Next(1000, 9999));
var account = new Account(phoneNumber, otp);
await this.accountRepository.AddEntity(account);
return true;
}
Account Repository Azure Storage table is my database
public virtual async Task AddEntity(TEntity entity)
{
TableOperation insertOperation = TableOperation.Insert(entity);
await table.ExecuteAsync(insertOperation);
}
I want to raise domain event only when data get saved in the database. For a workaround, I'm calling messaging service from account service.
Given the limited information provided, one option would be to create an AccountCreated event, (or an EntityCreated event if this is a cross-cutting concern) and publish it through some bus where consumers can asynchronousle receive it and do any subsequent processing needed.
The event need not use domain entities, and it can contain the information/data necessary to do any subsequent processing without the need to access a shared db (and as such adhering to DDD & microservice guidelines).
----Edit----
In the above I assumed that this is an established system and Azure storage isn't something that can change. Publishing an event, and handling it is pretty simple, but there are some things you need to be aware of. In general, you have 3 options here:
Publishing right after saving isn't wrong. It's simple way to do it, and (if you adopt an event-first methodology) you can do it in a generic way across your entities, minimal work. However, you need to be concious of how to deal with errors. Specifically, the issue is that if you store the entity first, before publishing the event, and then the process crashes for whatever reason, the event may be missed, so later workflows will not kick-off. If you do the reverse (publish then store), you run the risk of double-publishing the event. In this case you have two options:
If you store-then-publish: just accept the (really rare) possiblity of not publishing an event. This is something you need to speak to the business, and you can minigate the severity by logging the event before trying to save the entity.
If you publish-then-store: (you'll need to do this if the cost of fixing any issues ad-hoc are too great) you can fix the problem by having your consumers check the id of the incoming message if they ever have processed it before and reject it if they did OR make the process idempotent (if possible), meaning that doing the process twice isn't a problem
Using event sourcing. This isn't difficult in my opinion, but obviously it's an overhead if this is a a simple application, and while not difficult, it does need a significant amount of reading up if you're not familiar with it. If this is a non-trivial application, event sourcing can help a lot, because observers can just observe the events in the buffer and respond to that (so not need to explicitly publish the changes).
Append the event in a separate table within the same transaction where you're storing the entity, and use the outbox pattern implementation (publish those events from a separate service, marking them as published once they've been published). Honestly, the pattern shown on that is a bit simplistic, and there are a lot of tricky and small complexities, so prefer to use an existing one if you can find.
Honestly, if you can get away with 1.1, do that. It's simple and problems only very rarely appear. Just log the operation before you do it so that you can manually do it in the rare case of issues.

How do I handle claim check with cqrs

TL;DR:
1. Am I creating an anti-pattern?
2. What is the best way to handle a claim check with CQRS?
I have several entry points in my system (webapi passing in json and xml), as well as through the file system with fixed-length files.
I am using Rebus with MSMQ and Sql server to manage my messaging. The data can be larger than 4mb (MSMQ's max message size if I believe). When the system receives a file I convert it into a stream and create a command that implements IAttachmentCommand as below:
public interface IAttachmentCommand : ICommand
{
Stream Attachment { get; }
IClaimCheckCommand ToClaimCheck(string attachmentId);
}
public interface IClaimCheckCommand : ICommand
{
string AttachmentId { get; }
}
I then send it using a command bus (using Rebus). If the command is of type IAttachmentCommand I create an attachment in the rebus databus table and return a new IAttachmentCommand using ToClaimCheck on the original command. The AttachmentCommand is effectively a carbon copy of the original command, except it now has the attachmentId instead of the data.
I will then call send in my Rebus bus with my new AttachmentId as below:
public void Send<TCommand>(TCommand command) where TCommand : ICommand
{
if (command is IAttachmentCommand)
{
var cmd = command as IAttachmentCommand;
var task = CreateAttachment(cmd); // method excluded, but persists to Rebus DataBus and returns AttachmentId
var claimCheck = task.Result;
_activator.Bus.Send(claimCheck);
}
else
{
_activator.Bus.Send(command);
}
}
This seems to be working, although I am happy to have my code pulled to shreds. I can send commands, apply the events that are generated by my aggregate roots, persist to the event store etc etc.
I simply pick up a file from a webapi call or the file system, create a command and send it off with my command bus.
In a separate windows service I have a command dispatcher monitoring MSMQ for these messages. When a message comes in it will then iterate through however many CommandValidationHandlers there are to validate the command. CommandValidationHandlers implement the following:
public interface ICommandValidationHandler<in TCommand> where TCommand : ICommand
{
ValidationResult Validate(TCommand command);
}
ValidationResult effectively returns a collection of errors. These errors are logged, published as an InvalidCommand event that contains the Command info and the errors - this then allows me to have any subscribers that are listening pick up the event - send a mail or call a web service etc to say that the message failed, with the reasons. If the command is invalid an exception is then thrown and the process stops.
My concern is that on validation I have the attachmentId, and have to retrieve the file, which is then validated, for example against an xsd.
From there I need to deserialize it to an object (generally a collection of financial transactions with a header which contains meta data such as no of transactions etc) and perform extra validation on data in the object.
Once this validation is complete I need to iterate through the collection of transactions in the object and send these to their relevant bounded contexts using the command bus, and further processing takes place.
It seems in this instance that I will be hitting the claim store a number of times - once for each validation handler (although I guess this could be resolved with a composite collection of validators), but then again in the Command Handler once validation has taken place.
In the various Event Handlers I have that need access to all the data I need to retrieve the data from the claim store each time and deserialize a number of times.
This seems like code-smell to me. Should I consider caching the file the first time I retrieve it and clear it from cache once all event handlers have finished their work?
Does anybody have better suggestions?
From what I understand about your problem the question is really: "should I use a caching mechanism for reading the claim store on the validation handlers?"
In your case, because the data in the claim store is immutable, you could cache it as long as you need it. That is the beauty of the immutable data: is forever cacheable.
To implement the caching mechanism you could use the decorator pattern over the claim store and switch to the cached version in your composition root in the dependency container. In this way you can anytime switch back to the uncached one.
You could cache it even more, you could cache even the result of the validation if the validated data does not ever change and it is repeated over time.

Webhooks per entity

I'm using the ASP.NET Webhooks packages to allow users to receive callbacks when certain events occur in my application.
e.g. entityUpdated, entityCreated, entityDeleted
I would like to expose the possibility to users of registering Webhooks only for updates on specific entities in case they are only interested in receiving callbacks for those specific entities.
e.g. entityUpdated for entity1
The filters seem like a good candidate for implementing this behavior. Users can subscribe to events using filters.
e.g. entity* (to receive all event concerning entities)
So I was thinking of exposing events per entity like: entity_1_Updated.
That would mean the list of exposed event will change during the runtime of the application (as entities get created or deleted).
More concrete, the implementation of IWebHookFilterProvider would perform a database query to fetch the list of entities for wich events can occur.
Like so:
class EntityWebHookFilterProvider : IWebHookFilterProvider
{
public async Task<Collection<WebHookFilter>> GetFiltersAsync()
{
List<int> ids = await repository.GetAllUpdatableEntitiesAsync();
return new Collection<WebHookFilter>(ids.Select(id => new WebHookFilter { Name = string.Format("entity_{0}_Updated", id)}).ToList());
}
}
Would this be a good solution? Or should the list of events/filters be fixed?
An easier way may be to use a separate field in the registration to indicate the specific ID the subscriber is interested in using the Properties part of the WebHook registration.
Then when you send a notification on the server side you can use the overload which takes a Func enabling you to filter that WebHooks only are generated when the ID matches that of the WebHook registration, for example:
// Create an event with action 'event1' and additional data
await this.NotifyAsync("event1", new { P1 = "p1" }, (w, s) =>
{
// Check that the property included in the event data matches that
// of the WebHook registration.
return true;
});
Hope this helps,
Henrik

MVVMLight : is this the right way to use the Messenger?

I have a classic business application that manages clients and adresses.
There are tab items (Id, GenericInfo and a few more) with each their own ViewModel.
There is a MainViewModel that handles the save and load commands of a client and its addresses.
We retrieve the data from a WCF service. The data received/sent from each WCF Function is aggregated in a different container.
In my MainViewModel I create a SaveContainer and then send it with the messenger.
public void Save()
{
var container = new SaveContainer();
MessengerInstance.Send(container);
//the container is now populated and ready to be sent via WCF
Console.WriteLine(container.User.Name);
Console.WriteLine(container.Address.StreetName);
Console.WriteLine(container.Address2.StreetName);
}
In my UserViewModel is register for that container and then the viewmodel populate it with the data it has (the user).
public UserViewModel()
: base(Messenger.Default)
{
User = new User();
MessengerInstance.Register<SaveContainer>(this, (x) => x.User = User);
}
And in my AddressViewModel I do the same.
public AddressViewModel()
: base(Messenger.Default)
{
Address = new Address();
Address2 = new Address() { StreetName = "Washington Street" };
MessengerInstance.Register<SaveContainer>(this, x =>
{
x.Address = Address;
x.Address2 = Address2;
});
}
I'd do the same when I have to load data.
After I send the Message, I assume that every ViewModel registered received the message and handled it. Am I assuming wrong? Do you find this way a correct way to use the Messenger? What would you improve?
There is no right way to use the messenger. However, you will have to consider that the message is handled by all recipients that have registerd for the message, not just an intended subset. Furthermore, when using messaging you do not have control over when the message handling is finished, now do you get notified when all recipients are done handling the message. In addition - depending on the implementation of the messenger - the messages may be handled in parallel.
So the problem with your approach (and #cadrell0's extension using a callback) is that you don't know when all recipients have handled the message. Using the callback you will get a callback for each recipient handling the message (i.e. n recipients n callbacks).
So how can you check when all recipients are done handling the message?
You use a counter to determine how many recipients have called back - this is error prone as you might register another message recipient and this messes up your system.
Another way would be validating the save container and once it is complete continue processing - but this might lead to a race condition as you may think all recipients have handled the message and continue, but then one late recipient calls in and invalidates your save container ... not good.
As I see it the messaging is more designed as a notification mechanism, i.e. you notify some recipients that something has happened. If you know and can ensure that there is only one recipient you even can use it in a manner you describe, but as soon as more than one recipient is involved this causes the mentioned problems.
So where does this leave you ... in your szenario I would tend to design the viewmodels as "related" (i.e. the main view model knows about the user view model and the address view models - or the main view model knows about the user view model that in turn knows about the address view models if that is more appropriate). Usually, I also would desing a model that holds the unit of work that I have to deal with (in your case the SaveContainer). Then all view models are constructed from this model and write their data to it. In normal cases this unit of work is what you get from your data storage service and what, in turn, gets saved by the data store in a single transaction.
But again, there is no right way to MVVM!
If I need to do something after a recipient responds to a message I include a callback on my message. When the recipient is done, it executes the callback. Adding parameters to the callback allows the recipient to send data to the sender. This also allows the recipient to perform an async operation.

Categories

Resources