Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# attributes naming convention

Attributes in C# should be named using PascalCase with the 'Attribute' suffix as written here. My question regards to more subtle thing; how should attributes be named when it comes to natural language?

For instance - I have an authorization attribute in ASP MVC application which checks if logged user is authorized to see a module (represented by particular controller). I've decided to name it as follows:

[IsLoggedUserAuthorizedToSeeModule(ModuleName = ModuleManager.Modules.ExampleModule)]
public class ExampleModuleController : BaseController
{
    ...
}

It's quite comprehensible to me, but it's a long name, I have doubts about it. Microsoft usually provides short names of default attributes, but they aren't very homogeneous. The Serializable attribute means that the decorated class can be serialized. The ObsoleteAttribute attribute means that decorated class has some state. Description and Category attributes define metadata.

Full list of default .NET attributes can be found here.

Are there any guidelines about attributes naming convention? Do you know any articles about it?

like image 868
Arkadiusz Kałkus Avatar asked Oct 14 '25 19:10

Arkadiusz Kałkus


1 Answers

Attributes should be named using short and concise pascal-cased sentences.

After all, they decorate assembly members and when you see an applied attribute, you want to understand what do they mean by just reading their name.

Anyway, how to name your attributes is subjective. What may be meaningful for me, can be disturbing for you.

About yout concrete attribute IsLoggedUserAuthorizedToSeeModuleAttribute, maybe it can be simplified to just AllowedModuleAttribute and you should implement another attribute AuthorizeAttribute (or re-use MVC's one):

// This way you splitted out the concept of "logged users" and "what module"
[Authorize, AllowedModule(ModuleManager.Modules.ExampleModule)]
public class ExampleModuleController : BaseController
{
    ...
}
like image 127
Matías Fidemraizer Avatar answered Oct 17 '25 09:10

Matías Fidemraizer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!