Using Test-Driven in DDD (Domain layer) - testing

How to start with TDD in Domain layer, and what I mean by this is how to test Domain Models?
What is it that should be tested? the Aggregates or each Entity ?
What are some good practices and strategies for testing the onion architecture Domain layer ?

Here is a very good example of Vaughn Vernon on how to test the Domain Model:
https://github.com/VaughnVernon/IDDD_Samples/tree/master/iddd_collaboration/src/test/java/com/saasovation/collaboration
Check the subpackage, each packages test a layer of the onion architecture:
the application layer,
the domain layer,
the infrastructure layer
On the domain model tests, each components are tested: aggregates for their methods, entities and even value objects.
He is the author of the book Implementing Domain Driven Design, and he writes examples in java and C#. He is recognized for his work in the DDD community and his work promotes good practices. I suggest you to also read his book to learn more about this fascinating subject.

Related

Software Architecture - A Beginner's Questions

I worked at a company that uses Model View Controller architecture, it's a big project that didn't have unit tests because the code is tightly coupled.
So when I saw this, I started my research and came across several terms, like Domain-centric and Data-centric Architectures.
I recently started reading Uncle Bob's "Clean Architecture" and I'm confused...
I have two questions:
Is MVC architecture good for small projects?
Is Clean Architecture a monolithic architecture?
AND:
Can you advise me on some introductory books related to Software Architecture?
Sorry about my English.
MVC architecture is good even for small projects because it reduces coupling and ensures high cohesion
I don't think Clean architecture is a monolithic architecture because it can also be a micro-service architecture: https://blog.cleancoder.com/uncle-bob/2014/10/01/CleanMicroserviceArchitecture.html
For Software Architecture resources you could check out online courses through sites such as Udemy, or reference a textbook such as Software Architecture in Practice: https://www.amazon.ca/Software-Architecture-Practice-3rd-Bass/dp/0321815734
When it comes to design in general and architecture in particular it is always a matter of trade-offs so depending on the specifics MVC can be good for small projects and CLEAN can be used in microservices.
Generally speaking, MVC was born in the 70s and while it revolutionized UI te two way communications in incurs creates complexity, some coupling to the backend apis etc. There are many other approaches today like for example encapsulated component in svelte
As for CLEAN - in my opinion it makes little sense for anything but monolithic applications since there's a lot of overhead in each microservice and if you model each entity in a separate service you'd just end up with a distributed monolith

Does an external service (API) fit the DDD definition of a Repository?

