DDD etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
DDD etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

27 Mayıs 2021 Perşembe

Domain Driven Design - Application Services

Giriş
Domain Driven Design servisleri iki çeşit olarak sınıflandırıyor

1. Application Services

Application Services - Dış Dünyaya Sunulan Servisler
Açıklaması şöyle.
Application Services are the services used by the outside world which may have representations of data. A example of an application service is a database CRUD operation.
Örnek
Application services olarak Real-time stock control alanından şunlar verilebilir
payments microservices
ensure specific focus on consistent integration with the retail organizations preferred payment infrastructure

promotions microservices
allow the retail organization to provide consistent integration with price and supplier fluctuations in stock they are maintaining.

available to sell microservices
providing retail locations and suppliers with consistent integration to stock information, such as depleted stock items, updating stock items, and more

retail processes 
where long running processes can be captured, implemented, and leverage tasks for retail associates at the store level integration microservices and integration data microservices

5 Ekim 2020 Pazartesi

Domain Driven Design - Anemic Domain Model

Giriş
Bu konuyu ilk olarak burada gördüm. Bu konuda vurgulanan şey Bounded Context içindeki modelin hiç bir logic içermemesi. Normalde beklenen şey şöyle.
The Domain Model should be useful for the problem we're trying to solve. The model is there to enforce the business rules that we have ...
Örnek
Elimizde şöyle bir Spring servisi olsun.
@Service
public class ExpenseService {
  private ExpenseRepo expenseRepo;
  private AuthenticationBO authenticationBO;
  ...
  // other injected classes

  @Override
  @Transactional
  public void createExpense(ExpenseDTO expenseDTO) throws ValidationException {
    validateExpense(expenseDTO);
    ExpenseEntity expenseEntity = new ExpenseEntity();
    initExpenseEntity(expenseEntity, expenseDTO);
    expenseRepo.save(expenseEntity);
  }

  ...
  // other methods
  private void initExpenseEntity(ExpenseEntity expenseEntity, ExpenseDTO expenseDTO) {
    UserEntity userEntity = authenticationBO.getLoggedUser();
    expenseEntity.setUser(userEntity);
    expenseEntity.setPrice(expenseDTO.getPrice());
    expenseEntity.setComment(expenseDTO.getComment());
    expenseEntity.setDate(LocalDateTime.now());
    initExpenseTypes(expenseEntity, expenseDTO.getTypes());
  }
  ...
  // other methods
}
Bu kodda domain nesnesi sadece getter/setter'lardan oluştuğu için anemic kabul ediliyor. Zararı şöyle
Official Spring tutorials teach us that domain objects shouldn’t have any methods except getters and setters and they should be POJOs. Many authors (like Martin Fowler) consider it an antipattern and call it the Anemic Domain Model.

