Example #1
0
        public virtual Type FindViewModelByAttribute <T>(IModel data, Type[] typesToSearch = null) where T : IModelAttribute
        {
            _logger.Debug($"called FindViewModelByAttribute with typesToSearch {typesToSearch}");
            //Anyway to speed this up? Better than just a straight up loop?
            typesToSearch = typesToSearch ?? viewModels.Where(x => x.Key is T).Select(x => x.Value).ToArray();
            _logger.Debug($"using typesToSearch {String.Join(",", typesToSearch.Select(t => t.FullName))}");
            foreach (var type in typesToSearch)
            {
                T modelAttr = _resolver.GetCustomAttribute <T>(type);
                if (modelAttr != null)
                {
                    //modelAttr.ViewModelFactory = this;
                    if (modelAttr.IsMatch(data, _keyProvider.GetViewModelKey(data)))
                    {
                        _logger.Debug($"returning type {type.FullName}");
                        return(type);
                    }
                }
            }
            if (_configuration.UseDefaultViewModels)
            {
                if (data is IPage)
                {
                    _logger.Debug("no viewmodel found, using default page viewmodel " + typeof(DefaultPage).FullName);
                    return(typeof(DefaultPage));
                }
            }
            ViewModelTypeNotFoundException e = new ViewModelTypeNotFoundException(data);

            _logger.Warning($"Could not find a valid ViewModel for item {e.Message}");
            throw e;
        }
        public Type FindViewModelByAttribute <T>(IModel data, Type[] typesToSearch = null) where T : IModelAttribute
        {
            //Anyway to speed this up? Better than just a straight up loop?
            typesToSearch = typesToSearch ?? viewModels.Where(x => x.Key is T).Select(x => x.Value).ToArray();
            foreach (var type in typesToSearch)
            {
                T modelAttr = resolver.GetCustomAttribute <T>(type);

                if (modelAttr != null && modelAttr.IsMatch(data, keyProvider.GetViewModelKey(data)))
                {
                    return(type);
                }
            }
            throw new ViewModelTypeNotFoundException(data);
        }
Example #3
0
 public bool IsMatch(IViewModelData data, IViewModelKeyProvider provider)
 {
     var key = provider.GetViewModelKey(data);
     return IsMatch(data, key);
 }