Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damion 48 posts 240 karma points
    Apr 16, 2019 @ 13:00
    Damion
    0

    using localization service GetAllLanguages from a component composer, incorrect DI?

    I have an interface as below, which I use to add a specific language if it does not exist:

        public interface IGetLanguagesService
        {
            void GetLanguages(ILocalizationService localization);
        }
    
    
        public class LanguageService : IGetLanguagesService
        {
            ILocalizationService _localizationService;
            public void GetLanguages(ILocalizationService localization)
            {
                _localizationService = localization;
                var currentLanguages = _localizationService.GetAllLanguages();
                bool exists = false;
                foreach (var currentLan in currentLanguages)
                {
                    if (currentLan.IsoCode == "es-ES")
                    {
                        exists = true;
                    }
                }
                if (!exists)
                {
                    AddLanguage(_localizationService);
                }
            }
    
            public void AddLanguage(ILocalizationService localization)
            {
                var languageSE = new Language("es-ES") { CultureName = "es-ES", IsMandatory = true };
                localization.Save(languageSE);
            }
        }
    

    I want to use this at start-up so have created a component composer, which on Initialize() I want to call CallGetLanguages() but Im not entirely sure what should be in Initialize(), I think my DI may be wrong?

    public class LanguagesComposer : ComponentComposer<LanguagesComponent>
        {
            public void Compose(Composition composition)
            {
                composition.Register<IGetLanguagesService>(Lifetime.Singleton);
                composition.Register<ILocalizationService>(Lifetime.Singleton);
    
                composition.Components().Append<LanguagesComponent>();
            }
        }
    
        public class LanguagesComponent : IComponent
        {
            public void Initialize()
            {
               ???????
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
    
            IGetLanguagesService _getLanguagesService;
            ILocalizationService _localization;
            public void CallGetLanguages(IGetLanguagesService getLanguages, ILocalizationService localization)
            {
                _getLanguagesService = getLanguages;
                _localization = localization;
                _getLanguagesService.GetLanguages(localization);
            }
        }
    
  • Dave Woestenborghs 3203 posts 10697 karma points MVP 4x admin c-trib
    Apr 16, 2019 @ 13:14
    Dave Woestenborghs
    100

    Hi Damion,

    I think you need to inject the localization service into the constructor of your class.

        public interface IGetLanguagesService
            {
                void GetLanguages();
            }
    
    
            public class LanguageService : IGetLanguagesService
            {
                private readonly ILocalizationService _localizationService;
    
    public LanguageService(ILocaliztionService localizationService)
    {
    _localizationService = localizationService;
    }
                public void GetLanguages()
                {
                     var currentLanguages = _localizationService.GetAllLanguages();
                    bool exists = false;
                    foreach (var currentLan in currentLanguages)
                    {
                        if (currentLan.IsoCode == "es-ES")
                        {
                            exists = true;
                        }
                    }
                    if (!exists)
                    {
                        AddLanguage();
                    }
                }
    
                public void AddLanguage()
                {
                    var languageSE = new Language("es-ES") { CultureName = "es-ES", IsMandatory = true };
                    _localizationService.Save(languageSE);
                }
            }
    

    Dave

  • Damion 48 posts 240 karma points
    Apr 16, 2019 @ 13:17
    Damion
    0

    Ah I see, thanks will try that now

Please Sign in or register to post replies

Write your reply to:

Draft