With Anemic Domain Design, all a program’s logic is kept in the business logic layer (classes with Service or BO suffixes). Then domain objects don’t have methods that operate with class fields, hence the object doesn’t have behavior. That breaks OOP principles, GRASP patterns, and prevent us from implementing design patterns.
Bu kodu zengin alan modeli (rich domain model) kapsamında şu hale getirebiliriz. Bu sefer de @Entity nesnesi içine Repository eklemek zorunda kaldık. Bence bu da iyi değil
@Entity
public class ExpenseEntity {
  ...
  @Autowired // model has dependency from other layers and from infrastructure
  private ExpenseRepo expenseRepo;
  @Autowired
  private AuthenticationBO authenticationBO;
  @Autowired
  private UserRepo userRepo;
  ...
  @Transactional
  public void createExpense(ExpenseDTO expenseDTO) {
    // model does too much!
    validateExpense(expenseDTO);
    ExpenseEntity expenseEntity = new ExpenseEntity();
    initExpenseEntity(expenseEntity, expenseDTO);
    expenseRepo.save(expenseEntity);
  }
  private void initExpenseEntity(ExpenseEntity expenseEntity, ExpenseDTO expenseDTO) {
    UserEntity userEntity = authenticationBO.getLoggedUser();
    expenseEntity.setUser(userEntity);
    ...
    initExpenseTypes(expenseEntity, expenseDTO.getTypes());
  }
  private void initExpenseTypes(ExpenseEntity expenseEntity, List<String> types) {
    // model persists to a DB other objects!
    List<ExpenseTypeDictEntity> expenseTypes = new ArrayList<>();
    types.forEach(x -> {
      ExpenseTypeDictEntity typeDict = expenseTypeDictRepo
        .findByNameIgnoreCase(x.trim())
        .orElseGet(() -> createExpenseTypeDict(x));
       ...
    });
    expenseEntity.setExpenseTypeDict(expenseTypes);
  }
  private ExpenseTypeDictEntity createExpenseTypeDict(String name) {...}
}
Bu kodun şu akışa uygun hale gelmesi gerekir.
Böylece alan nesneleri ile JPA teknolojisi birbirinden ayrılabilir. Yani basit bir kural olarak alan modeli teknolojiden bağımsız olmalıdır. Açıklaması şöyle.
The business logic pertaining to the Product class can exist within the class itself and the business logic for the domain can be tested in isolation. The Product class contains data fields and these can be persisted to a database. As part of that process the data fields are first wrapped in another class, and then transformed into a JPA entity before being persisted. 
Örnek
Yine sadece Spring servisleri kullanan bir başka örnek şöyle
@Transactional
public void clearBills(Long customerId) {
  // Get bills needed for clearing
  ClearContext context = getClearContext(customerId);
  // Verify that the amount is legal
  checkAmount(context);
  // Determine whether coupons are available and return the deductible amount
  CouponDeductibleResponse deductibleResponse = couponDeducted(context);
  // clear all bills
  DepositClearResponse response = clearBills(context);
  // Send repayment reconciliation message
  repaymentService.sendVerifyBillMessage(customerId, context.getDeposit());
  // Update account balance
  accountService.clear(context, response);
  // Dealing with cleared coupons, used up or unbound
  couponService.clear(deductibleResponse);
  // Save coupon deduction records
  clearCouponDeductService.add(context, deductibleResponse);
}
Örnek - Anemic Mesajlaşma Sistemi
Bir mesajlaşma yazılımını güncellerken şöyle bir durumla karşılaştım. Güncellediğimiz yazılım, mesajlar arasında graph tarzı ilişkiler tutuyordu. Mesajlar ara katman yazılımından gelen ve behaviour içermeyen, sadece veri içeren nesnelerdi.

Mesajları çeşitli durumlara göre sıralayan (örneğin öncelik) bir servis katmanı da yoktu.

Dolayısıyla domain sadece getter/setter'lardan oluşan data nesnelerinin graph'ından ibaretti.

Örnek - Anemic İstek (Request) Sistemi
Bir  yazılım, çeşitli istekler gönderiyordu. Bu istekler bir ICD mesajında tanımlıydı. Ancak istekleri sarmalayan bir domain nesnesi yoktu. Gönderilen istekler farklı listelerde saklanıyordu. Mesela sentList, acceptedList, deniedList gibi. Gelen cevaba göre istek farklı listeye taşınıyordu. Bir zaman sonra farklı bir liste daha eklenmesi gerekti. Bu liste notHitList yani işlenen ancak istenilen sonucun alınamadığı bir listeydi. Kod çorbaya döndü.

Domain anemic olmasaydı, Request nesnesi diye bir şey olurdu. Bu nesnenin status diye bir alanı olurdu. Bu alan tüm listelerin bir enum'u şeklinde olurdu. Yeni liste eklemek yerine yeni enum eklenir ve elimizde tek bir liste olurdu

Aslında her gönderilen istek bir Request içinde saklansa 

22 Temmuz 2019 Pazartesi

Domain Driven Design - Bounded Context

Giriş
Domain Driven Design'ın en önemli kavramlarından birisi olan Bounded Context ile ilgili notlarım

