Passing list of exceptions Back to Caller - Domain Driven Design - domain-driven-design

I am new to DDD but I am trying to implement it in my Project - I have a service which is setup following the DDD principles - Application / Model / Repository - The Clients of the Service want to get back a DTO class (which also contains a Error Collection as one of its members) . Questions is how do I populate the Error Collection of the result DTO. Can the Error DTO be passed from the Application/Service Layer to Model/Service layer and populated there – Can someone point me to some example of these kinds of scenarios Currently I am bubbling up all the errors that I am getting back to the Application Service and populating it there like I said I am struggling.

As a general rule try not to copy code (classes, methods, interfaces). If you really have to use DTOs create them as late as possible in the process so that if you remove them you should still be able to use the system in another way.
I would have something like this:
Model
Domain classes
Error class
Model/Service (has reference to Model)
Application/Service (has reference to Model and Model/Service)
Domain DTOs
Error DTO
Also a question do you really need two Service Layers? Avoid Anemic Domain Model

Related

Where to place scraping logic in Nestjs applying DDD?

I have a nestjs application using DDD.
I have following folders:
Domain
Application
Infraestructure
I have the use case of scraping information from youtube channels. DDD is clear to me if I end up saving things in the database. But if I am only scraping? once the scraping is finished, then I would have the use case of saving in the database
Application
use-cases
scrapChannels.useCase.ts -> instanciates the youtubeLogsRepository interface in the constructor
Domain
entities
ports
repositories
youtubeLogs.repository.ts -> interface with getYoutubeLogs() method
Infraestructure
adapters
presentation / http / controllers
repositories
youtube-logs
youtubeLogsScraper.repository.ts -> here the logic to scrap
I placed the logic to scrap in infraestructure, but not sure if it should be in the domain. Also not sure if it should be under repository, since it's not touching the db. If not, then where it should go?

How to utilize the Unit of Work pattern with repositories in onion architecture that query external APIs?

