Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Wire a Configuration to a Service
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Arne Vajhøj  
View profile  
 More options Nov 19 2012, 9:38 pm
Newsgroups: microsoft.public.dotnet.languages.csharp
From: Arne Vajhøj <a...@vajhoej.dk>
Date: Mon, 19 Nov 2012 21:38:23 -0500
Local: Mon, Nov 19 2012 9:38 pm
Subject: Re: Wire a Configuration to a Service
On 11/15/2012 6:59 AM, Shapper wrote:

> I am creating a translator as follows:

>    public interface ITranslator {
>      String Translate(String text, String fromCulture, String toCulture);
>    } // ITranslator

> Basically, this translates a string from one culture to another.

> This is working in a different way from something I asked sometime ago in this newsgroup. I hope I am able to get some advice on this version.

> I have a class which holds groups of translations:

>    public class TranslatorProviderBase {
>      public List<Dictionary<String, String>> Table { get; private set; }
>      public TranslatorProviderBase() {
>        Table = new List<Dictionary<String, String>>();
>      }
>      public void Add(Dictionary<String, String> translations) {
>        Table.Add(translations);
>      }
>    } // TranslatorProviderBase

> Using this class as base I can place translations in different parts of my application:

>    public class TranslatorProvider : TranslatorProviderBase {
>      public TranslatorProvider() {
>        Add(new Dictionary<String, String> { { "en", "Hello" }, { "pt", "Ol " }, { "fr", "Bonjour" } });
>        Add(new Dictionary<String, String> { { "en", "Bye" }, { "pt", "Adeus" }, { "fr", "Adieu" } });
>      }
>    } // TranslationProvider

I do not see any reason why to extend the class here instead of just
having the client code Add to TranslatorProviderBase (which in that case
probably should just be called TranslatorProvider).

> I created a class to "save" the providers and the translator configuration:

>    public class TranslatorConfiguration {
>      public IList<String> Cultures { get; private set; }
>      public IList<TranslatorProviderBase> Providers { get; private set; }
>      public TranslatorConfiguration() {
>        Cultures = new List<String>();
>        Providers = new List<TranslatorProviderBase>();
>      }
>      public void AddProvider(TranslatorProviderBase provider) {
>        Providers.Add(provider);
>      }
>      public void AddProvider<T>() where T : TranslatorProviderBase, new() {
>        Providers.Add(new T());
>      }
>      public void SetValidCultures(params String[] cultures) {
>        Cultures = cultures.ToList();
>      }
>    } // TranslatorConfiguration

What value does this class have? If none then get rid of it.

> I have a class through which I configure the translator:

> public class TranslatorManager {
>    public static void Initialize(Action<TranslatorConfiguration> configuration) {
>       configuration.Invoke(new TranslatorConfiguration());
>    } // Initialize
> } // TranslatorManager

> NOTE: This class will have other methods like AssertTranslatorConfigurationIsValid, etc.

> The translator setup and usage would be something like this:

>    TranslatorManager.Initialize(x => {
>      x.AddProvider<TranslatorProvider>();
>      x.SetValidCultures("en", "pt", "fr");
>    });
>    ITranslator translator = new Translator();
>    String adeus = translator.Translate("Bye", "en", "pt");

> PROBLEMS:

> 1 - My main problem, at the moment, is how to wire the Translator, implementation of ITranslator, to the TranslatorConfiguration so I can access providers and values in Translator.

> 2 - I think I should also allow to register the container in configuration and have some kind of factory to wire things ... But I am not sure if yes and how.

> My Translator, implementation of ITranslator, is the following:

>    public class Translator : ITranslator {

>      public String Translate(String text, String fromCulture, String toCulture) {

>        foreach (TranslatorProviderBase provider in TranslatorConfiguration.Providers) {

>          Dictionary<String, String> row = provider.Table.First(x => x.Any(y => y.Key == fromCulture && y.Value == text));
>          if (row == null)
>            return null;

>          String translation;
>          if (row.TryGetValue(toCulture, out translation))
>            return translation;

>        }
>        return String.Empty;
>      } // Translate

>    } // Translate

> See how I am using "TranslatorConfiguration.Providers" in my foreach loop.

> Of course this does not work unless I make TranslatorConfiguration as static as well as its methods and properties.

> But then I get the following errors in TranslatorManager:

>    'TranslatorConfiguration': static types cannot be used as type arguments

>    Cannot create an instance of the static class

> I have seen a few libraries 'working' this way but I am not sure how to wire the configuration values (TranslatorConfiguration) to the service (Translator).

You translator needs to get an IList<TranslatorProviderBase> in as
constructor argument or via setter, save it in a field and then
just use it.

Arne


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.