Bounded Context Nedir?
Bounded Context bir yazılımı daha küçük alt bileşenlere bölmek içindir. MIL-STD- 498 terminolojisi ile konuşursak CSCI (Computer Software Configuration Item) yazılım ise CSC (Computer Software Component) gibi düşünülebilir. Bir bakıma tabii ki :) Açıklaması şöyle.  
Building just one domain model for entire e-commerce will be tough to comprehend and implement in the code. Bounded context helps split the e-commerce domain into smaller subdomains: E.g. Inventory, Shopping Cart, Product Catalog, Fulfilment & Shipment, and Payment. We can use technics like event-storming to identify such subdomains and bounded contexts. So we now have Inventory bounded context, Product Catalog bounded Context, and so on…
Bounded Context ve Ubiquitous Language İlişkisi Nedir?
Konuşulan ortak dildeki (Ubiquitous Language) bazı kavramlar bağlama göre farklı anlamlara gelebilir. Bounded Context kavramın hangi bağlamda kullanıldığını belirtir. Örneğin Product sınıfı context'e göre farklı bir şeyi temsil edebilir. Açıklaması şöyle
It is important to note that the Product in each bounded context has very different behavior. In Inventory, Context Product is concerned about weight, expiry date, and supplier, whereas in Shopping Cart bounded context, the expiry, the Supplier of the Product, is not in the picture. So it is better to model different Product classes in each bounded context instead of having a common Product class across the bounded context.
Bir örnek şöyle
Let us consider an enterprise application in the telecom domain. There will be more than 70 applications in the system. Imagine 70 applications that have to integrate successfully to run the business. It starts with a person approaching the service provider like Airtel, Jio, etc., for a new connection. The moment he approaches the service provider, he will be considered as a Lead. He is not the customer yet. If he shows interest in any plan, he will be considered as an opportunity. His details will go to application verification systems.

If everything is fine, the helpline guy or the sales guy will call and confirm his plan. Once he confirms a plan, he will be provisioned into the system. Then he becomes a customer. His account will be created in a profile application. Note that the same person is identified as lead, opportunity, and customer in different applications.

His details are captured in CRM — customer relationship management system, billing system, sales and marketing system, package management system, fraud management system, inventory systems, analytics tools, dealer management systems, secondary sales systems, revenue leakage tools, debt management systems, etc.

Now imagine if you want a single model — customer in this enterprise application. The leads and opportunities system will be interested in details like what is his existing service provider, through which channel did he get to know about us, etc. Profiling and the account creation systems will be interested in other details like name, address, age, profession, etc. The CRM system might be interested if there are any previous service tickets raised by the person. The sales and marketing team will be interested in his profile and usage details to get an idea of what packages could be recommended to him down the line. The billing system will be focused on the billing address and payment mode, etc. Similarly, the fraud management system, inventory systems, analytics tools, etc., will be interested in other details.

Imagine how confusing your model will be if it includes all these details. The address required for the billing application is the billing address. For the profiling system or CRM application, it will be the current address and permanent address. If the same model is used across the system, at some point in time, the billing team could feel that naming the address field as billing address is more appropriate than the current address and rename the current address field to the billing address. The model integrity is compromised which in turn breaks the system.

So, when the billing team says address, it might be billing address and when the CRM team says address it might be mailing address. The CRM team is not aware of the billing address and the billing team might not be aware of the other addresses. If these two teams discuss with each other the model you could guess the confusion it creates because of the conceptual differences.

Even if both the addresses are saved as different attributes, the billing address will be redundant and irrelevant to profiling and other applications.

The same person is a Lead in leads application and opportunity in the opportunities application. He is a customer in the provisions and accounting systems. In case he did not pay the bills, he will be a defaulter in another system. If you observe, the details of the same person are interpreted differently in different applications based on the context. This is where the bounded contexts come into the picture.

The sales team cannot go to the leads/opportunities team and ask for the customer details because their model is not supposed to have customers but instead have leads /opportunities. They will understand only if you ask for the leads or opportunities details.

To get rid of these, you need to define the boundaries of your model and confine it to a context. Otherwise, there will be a lot of confusion and chaos that creeps into your system and makes the model unmanageable and impure.
Bounded Context ve Noun Based Models
Açıklaması şöyle
Noun based domain models have the downside of introducing high functional coupling in a system.

Which is contrary to the recommendation to keep coupling low and cohesion high.

When data is grouped in noun based models, subsequent steps in a business workflow need access to show, or amend, some of the data embedded in that model.

This couples each subsequent step to that functional model.

