Let's assume there are two domain entities:
UserImages with methods addNewImage(), removeImage($imageId), getImages($from, $count).
UserProfile with fields name, age, mainImageId, etc.
Following functionality is desired inside the domain: when application layer calls UserImages -> addNewImage(), UserProfile -> mainImageId is set automatically in case it was empty.
So, what is the best way and best place to implement an in-domain over-entity business logic? Domain events with observing services, referencing special services from the entities, or somewhat else?
I create all the entities using a some kind of factory, i.e.
$userImages = Domain::userImages($userId); // getting an instance of UserImages
$newImageId = $userImages -> addNewImage(); // adding a new image
I also should mention that I will have a lot of logic like described above in my project.
Thank you very much for help!
First off, as specified in your other question, UserImages is not itself an entity. Instead, there is likely an entity called UserImage which refers to a user's image, singular. A UserImageRepository can then provide access to all images associated with a user, using pagination where necessary.
Now, the use case is:
When a new user image is added, if a user's profile image is not set,
set it to the added image.
The term when indicates a domain event. In this case, the event is UserImageAdded. The bold if and the subsequent set represent the desired behavior in response to the event.
To implement this use case, an event must be published when a user image is added and a handler implementing the desired behavior must be subscribed to this event. This linked domain events article provides a sample implementation in C#, however the concept can be easily ported to PHP. Overall, the required components are:
An application service which handles the adding of user images. This service will publish the event.
A domain event publisher which is called by the application service and allows decoupling of publishers from subscribers.
A domain event handler which handles the UserImageAdded event and invokes the desired behavior.
User sounds like Aggregate for me (it's difficult to give you an opinion without context :) ), but it can solve your problem.
If Profil and Images are differents bounded context, may be you can use Domain events (it's helpful for cross bounded context communication)
Related
I am currently checking out the DDD pattern and (tried) to refactor a project of mine accordingly.
Now I got a case where I don't really know what to do: Some business logic can be called by multiple entities. In my case it's possible to mark items as hidden.
The item can be a Product or a Category (or many more).
The related event is called MarkedItemAsHidden, fired by an action handler called MarkItemAsHidden, which accepts the item as first parameter and fires the event based on the item's class.
So I got:
Events: MarkedItemAsHidden
Actions: MarkItemAsHidden($item)
Projector: HidingProjector
But where do I put those classes?
My app has a Domain-directory, which includes:
Product
Category
Cart
...
but I can't really decide where to put these "shared classes".
Some of what you are describing lies outside the boundaries of your Domain.
Your Domain starts from the Application Services. Application Service lie on the boundary of your Domain. The job of the Application Service is to translate external messages into Domain Actions.
For Example: Assuming you have a complete application with UI and Domain. You want to mark an "item" as Hidden.
So you:
Trigger from your "Action Handler" that might be in the UI (a button
that's clicked as an example.). This is outside of the Domain. Your
click generates an event that is handled by the
ApplicationService(ItemAppService). How exactly the event gets to the Application Service is an implementation detail (it's not a DDD concern). For Example: ItemAppService could be a Controller class in a Rest API App. In this example, your event is a HTTP Request and the Implementation Detail would be something like the ASP.net core API framework that routes that HTTP call to your Controller.
ItemAppService.Hide(ID: String) is the method that's ultimately triggered. ItemAppService is in the Domain and Using Domain
Concepts like Factories and Repositories, it can retrieve an instance of "Item".
After retrieving the "Item", it'll then call a DomainService (or the
appropriate Domain Class based on your Ubiquitous Language) that
knows how to hide an item.
It then returns some sort of a response to the caller on the
results.
ItemAppService doesn't care how its hide function is called. You can't pass hide a Domain Entity because that'll mean that the consumer of the Domain action knows what and how to get an Entity.
If your intention is to use Events and Triggers as part of your Domain Model, those concepts must be translated into the Ubiquitous Language. When you do something like "generate an event" inside your Domain, your Domain doesn't care how you "generate an event" so that logic or processing is usually left to something outside of the domain (that's injected in through IOC) that can contain the specific knowledge of how to "generate an event".
I am trying to understand how to implement this in Event sourcing model / DDD.
Assume a distributed application in which user submits an application for something, say Job/Loan. So the application raises an UserApplied Event.
There are few micro services like credit service, criminal record service.. they consume this UserApplied event do some validation, responds with CriminalCheckPassed, CreditCheckPassed ... etc. Assume there are 5 checks to be done. In future we might also add more checks like this.
The app consume these events and take some decision. That is - only if they are all validated successfully app can approve the user application by changing the status to UserApproved. Any of the validations failed, them it would be UserDeclined. Something like that.
It sounds simple. But I am banging my head how to implement that correctly?
This is my event store
I have a materialized view
If we have to update the materialized view/aggregate whenever we receive an event, app needs 5 different events to take decision. Till then it will be pending. Even when I receive the 5th event, the materialized view does not know how many events it has received before. I will end up querying entire event-store.
Another approach is - adding these columns in the materialized view. So that we know if we have received all these events. It will work. But looks super ugly.
My question is - how to use the aggregation properly in this case?
If I understand correctly, the validation is a part of domain logic (it is something that has to be made sure that it passes). Here, there are some external services like Credit Service and Criminal Record Service.
First, I would model User as an Entity and an Aggregate Root of itself. Then, I will model Job Application as another Entity and another Aggregate Root of itself. Now there are 2 aggregates, with the relationship: User can have many Job Applications.
Now, you need to validate some things before you create a Job Application instance. This validation requires some knowledge from other services. This can be solved by creating a domain service, say JobApplicationCreationService which sole responsibility is to create new instance of Job Application. Then, you would want to inject those external services here. Inside the service, do the validation using the services you injected, then if all validations pass, return a new Job Application instance. This Aggregate instance will have fulfilled your validation rules/domain logic.
Events here is not suitable for validation, rather it is used to synchronize states between Aggregates using eventual consistency. When Events are published and being processed, you want to make sure that the Aggregate that produces the events is already in a consistent state (in this case, the Job Application aggregate).
Here is my personal rule of thumb: Try to create an Aggregate from static factory method to contain the creation logic. If the creation requires something outside of the boundary of the Aggregate itself, refactor it to a Domain Service.
Well, if CriminalCheckPassed are domain events, then they need to somehow mutate the domains state, so you need to store it within your domain (which will be restored when you load your domain entity), say a private readonly List<RequiredCheck> RequiredChecks and check these on recieving of any of the responsible events, then decide.
If it's not a domain event and is not persisted with the aggregate root, then have a process manager (aka Saga) (i.e. UserApprovalProcessmaanger) collect these external events and process/persist them and once all of them are collected fire off an UserApproved / UserDeclined event which is processed by the domain model/aggregate root
I have product service, category service, promotions service, search service.
When User want to add product. CreateProductRequest come to product service. Request includes product data and datas of other services like categoryId,uncalculated price , too. After product is added. I need to send other servie datas. Category service needs productId and CategoryId. Promotions service needs productId and price.
After creat eproduct transaction commited;
1) I put all data in ProductCreatedEvent that includes saved productId, categoryId, uncalculated price etc. Every service get what it needs from event and save to own db. I publish event with RabbitMQ
2) Send via seperated commands to services.I send commands with RabbitMQ
And What If there is no category that id come with event and Category services didn't save. But Product saved at product Services ?
or what do you suggest ?
To answer the question, it's important to keep in mind the difference between a command and event. A command is a request to do something. An event is a record of something that has happened. One key difference is that a command can be rejected.
When looking at your use case, publishing events to other services makes the most sense. The product has been created and you are notifying the other bounded contexts that care about the change. If you issue a command, you are telling other bounded context to make a change that may or may not fail.
That said, you each bounded context may receive the event and produce a command within their own context to update aggregates managed within. As such, the difference is subtle between these two:
- Issue a command to each bounded context
- Issue an event to each bounded context and they can then trigger a command as needed
But given the above, the notification of the creation of the product should not fail. It has happened already. From there, each context can decide what to do about it.
I have a question about the integration events used in a microservice / CQRS architecture.
The payload of the event can only have references to aggregates or can it have more information?
If only reference ids can be sent, the only viable solution is to bring the rest of the information with some type of call but the origin would have to implement an endpoint and the services would end up more coupled.
ex. when a user is created and the event is raised.
UserCreated {
userId
name
lastname
document
...
}
Is this correct?
If only reference ids can be sent,
Why would only that be allowed? I have worked with a system which was using micro-services, CQRS and DDD(similar like yours) and we did not have such restrictions. Like in most cases it is: "What works best for your application/business domain". Do not follow any rule blindly. This is perfectly fine to put other information in the events Payload as well.
the only viable solution is to bring the rest of the information with
some type of call but the origin would have to implement an endpoint
and the services would end up more coupled.
This is fine in some cases as well but this brings you to the situation to have additional call's after the event has been processed. I would not do this unless you have a really heavy model/models and it would affect your performance. For example if you have an event executed and based on userId you would need to load a collection of related objects/models for some reason. I had one similar case where I had to load a collection of other objects based on some action on user like event UserCreated. Of course in this case you don't want to send all that data in one Event payload. Instead you send only the id of the user and later call a Get api from the other service to get and save that data to your micro-service.
UserCreated
{
userId
name
lastname
document
... }
Is this correct?
Yes this is fine :)
What you could do instead:
Depending of your business scenario you could publish the information with multiple events with Stages and in different States.
Lets say from UI you have some Wizard-like screen with multiple steps of creation. You could publish
event: UserCreatedDraft with some initial data from 1st Wizard page
event: UserPersonalDataCreated with only part of the object related to private data
event: UserPaymentDataCreated with only the payment data created
UserCreatedFinal with the last step
Of this is just an example for some specific scenario which depends on your use case and your Business requirements. This is just to give you an Idea what you could do in some cases.
Summary:
As you can see there are multiple ways how you can work with these kind of systems. Keep in mind that following the rules is good but in some cases you need to do what is the best based on your business scenario and what works for some application might not be the best solution for your. Do what is most efficient for your system. Working with micro-services we need to deal with latency and async operations anyways so saving some performance on other parts of the system is always good.
We have an application wich has a typical Master/Detail view. The master view displays a list of ticket titles and if you click on a ticket title you see a view with the ticket details.
The master view has a model wich is a list of "ticket" objects each containing more information about a specific ticket. We bound a handler on click which basically instantiates a new detail view and displays it. This works fine, except that we have to give all the config values as primitive data types to the constructor (we know we can't use objects when instantiating)
We thought about another way to do this but couldn't get it working. In the Init() (of the detail view model) we only pass the ID (of the clicked ticket) and wanted to use Mvx.Resolve to get the master view model. So question one would be, can you fetch arbitrary view models inside other view models?
A second idea would be write a base view model class from which all view models in our app inherit, which registers the view model on instantiation to a service so that you can call that service and fetch the view model from anywhere (mediator pattern). Would that be against the framework?
An answer that I have found is "use messages" but to me it seems sort of bulky, at least how I understood this. The detail view model would have to send a message to the master view model "Ok I'm ready now" and then the master view model would say "Alright, heres the configuration".
So to sum up the questions:
Is it possible to fetch certain view models from the framework inside other view models?
Would implementing this by hand be against the framework?
Did we understand the message approach correctly and if no, what would be a more lightweight/generic way of doing it?
Cheers and thanks
Tom
Usually when I have to share data across View Models, I use a Service to manage it. The service is typically injected via IoC.
I treat View Models as a mediator between the Services and the View. Most of my logic is stored in the Service. The only things I have in the View Model are properties for data binding, commands that dispatch to a service, and other presentation level concerns.
For your scenario, I would have the MasterViewModel pass an identifier to the DetailViewModel, where it will ask the ConfigurationService for the configuration by id.
Hope this helps.