I haven't found anything concrete on the topic from the gurus, except
There are other ways to implement an Anticorruption Layer, such as by
means of a Repository (12). However, since Repositories are typically used
to persist and reconstitute Aggregates, creating Value Objects by that means
seems misplaced. If our goal is to produce an Aggregate from an Anticorrup-
tion Layer, a Repository may be a more natural source.
from Vernon Vaughn.
What my concern is that mostly ORMs/queries are used as examples of Repositories in the DDD literature. My project is very scarce in domain logic cause it's mainly a wrapper on a couple of APIs and combines the outcome of those Contexts. The responsibilities of the project are broad and could fit many areas/contexts of the business as a whole. The only architectural rule forced from the beginning is the onion architecture and at least the DDD technical modeling concepts seem fitting for me. I must say it's hard to reason about the domain in this particular ongoing project.
Does an external service (API) fit the DDD definition of a Repository?
Maybe?
REPOSITORIES address the middle and end of the [domain object's] life cycle, providing the means of finding and retrieving persistent objects while encapsulating the immense infrastructure involved.
Repository is a pattern, motivated by the notion of separation of concerns -- you shouldn't have to fuss with the details of persistence when you are working on the domain logic.
DDD is about the domain only. Details of how your app persists entities are not of its concern. That's the reason why you define the interface (in the case of .NET) of your repository in your domain, but the actual implementation is part of the infrastructure of your code.
Repositories are nothing but a pattern for you to do "CRUD" operations on an entity without the concerns of how is done. Remember that your client class (the one using the repository) can only see the exposed public methods. Whatever happens inside, it's a mystery :)
DDD says, give me an interface for me to operate. How you do it, that's your problem. You can effectively persist your entities using an external API (think of Twitter API), a text file, ORM (direct connection to a database). DDD doesn't care.
take a modern JavaScript website as an example. You'll have plenty of REST calls to create/find/update/delete your domain objects.
In the case of a server application, you'll have a database and a DAO implementation as a client interface to your database. In your web application, you'll also have some REST-client functionality as client interface to your server application. Both are considered repositories, no matter if the implemenentation of the client interface access data in your database / your server / your file sytem etc.

How to bind UML with code?

I am beginning in UML and software analyse and i do not understand how UML and diagrams can influence coding and software architecture while we can directly build the code and its data base without diagrams.
I read lot of tutorials abouat the subject but not enough to understand the utility of UML in coding.
I understand everey diagram and its role. That is not my problem but i do not yet understand their roles after the analyse and design phase.
So what is the role of UML in coding phase of a software ?
Thank you.
The comment by #xmojmr already puts it right. UML creates a model (hence the M in UML) of a system. A model reduces information of a system to a level so it is a) manageable and b) complete. Human brains are not computers and you need a means of communication what the system is all about. You can do that as pure code, as paper document and as UML model. A combination of all is not uncommon. As long as you have tiny systems you can live with pur code and tools like Doxygen. But once it starts getting complex you need some handles. UML offers these to end users, architects, testers, developers, managers, etc. Along with UML you will also need a methodology. UML delivers the syntax how to document a system. But you need some structure above to write a nice novel.
UML-based models play an essential role for coding/implementing a software system in model-based (or model-driven) development. The basic idea is that you start making a model of your problem domain (the domain model), then you derive from it a platform-independent design model, which can be transformed into platform-specific implementation models (e.g. for Java- or C#-based platforms) that are finally encoded in the target languages.
The most prominent part of model-based development is the encoding of model classes (forming the model layer in an MVC architecture for apps) based on a data model (a UML class model) that has been derived from an information design model, which was obtained from a domain information model (where all these information/data models are UML class models).
You can find an instructive example of model-based development in my tutorial book Engineering Front-End Web Apps with Plain JavaScript.
This one is in my point of view a duplicate of that other question. It can't be flagged because there is no accepted answer. The related question on meta stackexchange does not provide a clear solution to that situation.
I think my personal answer was relevant and is applicable to the current question.
To be synthetic, Martin Fowler considers current uses of UML. I think he describe the current practices. Perhaps should these evolve ?
Perhaps would the initial question be the right place to discuss ?

What does "model" actual mean in the context of rich domain models and anemic domain models?

Apologies in advance if this question has already been answered, but I haven't been able to find a definition for what model actually means in this context that I actually grok.
I've heard the term model be used to describe just the persistence layer, all domain specific code, or just domain entities. From what I've read, anemic domain models seem to be regarded as a bad thing, but the specific reasons as to why don't quite seem to click with me and I think it's because I don't know what model is referring to in this context. US this specific to MVC?
If it's just referring to entities, what is the issue with only putting the logic to keep data integrity in the entities and the putting the logic for actually utilizing those entities into service classes? Is that still considered anemic? If that is the case how can you actually implement rich domain models without breaking single responsibility principle.
The answer doesn't need to be framework or language specific, but if it matters I'm working with php and Symfony2.
Thanks in advance anybody who takes the time to provide me some clarity!
Just on the distinction of what should be in the service layer versus the domain layer, here's a nice article by Martin Fowler on Anaemic Domain Models.
The anti-pattern he talks about is when the domain model lacks the domain logic & is really just a collection of getters & setters.
The fundamental horror of this anti-pattern is that it's so contrary
to the basic idea of object-oriented design; which is to combine data
and process together. The anemic domain model is really just a
procedural style design.
Quoting Eric Evans, he says:
Application Layer [his name for Service Layer]: Defines the jobs the
software is supposed to do and directs the expressive domain objects
to work out problems.
…
Domain Layer (or Model Layer): Responsible for representing concepts
of the business, information about the business situation, and
business rules. State that reflects the business situation is
controlled and used here, even though the technical details of storing
it are delegated to the infrastructure. This layer is the heart of
business software.
[My emphasis]
Generally, his blog posts are a great introduction to some of the concepts within DDD.

approaches to WCF service version control

We are implementing numerous services in our company and running into versioning issues with data contracts. One of the problems we have is that our data contract are also used as the model of the actual application behind the service. I was wondering what approach others have taken in this kind of situation or just service versioning in general. I am aware of the microsoft best practices guide but wanted to see if anybody has any other ideas on how to version.
The first rule of Services, Business Object != Message Object. Basicly, never expose your business objects as data contracts. Or as I like to say, you can't fax a cat. You can send a facsimile of a cat, but you can't send a cat over the wire. Here's a great picture to remind you: http://www.humorhound.com/2009/04/demotivational-poster-youre-doing-it-wrong/
In more modern terms, it is really the MVVM pattern. The view of the model that the domain layer uses is not built for a client, so you have to create a separate model and view for the other layers. Yes it seems like a lot more work, but in the end it is a much easier and better way to build service oriented applications. Versioning is just one of the ways that it makes life easier. The other important thing is that you tend to build models that are geared around how it is going to be used, and you wind up with more explict code (less crazy branching).
The way that we have implemented this is to build a facade layer on top of the business layer.
The facade layer talks to the rest of the world using the objects defined in the data contracts.
The facade layer maps the objects to internal objects before sending the data into the business layer.
This isolates the internal functionality of your system from the objects used in the data contracts.