As more nouns are introduced to the model, the functional coupling tends to increase.

A better way to divide your domain model is by business capability.

Only store the data required by the respective capability in a local model. Individual capabilities usually only care about a subset of the data.

As the workflow progresses, pass data from one capability to the next.

This can be done either via the frontend, or via event publishing in the backend (depends on how much time there is between the steps).
Şeklen şöyle


Bounded Context'in Varlığını Nasıl Anlarız?
 Açıklaması şöyle. Her ortak dildeki bir kavramın farklılaştığını görüyorsak, orada bir Bounded Context olabilir.
So bounded context is a linguistic boundary! Any time you see that the Product is acting differently, it is a clue that there are different bounded contexts in the play.

One bounded context typically has few (or one) micro-services
Şeklen şöyle. Burada iki tane bounded context var. Her ikisinde de Customer ve Product kavramları var ancak kendi bağlamlarında farklıklar gösteriyorlar. Aslında bu şekli yukarıdaki Noun Based Model'e uymuyor.


Ayrıca Bounded Context'in Kendi Domain Modeli ve Kendi Ubiquitous Language'i Vardır
Bounded Context kendi başına bir birim olduğu için kendine mahsus bir domain modele ve dile sahiptir. Açıklaması şöyle
The ubiquitous language applies within a bounded context. Each bounded context has its own ubiquitous language. It is a language that is spoken both by the Business Teams and the Development teams. It helps them to communicate better.

If your Business team is talking in terms of Database tables, then as the Development Team, you have influenced them incorrectly.
Context Map Nedir?
Açıklaması şöyle
Context Map defines the relationship between different bounded contexts.

How different bounded contexts communicate with each other and how they influence each other.
Bounded Context ve Yeniden Kullanılabilirlik
Açıklaması şöyle. Amaç yeniden kullanılabilir bir yapı ortaya çıkartmak değil. Amaç mantıksal olarak bölünmüş ve yalıtılmış yapılar yaratmak
The promise of reusable components, just as the idea of reusable business logic across applications didn't turn out to be practical. Modern trends reflect this insight well. The microservices approach suggests that instead of reusing code, we should separate things and make them easily replaceable. Domain-Driven Design's Bounded Context concept says the same: that there should be clearly separated contexts that create semantic boundaries, which, in turn, make sharing "business code" among contexts unnecessary and unwelcome by definition.
Dikey bölümleme için açıklama şöyle.
New trends like Domain-Driven Design and microservices advocate splitting applications vertically instead of horizontally. And, new types of development processes and organization, like cross-functional and DevOps teams, support this vertical slicing and scaling much more efficiently.
Isolation Layer
Açıklaması şöyle.
Code reuse between Bounded Contexts  should be avoided. The integration of functionality and data must go through a translation. The translation logic provided by the Isolation Layer.
Isolation Layer için 3 temel yöntem var. Bunlar şöyle.
Customer/Supplier
Conformist
Anticorruption Layer (ACL)
Anticorruption Layer (ACL) Nedir
Aslında ismi Translation Layer'da olabilirdi. Anti Corruption Layer yazısına taşıdım

Bounded Context ve İşbirliği
Benim için ufuk açıcı yazı şu. Her bounded context diğeri ile olan ilişkisini "Foreign Key" benzeri yapılar ile yürütüyor. Aslında bu kullanım bounded context'in daha büyük bir aggregate'in parçası olduğunu gösteriyor. Bu büyük aggregate daha küçük ve yönetilebilir hale getirilmiş.

Örnek
Elimizde şöyle bir mesaj olsun. Foreign key'leri göndererek mesajı işleyen diğer kod parçalarına da bilgiye erişme imkanı tanırız.
{
  "Status": "Closed",
  "RentalAgreementID": 1234,
  "CustomerID": 8965,
  "VehicleID": 98263,
  "RentalAgent": 24352,
  "Broker": 6723
}
Bounded Context'ler Arası İletişim - Domain Events
Domain Events yazısına taşıdım.

Plugin Yapısı ve Bounded Context
Şöyle bir yapı ile karşılaştım. Her plugin için bir bounded context yapılmıştı. Plugin kendi bilgilerini burada saklıyordu.
AbstractBoundexContext <-(extends)-AppBoundexContext
                ^-(extends)--PluginABoundedContex,PluginBBoundedContex
