Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing Extension Methods [closed]

How do you organize your Extension Methods? Say if I had extensions for the object class and string class I'm tempted to separate these extension methods into classes IE:

public class ObjectExtensions
{
    ...
}

public class StringExtensions
{
    ...
}

am I making this too complicated or does this make sense?

like image 728
Jared Avatar asked Sep 18 '08 20:09

Jared


People also ask

What is extension method with example?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Where do extension methods go?

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.

Are extension methods good practice?

For an application programmer, extension methods are an incredibly powerful and expressive tool. They enable convenience, extensibility, and an improved intellisence experience. However, many of the features that make extension methods so useful for library consumers can be problematic for class library authors.

What is extension method in computer?

In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages.


2 Answers

I organize extension methods using a combination of namespace and class name, and it's similar to the way you describe in the question.

Generally I have some sort of "primary assembly" in my solution that provides the majority of the shared functionality (like extension methods). We'll call this assembly "Framework" for the sake of discussion.

Within the Framework assembly, I try to mimic the namespaces of the things for which I have extension methods. For example, if I'm extending System.Web.HttpApplication, I'd have a "Framework.Web" namespace. Classes like "String" and "Object," being in the "System" namespace, translate to the root "Framework" namespace in that assembly.

Finally, naming goes along the lines you've specified in the question - the type name with "Extensions" as a suffix. This yields a class hierarchy like this:

  • Framework (namespace)
    • Framework.ObjectExtensions (class)
    • Framework.StringExtensions (class)
    • Framework.Web (namespace)
      • Framework.Web.HttpApplicationExtensions (class)

The benefit is that, from a maintenance perspective, it's really easy later to go find the extension methods for a given type.

like image 69
Travis Illig Avatar answered Oct 19 '22 10:10

Travis Illig


There are two ways that I organize the extension methods which I use,

1) If the extension is specific to the project I am working on, then I keep it in the same project/assembly, but in its own namespace.

2) If the extension is of a kind so that I may or is using it in other projects too, then I separate them in a common assembly for extensions.

The most important thing to keep in mind is, what is the scope which I will be using these in? Organizing them isn't hard if I just keep this in mind.

like image 3
Geir-Tore Lindsve Avatar answered Oct 19 '22 10:10

Geir-Tore Lindsve