Exemple #1
0
        private async Task <TGGDBFContextType> GetContext(IGGDBFDataSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            //TODO: This could load the context multiple times due to race conditions (but should be ok)
            if (!GGDBFHelpers.IsContextInitialized <TGGDBFContextType>())
            {
                await GGDBFHelpers.CallInitialize <TGGDBFContextType>(source);
            }

            return(GGDBFHelpers.GetInstance <TGGDBFContextType>());
        }
        /// <inheritdoc />
        public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
            where TModelType : class
        {
            IEnumerable <TModelType> models = await RetrieveAllAsync <TModelType>(token);

            if (config == null)
            {
                throw new NotSupportedException($"TODO: Cannot support no config.");
            }
            else if (config.KeyResolutionFunction == null)
            {
                //TODO: This only supports simple primary keys
                var keyName = Context.Model
                              .FindEntityType(typeof(TModelType))
                              .FindPrimaryKey()
                              .Properties
                              .Select(x => x.Name)
                              .Single();

                config = new TableRetrievalConfig <TPrimaryKeyType, TModelType>(m => (TPrimaryKeyType)m.GetPropertyValue(keyName), config.TableNameOverride);
            }

            var    map     = new Dictionary <TPrimaryKeyType, TModelType>();
            var    version = GGDBFHelpers.ConvertToVersion(typeof(GGDBFTable <TPrimaryKeyType, TModelType>).Assembly.GetName().Version);
            string name    = string.IsNullOrWhiteSpace(config.TableNameOverride) ? typeof(TModelType).GetCustomAttribute <TableAttribute>(true).Name : config.TableNameOverride;

            foreach (var model in models)
            {
                map[config.KeyResolutionFunction(model)] = model;
            }

            return(new GGDBFTable <TPrimaryKeyType, TModelType>()
            {
                TableData = map,
                Version = version,
                TableName = name
            });
        }
Exemple #3
0
        /// <inheritdoc />
        public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default) where TModelType : class
        {
            TGGDBFContextType context = await GetContext(Source);

            try
            {
                //Total hack but the best way to get the data I guess.
                var modelDictionary = (IReadOnlyDictionary <TPrimaryKeyType, TModelType>) typeof(TGGDBFContextType)
                                      .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty)
                                      .First(p => p.PropertyType == typeof(IReadOnlyDictionary <TPrimaryKeyType, TModelType>))
                                      .GetValue(context);

                return(new GGDBFTable <TPrimaryKeyType, TModelType>()
                {
                    Version = GGDBFHelpers.GetContextVersion <TGGDBFContextType>(),
                    TableName = GGDBFHelpers.GetTableName <TModelType>(),
                    TableData = modelDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
                });
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Failed to {nameof(RetrieveFullTableAsync)} for Key: {typeof(TPrimaryKeyType)} Model: {typeof(TModelType)}", e);
            }
        }
Exemple #4
0
        public async Task ReloadAsync(CancellationToken token = default)
        {
            await Source.ReloadAsync(token);

            await GGDBFHelpers.CallInitialize <TGGDBFContextType>(Source);
        }