Fill Dictionary in a Fluent Way

S

Shapper

Hello,

I have the following translation dictionary:

public class TranslationDictionary : Dictionary<String, Dictionary<String, String>> { }

A "row" of the translation dictionary might become something as:

new Dictionary<String, Dictionary<String, String>>() { { "Hello", new Dictionary<String, String>() { { "Hello", "en" }, { "Bounjor", "fr" }, { "Olá", "pt" } } }

Note that the first item to be added is also key, e.g, "Hello" in english ("en") is set as the key of that row. However, I would like to fill the Dictionary in a Fluent Way:

translations.For("Hello", "en").Add("Bounjour", "fr").Add("Olá", "pt");

For this I created a TranslationProviderBase class and a TranslationBuilder(Fluent Helper):

public class TranslationBuilder {

private readonly TranslationDictionary _translations;
private readonly String _key;

public TranslationBuilder(String key, TranslationDictionary translations) {
_translations = translations;
_key = key;
}

public TranslationBuilder Add(String text, String culture) {

IDictionary<String, String> filtered;
if (!_translations.TryGetValue(_key, out filtered))
_translations.Add(_key, translations);

return this;
}
}

public class TranslationProviderBase {

public TranslationDictionary Translations { get; private set; }

public TranslationProviderBase() {
Translations = new TranslationDictionary();
}

public TranslationBuilder For(String text, String culture) {

Translations.Add(text, new Dictionary<String, String>() { { text, culture } });

return new TranslationBuilder(text, Translations);
}

}

This is not working ... I am having problems in creating it.

I intend to use TranslationProviderBase class as a base class for RouteTranslationProvider cass, MessageTranslationProvider class and so on ...

Could someone, please, help me out?

Thank You,

Miguel
 
S

Shapper

Hello,



I have the following translation dictionary:



public class TranslationDictionary : Dictionary<String, Dictionary<String, String>> { }



A "row" of the translation dictionary might become something as:



new Dictionary<String, Dictionary<String, String>>() { { "Hello", new Dictionary<String, String>() { { "Hello", "en" }, { "Bounjor", "fr" }, { "Olá", "pt" } } }



Note that the first item to be added is also key, e.g, "Hello" in english("en") is set as the key of that row. However, I would like to fill the Dictionary in a Fluent Way:



translations.For("Hello", "en").Add("Bounjour", "fr").Add("Olá", "pt");



For this I created a TranslationProviderBase class and a TranslationBuilder (Fluent Helper):



public class TranslationBuilder {



private readonly TranslationDictionary _translations;

private readonly String _key;



public TranslationBuilder(String key, TranslationDictionary translations) {

_translations = translations;

_key = key;

}



public TranslationBuilder Add(String text, String culture) {



IDictionary<String, String> filtered;

if (!_translations.TryGetValue(_key, out filtered))

_translations.Add(_key, translations);



return this;

}

}



public class TranslationProviderBase {



public TranslationDictionary Translations { get; private set; }



public TranslationProviderBase() {

Translations = new TranslationDictionary();

}



public TranslationBuilder For(String text, String culture) {



Translations.Add(text, new Dictionary<String, String>() { { text, culture } });



return new TranslationBuilder(text, Translations);

}



}



This is not working ... I am having problems in creating it.



I intend to use TranslationProviderBase class as a base class for RouteTranslationProvider cass, MessageTranslationProvider class and so on ...



Could someone, please, help me out?



Thank You,



Miguel

Hello,

I just solved this ...

Thank You,
Miguel
 
A

Arne Vajhøj

I have the following translation dictionary:

public class TranslationDictionary : Dictionary<String, Dictionary<String, String>> { }

A "row" of the translation dictionary might become something as:

new Dictionary<String, Dictionary<String, String>>() { { "Hello", new Dictionary<String, String>() { { "Hello", "en" }, { "Bounjor", "fr" }, { "Olá", "pt" } } }

Note that the first item to be added is also key, e.g, "Hello" in english ("en") is set as the key of that row. However, I would like to fill the Dictionary in a Fluent Way:

translations.For("Hello", "en").Add("Bounjour", "fr").Add("Olá", "pt");

See my reply to the previous thread.

Arne
 
R

RayLopez99

Could someone, please, help me out?

I would not store as a value in a dictionary yet another dictionary--too complicated and probably too slow behind the scenes. It's like recursion--break it down into a series of steps and use a stack to eliminate recursion, and your program will be simpler to maintain as well as run faster (less memory overhead, etc etc).

RL
 
A

Arne Vajhøj

I would not store as a value in a dictionary yet another
dictionary--too complicated and probably too slow behind the scenes.

A two level Dictionary look up is still very fast.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top