Where do I put shared constants in a C# application? [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have some constant strings that are needed in multiple classes. How can I store and retrieve these constants in a way that meets these required criteria:
No duplication of information.
No function call to retrieve constants.
No namespace symbol prior to constant (IE: Constant not Namespace.Constant)
None of the solutions I've found so far meet every criteria:
Duplicate them in every class they are needed in (violates criteria #1).
Store them in a separate static class that is referenced wherever needed (a bit like a C-style header file, violates criteria #3).
Put them in one of the classes where they are used and reference that class when they are needed elsewhere (this is seems like a bad idea because it could create circular references, and it violates criteria #3).
Put them in app.config and retrieve them whenever they are needed (This seems to break style conventions since non-configuration data is stored in a config file, and a change to the config file could break application logic. It also violates criteria #2).
Is there a solution in C# that meets every criteria?

I use option #2 often, because #1 is rife with risk of different values, and #3 breaks normally-sought class encapsulation. If you must have globals. have one Single Source of Truth for them.
You can make the code somewhat cleaner with a using alias:
using static namespace.StaticClassName;

VB.Net has Module Statement that can be used for global variables that can be accessed without using the name of the module, but C# doesn't have anything like that.
In C# I use #2, but with const. That way the value is shown when you hover it in Visual Studio:

Related

Is a partial class internally different from a regular class? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Are partial classes any different from regular classes after compilation? Are there performance differences? Are there security differences? Is the generated CIL different? etc...
I couldn't find any information about this, all of the search results are spammed with when and how to use partial classes.
No, they are same according to this. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#partial-types
A type declaration can be split across multiple partial type declarations. The type declaration is constructed from its parts by following the rules in this section, whereupon it is treated as a single declaration during the remainder of the compile-time and run-time processing of the program.
No, those are exactly same. The compiler is just joining them files together at the build time.
The perfect use case for partial classes is extending of the auto-generated code. As an example, try to imagine generated DataSet class, that has been generated from .xsd file.
If you wish to extend it, you can use partial class.
Doing so won't remove your custom code when the DataSet is regenerated with Custom Tool.
I personally believe that splitting classes into partial classes only to reduce the sizes of files is wrong, and it's reducing readability of the code.

What is meant by outsiders? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I Started Learning C# and it confuses me with term 'outsiders'. Are outsiders some unauthorized people to our code?
It is not C# jargon, but it refers to any entity that is not the object.
In this specific case, outsiders might be the factory that creates the player or other entities of the game.
In general you want to grant access to specified resources only to a selected few. This maintains the code cleaner (as you force using specific accesses you appositely designed) and ensures the flow is followed (imagine that when setting the score you also want to update other variables of Player, if someone modified the variable directly, the side effects would be bypassed).
The whole situation becomes even more critical when you are writing a library for others: you want to encapsulate the internal variables as much as possible and not allow others to have "free access" to everything, as they might tamper with important stuff.
Outsiders is any other code outside of this object. So when you set variable as private, only code in this object can change it. That way you force any other code outside this class to modify score only by calling setScore method, and not directly accessing it.

Class structure that change without violating SOLID principle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my first question here so let me know if additional info is required...
I am new to designing so my knowledge is a little limited, so I have this application in which I have a project class, the project goes through 7 stages, and those stages have nothing in common...first one is Discovery, which contains question about the requirements, second is Product Mapping, which lets the user select products required by the project...
The problem comes, that stages keep on getting added or removed...
So I cant put their reference in the class, cuz then I need to modify the class every time. So how to design the flow?
Should I pass the project object into stage object? Then how to keep track on which stage the project is?
I think you should go for Process Manager Pattern.
Each of your stages should be separate class implementing a common Interface/Abstract class(according to your need) and then you need to have a Controller class which will control the work flow of your project. You can add or remove your steps to this Controller class instance as per your requirement.
You can track the "status" of the Project as Project.status, being an enum that advances through the appropriate stages. (This assumes that each stage exists only once; it can be re-executed, but would update/ overwrite the previous results.)
Data captured at each stage can be implemented as properties in the Project class (most simple), or (more advanced) "pushed down" into composite entities per-stage.
Don't forget also to take advantage of entities/objects to hold information like Requirement, Product Mapping etc!

Sharing xml and data between multiple projects [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have three projects(C# libraries) namely A,B,C.
All the 3 have 3-4 xml files(in general can be resources) associated with them.
Each of these projects have classes that access these files for settings and information.
(loading xmls when ever they need)
The problems is sometimes there is a need that a class in project C may need to access
resources(xml files,images etc) of project B and vice versa.
Also these files may or may not be a part of the project solution.These resource paths
can come from app.config etc.
Its really becoming tedious to work out how to centralise access to these resources so that
all three projects can access them uniformly.
Currently all the projects load the files using app.config.
Also i'm trying to minimise the number of times a xml is loaded.(ideally once).
But given the projects are different i have to load it again.
I thought of using a Singleton class as it would make more sense for making uniform access but haven't quiet figured out a way.
Anyone has come across similar situations?
Are there any design patterns or best practices for sharing resources across projects?
Create one library containing the class(es) that access your centralized XML settings, and reference that library from the other libraries.
You don't necessarily need a Singleton for this, but putting it in one place will allow you to focus your efforts on things to improve it later, possibly caching, etc.

String resource file naming schemes and management [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
A trivial question perhaps, but I'm interested in the answers. I'm currently refactoring some very large monolithic string resource files (one dumpster resource file per project, in about 30 projects). I'm splitting them such that we follow a convention for our files and make the strings easier to find and manage when coding.
Generally I'm splitting the files into this scheme:
ErrorMessages.resx
LogMessages.resx
ViewResources.resx
AppResources.resx
I'm not terribly thrilled with the naming, and I'm just wondering what other people use. For example, instead of AppResources (strings for internal use by the application), I've seen a lot of demo projects use StringResources, Internal (terrible!), etc.
Ideas/anecdotes/suggestions on managing resources or standard naming schemes are appreciated.
I generally structure my resources like this:
The first resource file is used by the entire application (e.g. Project.Core) and does include all sorts of widely used common strings. I actually don't make any difference between errors/exceptions and logging:
CommonResources.resx
Access modifier: Public
Error_Contexte.g. Error_ArgumentCannotBeNull
Warn_Contexte.g. Warn_ApplicationSettingNotFoundUseDefault
Info_Contexte.g. Info_UpdateAvailable
Validation_Contexte.g. Validation_EmailNotValid
The second resource file is used by the presentation layer and contains all sorts of UI strings. The naming can vary from project to project but generally it looks like the following schema:
PresentationResources.resx
Access modifier: Internal
Common_Contexte.g. Common_Yes
Section/Controller_Window/View_Contexte.g. Help_FAQ_HeadlineHowToUseResources or Help_FAQ_TextHowToUseResources
Finally every project/assembly does also have an internal resource file for Error/Warn/Info/Validation resources that are too specific to go in the CommonResources.resx file. I have to admit, that I mostly name this resource file InternalResources.cs ;)
InternalResources.resx
Access modifier: Internal
Classname_Error_Contexte.g. BCrypt_Error_InvalidSaltRevision
Classname_Warn_Context
Classname_Info_Context
Classname_Validation_Context

Categories

Resources