Avoiding hard coded strings in MVC Project

I’m really just looking any constructive criticism to what I’ve been doing to avoid hard coded strings in my latest MVC project. Basically what I’ve been doing is making a bunch of static classes that contain nothing but const/static string variables. I’ve been trying to separate the classes out, having a namespace for each controller that contains multiple classes to break up what the string is used for. This is basically I concept I saw when I was doing some android/ios app programming, and I’ve gotten fairly hardcore about separating these strings out of the controller. So much so that my controllers do not have a single ” character in them.

For instance I have a TaxAdminController and I use the following namespace to contain all of the would be hard coded strings for that controller.
namespace my.project.Strings.TaxAdmin { public class Labels { public const string INVOICE_NUMBER = “Invoice Number”; public const string FROM_DATE = “From Date”; public const string TO_DATE = “To Date”; public const string TAX_EXEMPTION_REASON_ID = “Exemption Reason”; public const string INSTANCE_NUM = “Instance Number”; public const string CERTIFICATE_NUM = “CertificateNum”; public const string START_DATE = “Start Date”; public const string END_DATE = “End Date”; public const string TAX_INVOICE_NAME = “TaxInvoiceDiagnostics”; public const string TAX_EXTRACT_NAME = “Tax Data Extract”; public const string MONTHLY_COMPLIANCE_NAME = “Monthly Compliance Report”; } }

namespace my.project.Strings.TaxAdmin { public static class MessagesSuccess { public const string EXEMPTIONS_UPDATE = "Tax exemption updated."; public const string EXEMPTIONS_INSERT = "Tax exemption created."; } } namespace my.project.Strings.TaxAdmin { public static class MessagesError { public static string InvalidRow(string rowValue) { return "Requested data "" + rowValue + "" is not valid for this report."; } public const string INV_DIAG_RPT_INSUFFICIENT_INPUT = "Not enough data submitted to run the report. Please enter either an invoice number or all of the following: customer number, from date, and to date."; public const string NO_ROWS_SELECTED = "No data selected to be returned."; public const string TAX_EXTRACT_INSUFFICIENT_INPUT = "Not enough data submitted to run the report. Please enter either an invoice number or at least a from date and to date."; } } namespace my.project.Strings.TaxAdmin { public static class Keys { public const string INVOICE_NUMBER = "InvoiceNumber"; public const string DATE_FROM = "DateFrom"; public const string DATE_TO = "DateTo"; public const string ROW_NAME = "RowName"; public const string TAX_EXEMPTION_CUSTOMER_ID = "TaxExemptionCustomerID"; public const string TAX_NEXUS_ID = "TaxNexusID"; public const string TAX_EXEMPTION_REASON_ID = "TaxExemptionID"; public const string INSTANCE_NUM = "InstanceNum"; public const string CERTIFICATE_NUM = "Certificate Number"; public const string START_DATE = "StartDate"; public const string END_DATE = "EndDate"; public const string SURCHARGE_EXEMPT = "SurchargeExemptionInd"; public const string TO_EXCEL = "XLS"; } } using my.project.Strings.Generic; namespace my.project.Strings.TaxAdmin { public static class Views { public static string TaxExemptionCustomerRows { get { return Locations.VIEW_ROOT + "TaxAdmin/Partials/TaxExemptionCustomerRows.cshtml"; } } public static string TaxNexusRows { get { return Locations.VIEW_ROOT + "TaxAdmin/Partials/TaxNexusRows.cshtml"; } } } } 

I have also set up a my.project.Strings.Generic that has strings that generally every controller will use (Common error messages, Form keys that get used over and over again, CDN Urls, common file locations, etc…). One thing I like about this approach is that you get intellisense suggestions for all of your form keys. I guess I should also mention that I use these strings in my Views as well, which brings me to the one thing I don’t like about this. Had I hardcoded the strings in the view, when I see an issue with something in the webpage I could do a ctrl + f to find the text in the view where the issue was, this strategy isn’t as effective when your view is filled with a boatload of variable names.

I’m really just trying to figure out if this a better way of doing things. Does this cost more memory for the website since the strings are static and never go away, where as the hard coded strings would only exist for life of the request? Is this a good way to manage these strings? Is there a better way? Any constructive criticism would be appreciated.

submitted by /u/bonsall
[link] [comments]

Leave a Reply