Is it advisable to use a .resx file to store common string references in a class library?

Can I use a .resx file to store shared string references in a business-level class library? I usually only see that they were used at the presentation level.

+4
source share
4 answers

One option is to have a separate project in your solution that has all the resx files for your entire solution. You can then add it as a link to your business layer. In the Resources project, you can write a wrapper around the .net ResourceManager class to return the resource value for your key. Something in the lines:

public class ResourceService : IResourceService { public ResourceService() {} public GetResourceValue(string resourceFileName, string resourceKey) { var resourceManager = new ResourceManager("Myresources", Assembly.Load("MyResourcesProjectName")); return resourceManager.GetString(resourceKey); } } 

Then you can use it from your business layer like:

 var resourceService = new ResourceService(); var resourceValue = resourceService.GetResourceValue("MyResources", "ResourceKeyName"); 

I did not have time to check the code, I wrote "on the fly", but it should give you a general idea. IMO, there is nothing wrong with resx files in a separate project. Another way to approach it is to have the .resx file in the places where they are used. I find a separate project idea better, because in this way you can create a separate folder for each type of language and store local .resx files in it.

+3
source

It’s best to try moving the lines that the user sees at the presentation level, but it’s not uncommon to create some messages at the business level. If you plan to translate messages into several languages ​​/ cultures, then it is advisable to put them in a resx file.

+2
source

I do not see any problems with placing lines facing the user in the resx file at any level.

those. if your business logic has something like CreateGreatingMail(User user, CultureInfo language) than you will have a few localization strings that you need to put somewhere, and resx is the best place.

To share user interface strings, you can have an explicit user interface level assembly designed exclusively for user interface strings, and not to merge it into a common business layer.

+2
source

Yes, the Exaclty .resx file is placed on the Presentation Layer .

But you can set constant

 const string toto = "test"; 
0
source

Source: https://habr.com/ru/post/1434030/


All Articles