Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what folder should I put my extension methods in ASP.Net MVC?

Tags:

Like the title said, In what folder should I put my extension methods in ASP.Net MVC?

What is the best practice for this?

like image 248
strike_noir Avatar asked Jan 22 '13 10:01

strike_noir


People also ask

How do you declare an extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

What is extension method in ASP NET MVC?

Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC. Extension methods are static methods of static class and they use "this" keyword in argument list to specify the type they extend. The following demo shows how to build extension method that returns word count in string.

Where we can use extension method in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

Which folder in an MVC application contains the style definition files?

Content. The Content folder contains static files like CSS files, images, and icons files. MVC 5 application includes bootstrap.


1 Answers

I don't believe that there is a standard best practice to follow, but I usually do one of two things:

  • For smaller projects, I'll simply create an "Extensions" folder, and add the various extension classes to there.
  • For larger solutions, I'll have a separate project named something like ProjectName.Extensions, and within that project, I will have a folder structure that mirrors the namespaces of the classes I'm extending. For example:

ProjectName.Extensions/System/StringExtensions.cs ProjectName.Extensions/System.Web/HttpContextExtensions.cs ProjectName.Extensions/System.Xml/XmlWriterExtensions.cs

And so on...

Regardless of which approach I take, in both cases I'll name the class ClassNameExtensions.cs (e.g., StringExtensions.cs, ListExtensions.cs, etc.).

like image 109
matt Avatar answered Sep 20 '22 12:09

matt