There are many excellent resources about the Unit of Work pattern. My understanding is that it's main purpose is to provide a way to ensure that the effects of a piece of code will not persist if an error occurs. There are plenty of examples of this usage for databases in most languages.
There are very few resources I can find about using such patterns to query and use external APIs while maintaining some level of data integrity during an error. Generally repositories are about data persistence but a lot of API's do concern such things especially in a microservice architecture. Clean Architecture: Where to make API calls suggests that such a microservice architecture should abstract calls to other microservices using a repository, and there are many public APIs that can be thought of in a similar manner.
In my specific case, I am looking to plug in the Todoist API for Task items into my application which works with its own version of a Todo entity. I have successfully adapted my TodoRepository for the Todoist API and can see my tasks from Todoist displayed in my UI - I now face the issue that if a call fails then I could be adding, deleting or updating a Task in the Todoist API when an error occurs after the call, which is not ideal for data integrity reasons.
There seems to be some distinction between an API that can act as a repository and one that cannot. Seemingly, if the API is able to perform general CRUD on a similar entity in the modelled entity then it may be a good candidate for a repository adapter, but if it were something like retrieving the weather forecast, determining if a name is the same as some celebrity, working with the google maps API (if your application wasn't a map itself), etc, then these are handled differently.
Under the assumption that I have not yet confirmed that all API adapters/facades will be implemented in the Infrastructure layer of a project, what context does the interface that defines the API usage exist? If I want to query to see if a name is also a celebrity name, would I have an Application or Domain service interface that looks something like
public interface CelebrityService {
Celebrity LinkNameToCelebrity(string first_name, string last_name);
}
Where Celebrity is a Domain entity. This feels out of place if the Celebrity entity has been made only for this call.
Similarly for a weather API,
public interface WeatherService {
Weather GetWeatherForDay(datetime day);
}

Messages Dictionary

We have a conflict in our programming teams when an error raising on the database.
When db facing with error like this:
“Msg 547, Level 16, State 0, Line 1
The DELETE statement conflicted with the REFERENCE constraint ... “.
Ui must show understandable messages, like this:
“You cannot Delete this Item. It Used.”
Database team(MS SQL) return pure error message by “raise error” and expects back-end team(Node js) or front-end team(Angular) convert this message to user-understandable messages and show to user. But the back-end team and front-end team say it is not optimized, and db messages must be converted to user-understandable messages in the database.
Are there any standards for this problem?
The responsibility for the content of error messages should not be in the database. For example, the same database might be used from a variety of different applications, with users in different cultures: the database will have no idea what localisation is in place on the particular user interface being used.
Even if your data layer returns a particular error, in different scenarios, this can mean different things. So it is common practice to build a "business" layer on top of the "data" layer. That business layer can make the decision about whether it is a problem or not. This business layer can be re-used by multiple different "user interface" layers (e.g. Web app, Windows app, phone app, etc), or even the business layer of other applications.
But the business layer should only return an indication of what the problem was (including any details which may be relevant). Ultimately, the user interface must be responsible for constructing the error message in a correctly localised way. Most UI layers have a way of storing localised resources for this purpose.

Which directory to put object constructors in node / express

I want to create an object constructor for a new sign up within express. Where should this be located? Within the model? The controller?
It's really up to you to decide on your directory structure. Express doesn't enforce any particular structure.
That said, an object constructor clearly seems to fall into the model category. The model should include any data structures and logic for interacting with the database. The view is just what the user sees and interacts with, and the controller mediates between the view and the model (express routes would fit into this category).
For a short example using express with the sort of directory structure you seem to have, see this. Although the example uses a mongoose schema, the same principle should apply if you are using an ordinary object constructor. You might also want to look at some of the examples in the Express repository, including a different implementation of an mvc pattern.

Creating Database class Library using NHibernate

I am writing an Class Library as DataModel. DataModel capable of handling all the Database related task. I am using NHibernate and Fluent NHibernate for the same.
Now the question arises are as follows :
Should we expose the Entity (POCO Class).
Is it good to have a Entity with internal protected property and property exposed as a interface.
Entity created for mapping can be a Model for WPF MVVM.
Or should we directly bind entity ?.
There is no control if Library returns a List of entity as API return. So anybody can do add or delete in list. How should I keep control on it. Should I create proxy derived from IList which will keep track of it.
Is it right to throw Exception occurring in an API or should I return null?.
Is it good keep logging in the Library ?.
Should we expose the Entity (POCO Class).
Yes, Creating wrapper class makes more effort.
Is it good to have a Entity with internal protected property and property exposed as a interface.
Yes, Setter and non-exposed properties are control.
Entity created for mapping can be a Model for WPF MVVM.
For primitive type can be, but reference can be exposed by interface.
Or should we directly bind entity ?.
If Model is created rather directly use of POCO object. It is much more flexible for refresh cases. User can not change property of POCO object if cancel operation is there.
There is no control if Library returns a List of entity as API return. So anybody can do add or delete in list. How should I keep control on it. Should I create proxy derived from IList which will keep track of it.
IEnumerable is used to exposed collection by interface.
Is it right to throw Exception occurring in an API or should I return null?.
Exception is much more better to make know to user about error. but wrap the exception in user readable rather returning NHibernate exception.
Is it good keep logging in the Library
Logging is very good feature to know about issue.
Should we expose the Entity (POCO Class).
yes, otherwise whats the use of the entities when nobody uses
Is it good to have a Entity with internal protected property and property exposed as a interface.
It depends! internal protected properties are no problem when using an ORM but i prefere to reduce internal stuff to a minimum because i like objects maintaining their own state. interfaces are fine
Entity created for mapping can be a Model for WPF MVVM.
of course. No need to duplicate them another time. That's what persistence ignorance is for
Or should we directly bind entity ?
More ofthen than not, the UI requirements are very different than persistence/businessrules so there will be specialised ViewModels for UseCases/Views. However simple Dataholders like Order class thrown into Lists can be bound directly (e.g. using a DatabindingFactory to make them implement INPC)
There is no control if Library returns a List of entity as API return. So anybody can do add or delete in list. How should I keep control on it. Should I create proxy derived from IList which will keep track of it.
Lists are just in memory container. The user still has to go through the API to Save/Update state.
Is it right to throw Exception occurring in an API or should I return null
if collections are returned then empty collections are far better than null.
Exceptions however should bubble up preferably wrapped in own handable Exceptions. Implement NHibernate.Exceptions.ISQLExceptionConverter (e.g. like NHibernate.Test.ExceptionsTest.MSSQLExceptionConverterExample) and configure it with e.g.
config.DataBaseIntegration(db => db.ExceptionConverter<MyExceptionConverter>())
Is it good keep logging in the Library
absolutly. Logging enables debugging deployed applications. (Fluent)NHibernate already has lot's of logging built in use it if possible.

Resources