I had a similar requirement before and I stored resources in a .resx file but I have decided to use classes for storing the keys.
For example, say I have resources for banking withdrawal screen.
I have created a simple class on the lines of
public class BankingWithdrawalResource
{
public string WithdrawFunds { get; set; }
}
I then implemented a class for retrieving resources. My aim was to have a strongly typed way of pulling out resources. I also wanted to make resource retrieval unit testable. I've extracted an interface for retrieving resources: IResource
.
Each time I wanted to use resources, I would get an implementation through IoC container.
var resource = Dependency.Resolve<IResource>();
Finally, when it came to using the resource, I would have something on the lines of:
resource.For<BankingWithdrawalResource>(p => p.WithdrawFunds)
In above line, I used lambda expression that allowed me to view all available resources through intellisense.
When it came to unit testing, I would mock IResource
and set expectations on it.
For example:
resourceMock.Expect(a => a.For<BankingWithdrawalResource>(p => p.WithdrawFunds)).Repeat.Once();
Also notice that we have extracted an interface for resource retrieval. Because of this, we are now able to store resources anywhere we want. For example:
public class DatabaseResource : IResource
{
// Logic to retrieve resources from database
// Won't cause recompilation
}
public class XmlResource : IResource
{
// Logic to retrieve resources from xml files
}
public class DefaultResource : IResource
{
// Will cause recompilation as resources are stored in .resx
}
I wrote above from top of my head in notepad, so if you have any problems let me know and I'll add implementation later.