4

Is it advisable to use a .resx file to store common string references in a business layer class library? I typically only ever see these been used in the presentation layer.

fin
  • 1,275
  • 4
  • 18
  • 34

4 Answers4

3

One option to be to have a separate project in your solution, which has all the resx files for your whole solution. You can then add it as a reference to your business layer. In the Resources Project, you can write a wrapper around .net ResourceManager class, to return the resource value for your key. Something on the lines of:

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 you business layer as:

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

I did not have time to test the code, I have written on the fly, but it should give you the general idea. IMO, there is nothing wrong in having the resx files in a separate project. Another way of approaching it, is to have .resx file in places where they are used. I find the separate project idea better, because that way, you can create separate folder for each type of language, and store locale specific .resx files in it.

tranceporter
  • 2,241
  • 1
  • 21
  • 23
2

It's best to try to move strings that the user will see up to the presentation layer, but it isn't uncommon to have to build some messages in the business layer. If you are planning on translating the messages to multiple languages/cultures then it is appropriate to put them in a resx file.

Brian
  • 37,399
  • 24
  • 94
  • 109
2

I see no problem with putting user-facing strings into resx file on any layer.

I.e. if you business logic have something like CreateGreatingMail(User user, CultureInfo language) than you will have multiple user-facing localize-able strings that you need to put somewhere and resx is the best place.

For sharing UI strings you can have explicit UI layer assembly solely dedicated to UI strings instead of merging it into shared business layer.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Yes Exaclty .resx file is placed on Presentation Layer.

But you can set constant

const string toto = "test";
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • So, where would one store common string references in a business layer? As a const member of the relevant class? What if the particular business reason requires the use many string references - how would we prevent these from been scattered through many classes. It would be nice to have them centeralized in one location such as a .resx file. – fin Sep 13 '12 at 15:58
  • 1
    @fin can't you centralize them as normal constants in a class? ResX files are meant for localization, and translations are very context-sensitive -- so it usually doesn't make sense to compile them in a shared library. – McGarnagle Sep 13 '12 at 16:05
  • The functionnzl constants are not made for visual need, so you don't need to save in resx file. – Aghilas Yakoub Sep 13 '12 at 18:07