Ana uygulama AppBoundedContext'i kullanıyordu. Her plugin ise sadece kendi bounded context'ini biliyordu.

Ana uygulama çalışırken plugin içindeki bazı bilgiler var ise farklı davranıyordu. Dolayısıyla iki tane bounded context arasında işbirliği (collaboration) gerekiyordu.

Bu durumda kurtulmak için geliştirenler PluginABoundedContex içindeki bilgileri AppBoundexContext'e geçmişlerdi.
PluginABoundedContex --(calls)--> AppBoundexContext
Ana uygulamada oluşan olayları plugine bildirmek için de listener yapısı vardı.
AppBoundexContext -- (notifies)-->PluginABoundedContex

9 Nisan 2019 Salı

Domain Driven Design

Giriş
Domain Driven Design (DDD) yazılım ile sürekli evrilen alan bilgisini (domain) birbirine bağlamak için kullanılır. Burada özellikle model yerine domain kelimesini kullanılıyor. Çünkü modelde statik veri daha ön plana çıkıyor hissi var.

DDD Eric Evans'ın 2003 yılında yayınlanan "Domain-Driven Design: Tackling Complexity in the Heart of Software" adlı kitabından doğmuştur

Model Driven Architecture vs Domain Driven Design
Model Driven Architecture ile Domain Driven Design arasındaki fark şudur:
In MDA, we start with database table diagrams or ERDs, and build objects to match.  In DDD, we start with interactions and behaviors, and build models to match.
DDD ve Nesneye Yönelik Tasarım el ele birbirlerine destek olan kavramlardır.

Clean Domain Driven Design
DDD'yi olgunluk modellerine göre sınıflandırır

Level 0: S.O.L.I.D code
Level 1: Hexagonal Architecture
Level 2: Clean Architecture
Açıklaması şöyle
Oftentimes used interchangeably with Hexagonal Architecture, Clean Architecture, nevertheless, presents a distinct, again — qualitative — break away from the former layer. If one have to resume in one concept the most important architectural innovation which application design exhibits on Level 2, it is this — Use Case.
Level 3: Domain-Driven Design
Son aşamada da Use Case kullanan DDD'ye erişiyoruz

Temel Kavramlar
DDD kodun belli şekilde düzenlenmesini bekler. Açıklaması şöyle.
One of the ways DDD helps teams of developers is by suggesting a specific (yet still subjective) way of organising your code concepts and behaviours. This convention makes it easier to discover things, and therefore easier to maintain the application.

-Domain concepts are encoded as Entities and Aggregates
-Domain behaviour resides in Entities or Domain Services
-Consistency is ensured by the Aggregate Roots
-Persistence concerns are handled by Repositories

This arrangement is not objectively easier to maintain. It is, however, measurably easier to maintain when everyone understands they're operating in a DDD context.
Şeklen şöyle


1. Ubiquitous Language
Müşteri ile konuşulan ortak dil. Açıklaması şöyle.
Ubiquitous Language is the practice for building up a communication language between developers and users. It helps developers and the business share a common language platform that both parties understand to mean the same things. Ubiquitous Language should evolve as the team's understanding of the domain grows.
2 Bounded Context
Bounded Context yazısına taşıdım.

3. Entity
Kendine ait bir kimliği olan nesne. Açıklaması şöyle.
An Entity is an object that can be identified uniquely or by its identifier. An Entity can be identified either by its IDs or a combination of some attributes. An entity is an identity.
4. Value Object
Kendine ait bir kimliği olmayan nesne. Açıklaması şöyle.
A Value Object is an object that contains attributes but has no conceptual identity. It is a descriptor or property which is important in the domain you are modeling.
5. Repository
Nesnelerin saklandığı alan. Açıklaması şöyle.
A Repository mediates between the domain and data mapping using a collection-like interface for accessing domain objects. It is more like a façade to your data store that pretends to be a collection of your domain. A Repository provides a centralized façade, storing data in a database, XML, SOAP, REST, and so on.
Repository sorgu inşa eder. Repository örüntüsüne alternatif olarak direkt Query Object kullanımı (yani sorgunun kendisi) sunuluyor.

