I feel like there’s a better way handle what I’m doing.

I've learned to dislike simple strings as a selector, but it's used that way for a LOT of things (Dictionary-like structures, ActionResults, etc.). At least, in their base form. Too easy to misspell, difficult to track, all in all, I'm finding that using them borders on an Anti-Pattern. But I'm not satisfied with my solution.

I've gotten into the habit of creating a simple Facade that instead represents the string that I wish to use. This reduces the probability of misspellings, and makes it easier to document and maintain my code (If name changes, I only have to do it in one place, If it's wrong, only one place to correct it, etc.).

Here's an example:

public static class Waste { /// <summary> /// A specific kind of Waste. /// </summary> public static const String Garbage = "Garbage"; /// <summary> /// A different, specific kind of Waste. /// </summary> public static const String Trash = "Trash"; } 

This works pretty great. But recently I came into a weird issue that I'm having with it. Static classes are sealed by default, but in the particular way I want to be using it, I want a common set of strings that they all happen to have, without repeating myself.

At the moment, I took out the static portion of the class, so that I could use inheritance to fold the common functionality into the inheritor. Example (Remove static from above):

public class ExtraWaste : Waste { /// <summary> /// A dangerous kind of Waste. /// </summary> public static const String Nuclear = "Nuclear"; /// <summary> /// A reusable kind of Waste. /// </summary> public static const String Recyclable = "Recyclable"; } 

But ReSharper complains about this (And for good reason, mind you), and wants to declaratively use the base static class (Waste.Garbage instead of ExtraWaste.Garbage), but then that makes it more difficult to track down things belonging to this particular static class, rather than the generalized static class. But it seems silly to re-type the definitions everywhere in separate classes.

Can someone redirect my thinking in the right direction? I feel like I'm missing something obvious.

by NormalPersonNumber3 via /r/csharp

Leave a Reply