I have designed my application to have two business context.
MenuBuilder - which build a menu from the input.
MenuRenderer - which render a menu from the above model.
User first send all the items to MenuBuilder and all the information are stored as a MenuBuilder domain object, an identifier is returned to the user.
And now the user want to render the menu with different color schemes. The user send the ID back and also schema details to MenuRenderer.
MenuRenderer has no idea what the menu looks like, so it has to go MenuRepository to fetch the Menu, and build a MenuRenderer object.
This might have violated the DDD principal by having that cross domain problem. Some options that I can think of.
Option 1
The user use the id to fetch the Menu from MenuBuilder and use that to create a new request for MenuRenderer, basically copy most of the items and put it into a new struct. This is probably the best isolation but it also seems unnecessary to duplicate the items. If performance is an issue, this might not be a good idea, because now you will have to send a large object instead of an ID.
Option 2
The user pass the ID to MenuRenderer, the MenuRenderer reaches out to MenuBuilder via an interface to fetch the Menu item. The MenuRenderer then do the mapping and map all the fields back to the domain context.
However, I am no sure who should perform the mapping.
Option 2.1
Define a repository interface, GetMenu(id) MenuRenderer.Menu, the database will figure how to do the mapping. And the MenuRenderer app layer just need to call GetMenu(id) to get a MenuRenderer.Menu object and pass it down to the domain logic.
Option 2.2
The MenuRenderer AppLayer calls a repostiory that returns MenuBuilder.Menu object, and perform mapping.
Related
Im working on a eCommerce system in which I try to implement the clean architecture.
But currently Im stuck a little bit.
So I have a use case called: CreateItemUseCase in which I create a Item (alias product) for the shop.
In this use case I call a method (createItemEntity()) of a Entity called ItemEntity.
This method creates just a data object with data like:
userId
itemTitle
itemDescription
...
So now I need another method in the ItemEntity which validates the userId.
To create a Item the user needs to have a userId so the method in the ItemEntity would be called:
validateUserId()
This method should check if the user has a userId in the database and if not the Item creation would be imposible.
Now my question:
How do I validate the userId?
Should I have the validateUserId() method take a array as a parameter, In which all the User Id´s are saved... something like this:
validateUserId(toBeValidated: Int, allUserIds: Array[Int])
{
// loop through the allUserIds to see if toBeValidated is in there ...
}
Or should I query the data in the method (which Im pretty sure, would violate the dependencie rule) like this:
validateUserId(toBeValidated: Int)
{
// get all user id´s through a query, and check if toBeValidated is in there ...
}
Or should I do it completly different?
In general, entities should only contain logic that is operating on information (data) that is within the entity's scope. Knowing how to query if a user with a certain user id exists or not is not in the scope of the item entity.
I think your motivation to keep all the logic for validation together is reasonable but on the other hand you should not introduce infrastructure dependencies (like talking to the database or user repository) to the entity. Knowing how to query if a user with a certain user id exists or not is not in the scope of the item entity.
Or should I query the data in the method (which Im pretty sure, would violate the dependencie rule) like this
Exactly, that's why it's usually best trying to avoid that to keep entities free from such dependencies. Introducing such dependencies can easily get out of hand and also increase complexity for testing such entities. If you need to do that it should be a very thought decision that justifies that.
Should I have the validateUserId() method take a array as a parameter, In which all the User Id´s are saved... something like this
This is not such a bad idea in general, because you would not make the entity dependent on infrastructure and provide the entity with all the data it needs for decision making. But on the other hand now you can run into another problem: bad performance.
Now you would retrieve all user ids everytime you create an item. If you would do the check for the user's existence somewhere else this can be optimized much better.
I suggest to ask the user repository beforehand if the user exists prior to performance the entity creation including all the other potentially required validations inside item entity that make sense there. The user repository could have a query that optimizes for just checking for the existence of this user by id.
In case these two operations (asking for the user's existence and creating the new item) only happen at one place of the application I'd be pragmatic and perform the user existence check directly in the use case. If this would occur from different places in your application you can extract that logic into a separate (domain) service (e.g. item service) which deals with the repetitive flow operations working with the user repository and item entity.
What you are dealing here with is a trade-off decision between domain model purity, domain model completeness and performance considerations. In this great blog this is named the Domain-Driven Design Trilemma. I suggest going through the reasoning in the article, I'm pretty sure it will help you coming to a final decision.
I think this is one of side case of what we call Business Gerunds
Details: https://www.forbes.com/sites/forbestechcouncil/2022/05/19/10-best-practices-for-event-streaming-success/
If Item has to validate the user, just see what common attributes are there between entities and who is responsible for change of those, and then a segregation can be done in DDD representation, and using a composite via transaltion, outside world entities can exist as the same
Spring Data REST has been working exceptionally well for me, but I need to be able to restrict what parameters the user may provide when creating certain resources. For instance, the user should not be able to dictate the ID assigned to the created resource or the activation state of an account, let's say.
My approach to this so far is simply to clear or reset these fields manually in a repository #HandleBeforeCreate event handler. Is there another, more clever option for restricting the accepted POST data for a resource?
Additionally, there are cases where a CRUD call needs to specify additional, contextual attributes that are not explicitly part of the target resource but may be used in the process of creating the resource. What is the appropriate way to handle this case?
You can define a custom validator by implementing org.springframework.validation.Validator and override validate(Object object, Errors errors) and validate the input fields and then populate or make necessary changes to the fields as required to the input request object.
You can refer here for more details and also here for an example.
Is it possible to execute some C# code when checking the "Is Approved" checkbox for a Member?
Our site has a registration form which programmatically creates a user in the Members section, however the new Members must be approved by an admin and we would like to send an email to the Member when they are approved.
I think what you will need to do is look at MemberService.Saving and MemberService.Saved events and attach a custom event handler. See Determining if an entity is new for information on determining if you are dealing with a new or existing member. Below is copied from documentation:
In v6.2+ and 7.1+ you can use the extension method on any implementation of IEntity (which is nearly all models returned by the Umbraco Services):
var isNew = entity.IsNewEntity();
How it works
This is all possible because of the IRememberBeingDirty interface. Indeed the name of this interface is hilarious but it describes exactly what it does. All entities implement this interface which is extremely handy as it tracks not only the property data that has changed (because it inherits from yet another hilarious interface called ICanBeDirty) but also the property data that was changed before it was committed.
From here you should be able to check the property data you are interested in and send your email accordingly.
I am a total newb to Spring. Even though I understand the concepts of individual annotations (and dependency injection), I am having difficulty "seeing the forest for the trees." Here, in this example, I have a page that has a dropdown box. It also stores the user's selected option from that box. So there are three beans, only one of which is properly called a domain bean:
DropDownEntry *domain
SelectedOption (which could be String or a whole DropDownEntry type stored at Session Scope)
PageModel (containing a List of #1 above, and a single instance of #2)
Below is an image of my best guess as how to use Spring to:
1. Retrieve a List from the persistence layer via DAO
2. Retrieve/Store the user's selection
Is this design remotely near correct? Is there an alternate "best practices" way to architect this scenario?
I think Spring MVC Forms might be what you are looking for.
http://www.javacodegeeks.com/2013/07/spring-mvc-form-handling-vol-5-select-option-options-tags.html
Model
The model is a map of entries for the drop down box.
Controller
You can fetch these entries from a database using the DAO Pattern and turn it into a map in a controller class.
View
The drop down box is generated using a mix of HTML, a JSP tags and map of 'drop-down' entries.
Hard to tell. But from the looks of it, you've made things awfully complicated.
My advice: stop drawing UML, re-read the specification, and start coding. Start with a simple model class that represents the selectable entity. Don't name it DropDownEntry (unless you're creating software to model dropdowns), but something that actually describes the selectable entity. Don't worry about data access (DAOs) at this point.
Then create a controller class that allows you to render a view that contains the said dropdown UI element. Then pass the selectable entities (as reference data) to the view in the model. Then make the view render the selectable entities appropriately. Then allow the user to post the selection back to your controller.
Once you have this, you can think about saving the selected entity to persistent storage. At that point you will probably find out that you need to link the selected entity to a user etc.
Good luck.
Here's an example structure of the DB I have:
In the Student view form, I've added the form to add a file.
In the Student Controller, when I create or update an entry, I manage the file upload and the creation of the File database entry.
What I want to know is, in the MVC design pattern, what is the right way to do this ? Is it that my Student controller must be aware of the way my File model is done and must know how to add a file?
Or the best way to do this would be that in my Student controller, I call the add or update action of the File controller? But in that way, am I breaking the MVC ?
Thanks!
Ways you are breaking the MVC:
controller being responsible application logic and maybe even persistence (it should only be changing the sate of model layer and view)
model is not any single class, it is a layer made up from different classes with different responsibilities (there is no "file model" or ""student model")
In best case scenario, the controller would have no feedback from data, that it passed on to model layer (preferably through some service, that would be dealing with application logic withing domain model layer).
Instead the view instance, when it starts to assemble the response for the user would check up on model's state (through services again), to see if there has something changed. In case of an upload, this would be the point where view discovers the result of your upload and, based on data, decides how to respond. Usually in case of file upload the response will contain only a HTTP location header.
I am assuming that you are talking about MVC in context of web, based on your profile history. In classical MVC the view would have know about changes in model layer without explicitly checking it, because of the observer pattern which is used there.
While you most likely will have some "upload controller", it should not directly interact with domain objects or storage abstractions. Instead it just takes a user's request, extract data from it and passes it where it needs to go.
Keep in mind, that in web applications the "user" is a web browser, not the person that works with it.