Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

JsonLocalization: MvxTextProviderBuilder virtual call in constructor #137

Open
LRP-sgravel opened this issue Sep 20, 2016 · 1 comment
Open

Comments

@LRP-sgravel
Copy link

In it's constructor, MvxTextProviderBuilder calls its LoadResources method which refers to the virtual property ResourceFiles. This is an issue as we might access stuff that isn't completely initialized yet, as we well know.

My specific use-case is that I'm passing my default text type key as a parameter to my text provider class. I also use reflection to find all ViewModels and create the mappings for those classes. Of course, my default type key is null when trying to setup the dictionary. Ideally, the initial loading of text resources would be deferred.

        public TextProviderBuilder(string textFolder, string defaultTypeKey)
            : base(Mvx.Resolve<ResourceLocator>().ResourcesNamespace, textFolder, new MvxEmbeddedJsonDictionaryTextProvider(false))
        {
            _DefaultTypeKey = defaultTypeKey;
        }

        protected override IDictionary<string, string> ResourceFiles
        {
            get
            {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                IEnumerable<TypeInfo> localizedViewModelTypes = Mvx.Resolve<ResourceLocator>()
                                                                   .ResourcesAssembly.DefinedTypes.Where(
                                                                       t => t.IsSubclassOf(typeof(MvxLocalizedViewModel)));

                dictionary.Add(_DefaultTypeKey, _DefaultTypeKey);

                foreach(TypeInfo type in localizedViewModelTypes)
                {
                    string typeKey = type.FullName;

                    dictionary.Add(typeKey, typeKey);
                }

                return dictionary;
            }
        }
@LRP-sgravel
Copy link
Author

Workaround :

        public TextProviderBuilder(string textFolder, string defaultTypeKey)
            : base(Mvx.Resolve<ResourceLocator>().ResourcesNamespace, textFolder, new MvxEmbeddedJsonDictionaryTextProvider(false))
        {
            _DefaultTypeKey = defaultTypeKey;
            LoadResources(string.Empty);
        }

        protected override IDictionary<string, string> ResourceFiles
        {
            get
            {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                IEnumerable<TypeInfo> localizedViewModelTypes = Mvx.Resolve<ResourceLocator>()
                                                                   .ResourcesAssembly.DefinedTypes.Where(
                                                                       t => t.IsSubclassOf(typeof(MvxLocalizedViewModel)));

                if (!string.IsNullOrEmpty(_DefaultTypeKey))
                {
                    dictionary.Add(_DefaultTypeKey, _DefaultTypeKey);
                }

                foreach(TypeInfo type in localizedViewModelTypes)
                {
                    string typeKey = type.FullName;

                    dictionary.Add(typeKey, typeKey);
                }

                return dictionary;
            }
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant