Practicing Clean Architecture in C# | by Genny Allcroft | Apr, 2022

Placing my learnings into observe

Picture by AltumCode on Unsplash

I’ve been studying the Clean Architecture book by Uncle Bob. I believed I’d test I understood every thing appropriately by making a mini challenge utilizing clear structure ideas. That is how I obtained on.

Here’s a diagram displaying how clear structure works:

Clear structure diagram

The concept is that the circles rely on the circles inside them i.e. the dependencies level inwards. Within the center are the enterprise guidelines which ought to keep the identical — these shouldn’t rely on something additional out within the circle.

The objective is to have the ability to change issues the place you may change your thoughts (e.g. exterior frameworks, databases, and UI format) actually simply and for the core enterprise logic to not need to be up to date. It additionally helps code be straightforward to check.

To observe clear structure, I’m going to construct an app in C# that permits the person to kind an tackle, after which the person will probably be returned with extra particulars concerning the location — specifically, the geographical coordinates and the complete tackle. A barely pointless app however there we go!

My challenge may have a three-layer structure — the area layer, the information layer, and the presentation layer.

The layers of my app (arrows comply with dependencies)

I’ll comply with the clear structure construction proven within the circle diagram.

Let’s begin with the entities.

Entities

The entities are the enterprise objects of the applying. They’ve probably the most normal high-level guidelines and won’t be modified when one thing modifications externally. The entity layer needs to be unaffected by any modifications in different layers.

In my software, I need to have only one entity, which is known as LocationEntity. It’s going to have the three properties: FullAddress, Latitude and Longitude.

Use Circumstances

Within the use instances part of the applying, there are enterprise guidelines which can be particular to the applying (and wouldn’t be related to different purposes utilized by the enterprise).

Any modifications to the use instances shouldn’t have an effect on the LocationEntity within the entities layer. Moreover, this layer shouldn’t be affected by any modifications to the database, UI, or frameworks used.

In my software, I’m going to have a use case that can get the placement particulars, given an tackle.

I’ll create a GetLocationInteractor class. That is going to have a way known as Deal with, which is able to name a way of IRepository (extra on this later) to GetLocation.

Interfaces

The interface layer has the IRepository interface in it. The concept of the interface layer is to transform knowledge from the shape used within the use instances or entities to the format used within the repository (or database) or within the presentation.

Utilizing an interface in my code right here for IRepository additionally makes it very easy to check my code by writing a unit take a look at. Right here I mock the outcome from the repository to test that my use instances are working correctly.

The information layer of the applying comprises the implementation of the information interfaces outlined within the area layer. In my case, it’s the IRepository that I must implement.

The LocationRepository implements the GetLocation methodology of the IRepository, which calls one other methodology to GetLocationData outlined in an interface known as ILocationApi.

The GoogleMapsApi class makes use of the nuget bundle gmaps-api-net to get the information for the tackle location entered by the person utilizing the Google Maps API.

The code is fairly simple — all you must do is get an API key from the Google Cloud Platform and use the code beneath.

The presentation layer is the outermost layer. As this layer is decoupled from some other, any type of presentation is feasible. For this instance, I’ll use a pleasant straightforward Console App.

In my program, I’m asking the person to enter an tackle. Once they do, they’re offered with the FullAddress and Latitude and Longitude on the Console App.

Right here is the attractive app in motion:

And there you could have it — a challenge constructed with clear structure!

It was nice to place my studying to observe after having learn the Clear Structure e-book.

It was helpful to consider easy methods to decouple the totally different layers in order that the core enterprise logic isn’t disturbed by issues just like the database or the UI determination.

Attempting to construct one thing myself actually helped to carry the speculation to life. I hope you loved studying it!

More Posts