👮 Architecture Governance: Keeping Your System's Design on Track with Continuous Arch Testing
Tags: architecture, dotnet, csharp, codequality
Published: 2023-01-22 23:00:00

## 📚 Tl;dr

- Architecture governance is a system for maintaining consistency and quality in architectural decisions throughout the development process of a software project.

- It involves having control over architectural rules and guidelines and ensuring they are adhered to.

- Fitness function is an important component of architecture governance which acts as a "cop" to ensure developers don't break established architectural rules and measures the alignment of the software architecture with business goals.

- NetArchTest is a tool that can be used to implement the automatic fitness function by writing unit tests that check dependencies between packages, classes, and methods in the codebase.

## 📐 What is architecture governance?

When building software projects, there are many architectural decisions that need to be made. It is important to maintain consistency in these decisions to ensure the overall quality and maintainability of the project.

Having a system for architecture governance allows the team to have control over the architectural rules and guidelines, and to ensure that they are being adhered to throughout the development process. This can help to ensure that the project remains on track, and that any issues or inconsistencies are identified and addressed in a timely manner.

Additionally, having a well-defined architecture governance system can facilitate collaboration and communication among team members and help to ensure that everyone is working towards the same goals. This can lead to a more cohesive team and a better end product.

It's also important to note that architecture governance is not a one time process, but a continuous one that adapts to the changes of the project and the organization. That way, the team can ensure that the software architecture is aligned with the business goals and the evolving requirements.

## 👮 Fitness function

Fitness function is a way to measure the alignment between the software architecture and the business goals. 

One approach to implementing a fitness function is through the use of automatic fitness functions. Automatic fitness function will be in our case units that will check the validity of architecture decisions. 

## 🚀 NetArchTest comes to play 

NetArchTest is a library that can be used to write such tests (automatic fitness functions), it allows to check the architecture and ensure that it adheres to predefined rules and constraints.

With NetArchTest, developers can write unit tests that check the dependencies between different packages, classes, and methods in their codebase. This can be useful for checking that the architecture adheres to principles such as separation of concerns and the single responsibility principle. By running these tests regularly, developers can ensure that the architecture remains consistent and that any changes to the codebase do not violate the predefined rules. ADR and Arch unit test is amazing mix that will help you keep consistant great design.

## 📐 Example 1️⃣

### ADR 1: Creating a Custom Shared Logging NuGet Package for Improved Performance and Maintainability.

#### Status

Approved

#### Context

We have identified that the current using the Microsoft ILogger interface does not provide optimal memory efficiency and there is inconsistent code optimization across our solution. This can lead to performance issues within our codebase. Additionally, having a consistent logging implementation across the solution will make it more maintainable and easier to understand.

#### Decision

We have decided to develop a custom shared logging NuGet package with “IOptimizedLogging” interface to improve the efficiency of our logging system.

### Consequences

####👍 Pros:

- Improved memory efficiency: The custom shared logging NuGet package will improve the memory efficiency of the logging system, as the optimized implementation will be used consistently throughout the solution.
- Improved performance: The package will also improve the performance of the logging system, as the code that optimizes log execution will be consistently implemented throughout the solution.

####👎 Cons:

- Increased development and maintenance effort: The development of a custom shared logging NuGet package will require additional resources and effort, including the development and testing of the package, as well as its ongoing maintenance and updates.
- Each new developer or current one has to know that he must use ```IOptimizedLogger``` instead ```ILogger```

###👮 Fitness Function

{% embed https://gist.github.com/kamilbaczek/df4d22efe39f1af00902efde8ea636f6 %}

---

## 📐 Example 2️⃣
### ADR 2: Notifications module should be decupled by events 

#### Status

Approved

#### Context

The team has determined that the current design of the notifications modules, which relies on coupling via interfaces, is not flexible enough to allow for easy integration with other modules.

#### Decision

The decision is to decouple the notifications modules by using events as the only method of integration, where other modules can publish integration events can be consumed by the notifications modules `event handlers`.

### Consequences

####👍 Pros:

- Increased flexibility in the overall system design
- Easier integration with other modules
- Improved maintainability of the codebase
- Reduced dependencies between modules
- Cons of decoupling notifications modules by using events:

####👎 Cons:

- Additional complexity in the codebase
- Additional overhead of managing events and event listeners
- The need to re-architect the current notifications modules
- More difficult to debug and troubleshoot if there are issues with the event-based communication.

## 👮 Fitness Function

{% embed https://gist.github.com/kamilbaczek/9f4bee3dc532bcf76731c9c2008f31a7 %}

## 📚 Description
Checks that any interfaces or class from notifications modules is used in other modules. This checks compliance with a predetermined ADR to avoid coupling via interfaces in the notification module. It is used as a means of ensuring that the code base complies with this ADR and that new developers do not introduce violations of this decision by their lack of knowledge.

---

## 👮 Fitness Function
{% embed https://gist.github.com/kamilbaczek/9ee93a64ea66efed049ac6eb5011f342 %}

{% embed https://gist.github.com/kamilbaczek/febe52189a9445874d09270b1e71cd5c %}

## 📚 Description
This function verifies that the notification module has event handlers that only consume integration events. It is used to ensure that the notification module adheres to an architectural decision (ADR) that specified that the module should be integrated with other modules through events. The function is checking that this design decision is being followed.

## 🏁 Summary
Architecture governance is a system for maintaining consistency and quality in architectural decisions throughout the development process of a software project.

It involves having control over architectural rules and guidelines, and ensuring they are adhered to. It also promotes collaboration and communication among team members, and helps to align the software architecture with business goals and evolving requirements.

A key component of architecture governance is the use of a fitness function, which acts as a "cop" to ensure that developers do not break established architectural rules and measures the alignment of the software architecture with business goals. One way to implement this is through automatic fitness functions, such as using NetArchTest, a library for writing unit tests that check dependencies between packages, classes, and methods in the codebase to ensure adherence to predefined rules and principles such as separation of concerns and the single responsibility principle.

## 🔗 References

[Software Architecture: The Hard Parts Book](https://www.oreilly.com/library/view/software-architecture-the/9781492086888/)

[Kamil Grzybek Modular Monolith has arch tests examples](https://github.com/kgrzybek/modular-monolith-with-ddd)

[Architecture Fitness Functions Talk](https://www.youtube.com/watch?v=aq8XTTxoslE)

[NetArchTest Project](https://github.com/BenMorris/NetArchTest)

## 🤝🏻 Follow me

🚀 If you're looking for ways to improve your software development skills and stay up to date with the latest trends and best practices, be sure to follow me on dev.to!📚 I regularly share valuable insights and tips on software development, as well as updates on my latest projects.

Speaking of projects, I'd like to invite you to check out my modular monolith project on GitHub💻, called Estimation Tool. It's a great example of how you can use modularization to build a more scalable and maintainable system. The project is open-source and well-documented, so you can learn by example and see how I applied the concepts of modularization in practice.

https://github.com/kamilbaczek/Estimation-Tool 🔗

So if you're looking to level up your development skills, be sure to follow me on dev.to and check out my modular monolith project on GitHub. Happy coding!🎉