0

I'm using clean architecture in asp.net core

enter image description here

where should I put the extensions method folder

regestea23
  • 456
  • 6
  • 19
  • I would say, the closest possible to the classes it extends. I'd avoid a single "Extensions" place for any element inside my solution, I'll instead have Extensions for my Domain (a subfolder inside Domain), for my Infra (a subfolder inside Infra) and so on. – zameb Sep 12 '22 at 17:03

2 Answers2

3

Extension methods are a way to add functionality to existing classes or interfaces without modifying their source code or creating a subclass. For example, you can define an extension method for the String class that reverses the characters in a string, and then use it as if it was a regular method of the String class.

Taking that information into consideration, you should treat the extension as part of the class, to which that extension extends. In other words, keeping SRP in mind, for every component, that has an extension, there will be a separate corresponding "extensions method folder".

However, you might use extension methods to implement data mappers. In that case, we will follow instructions provided by Uncle Bob in his book "Clean Architecture: A Craftsman's Guide to Software Structure and Design". I will quote the whole chapter, it is very short:

DATA MAPPERS

Going back to the topic of databases, in which layer do you think ORMs like Hibernate belong?

First, let’s get something straight: There is no such thing as an object-relational mapper (ORM). The reason is simple: Objects are not data structures. At least, they are not data structures from their users’ point of view. The users of an object cannot see the data, since it is all private. Those users see only the public methods of that object. So, from the user’s point of view, an object is simply a set of operations. A data structure, in contrast, is a set of public data variables that have no implied behaviour. ORMs would be better named “data mappers,” because they load data into data structures from relational database tables.

Where should such ORM systems reside?

In the database layer of course.

Indeed, ORMs form another kind of Humble Object boundary between the gateway interfaces and the database.

enter image description here

Dmytro T
  • 111
  • 2
  • 5
2

It depends which dependencies this code would create. Generally speaking:

  • put as less code as possible in the outer most layer/circle
  • strictly follow the dependency rule
  • keep your business logic free from "painful dependencies" (external frameworks, IO)

A more detailed discussion on where to put code in Clean Architecture can be found here: https://www.youtube.com/watch?v=7-IEhM6uiUU&t=12s

plainionist
  • 2,950
  • 1
  • 15
  • 27