Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what layer of a project do common extension methods belong?

I'm curious what "layer" of a system common extensions that are global to the application belong in. For instance, I may have extensions that let me use the Rails-like "DaysAgo", "MonthsAgo", etc. type of extension method on integers. What layer of a project does this typically belong in? I was thinking Infrastructure, but that seems to mean database-related (e.g. base repositories and data access). I have a "Core" library project, so maybe it belongs there?

I understand that you want to group extensions that are related to a specific group of classes, but these are essentially used across the entire application. In the days before extension methods, they would be in a Utilities static class or the like, so where should they live now?

like image 630
Wayne Molina Avatar asked Oct 20 '11 13:10

Wayne Molina


People also ask

Where do you put extension methods?

An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.

What are extension methods in C# stack overflow?

Extension methods are ways for developers to "add on" methods to objects they can't control. For instance, if you wanted to add a "DoSomething()" method to the System. Windows. Forms object, since you don't have access to that code, you would simply create an extension method for the form with the following syntax.

What is the role of an extension method when would you use it?

Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.

What are extension methods in LINQ?

Extension Methods are a new feature in C# 3.0, and they're simply user-made pre-defined functions. An Extension Method enables us to add methods to existing types without creating a new derived type, recompiling, or modifying the original types.


1 Answers

I would put these methods at the lowest possible layer where those objects/entities exist.

either in the interfaces, or in the entities or in a core assembly, the lowest the better so all upper layers can use it :)

like image 136
Davide Piras Avatar answered Sep 27 '22 18:09

Davide Piras