Klasik bir Repository nesnesine şuna benzer metodlar bulubur
getById(), deleteById(), getAll(), save(), update(). Repository altta "Entity Framework" veya "JPA" gibi bir ORM kullanabilir.

Nesne çekmek için örnek
CSAgent agent= (CSAgent) EmployeeRepository.getById(agentId);
Order order= OrderRepository.getById(orderId);
6. Aggregate Root veya Aggregates
Aggregates yazısına taşıdım.

Altyapı
Açıklaması şöyle.
...main point is to split the Domain Logic (Business Logic) from the Infrastructure (DB, File System, etc.).
Veri tabanı
Açıklaması şöyle.
These days, you are likely to see reads (queries) handled differently than writes (commands). In a system with a complicated query, the query itself is unlikely to pass through the domain model (which is primarily responsible for maintaining the consistency of writes).
Servisler
Yukarıdaki kavramların bazılarının servis olarak ta anlatıldığı görülebilir.
- Application services.
- Domain services.
- Infrastructure services.
- Factories, Repositories, Specifications
Application Services - Dış Dünyaya Sunulan Servisler
Application Services yazısına taşıdım.

Specifications - İş kuralları
Specifications yazısına taşıdım

Domain Services
Domain Services yazısına taşıdım.

Infrastructure Services - Kendi Kaynaklarımıza Erişmek İçin Servisler
Açıklaması şöyle.
An Infrastructure Service is a service that communicates directly with external resource. For example, accessing file system, registry, SMTP, database, and so forth in the application.
Anti Corruption Layer
Anti Corruption Layer yazısına taşıdım.

Anemic Design Model
Anemic Design Model yazısına taşıdım..


16 Kasım 2014 Pazar

Domain Driven Design - Anti Corruption Layer - Translation Layer Gibi Düşünülebilir

Anti Corruption Layer Nedir?
Anti Corruption Layer, Domain Driven Design ile karşımıza çıkan bir kavram. Bir çok açıklaması mevcut. 

1. Uygulamalar Arasındaki Katman Olabilir
Uygulamalar arasında olabilir. Açıklaması şöyle
When one system needs to talk to another system and if these 2 systems are not compatible, you can use an intermediate layer which is capable of translating first systems communication to the protocol which is used by the second system and vice versa, this intermediate system is called as an Anti Corruption Layer since that layer helps to avoid the data corruption which would have happened otherwise. This is not a new concept and the traditionally, Enterprise Service Bus (ESB) has been used as this intermediate layer.
Şeklen şöyle

2. Uygulama ve Veri tabanı arasında olabilir
Açıklaması şöyle.
If your application needs to deal with a database or another application whose model is undesirable or inapplicable to the model you want within your own application, use an Anticorruption Layer to translate to/from that model and yours.
3. Bounded Context' ler Arasındaki Katman Olabilir
Bir açıklaması şöyle. İki tane Bounded Context arasına yerleştirilir. Böylece birbirlerinden yalıtılmış hale gelirler. 
Anticorruption Layer is a means of linking two Bounded Contexts. Create an isolation layer to provide clients with functionality in terms of their own domain model. The layer talks to the other system through its existing interface, requiring little or no modification to the other system. Internally, the layer translates in both directions as necessary between the two models.

The public interface of the ACL usually appears as a set of services. One way of organizing the design of the ACL is as a combination of facades, adapters, and translators, along with the communication and transport mechanisms usually needed to talk between systems.
4. Microservice ve Legacy Sistem Arasındaki Katman Olabilir
Açıklaması şöyle
A legacy system may have unmaintainable code and an overall poor design, but we still rely on the data that comes from this module. An anti-corruption layer provides the façade, or bridge, between new microservices architecture and legacy architecture. Therefore, it allows us to stay away from manipulating legacy code and focus on feature development. 
Şeklen şöyle


Anti Corruption Layer Ne Değildir
Software ICD mesajlarını kendi yapımıza çeviren arayüz katmanına Anti Corruption Layer denilemez. Çünkü bu  çevrim işlevini yapacak kod parçası zaten yazılacaktır.