/// <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
            });
        }
 /// <inheritdoc />
 public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
     where TModelType : class
     where TSerializableModelType : class, TModelType, IGGDBFSerializable
 {
     //EF Core doesn't support the concept of serializable subtypes.
     //So we can only return the actual model type.
     return(await RetrieveFullTableAsync <TPrimaryKeyType, TModelType>(config, token));
 }
 public static async Task <IReadOnlyDictionary <TPrimaryKeyType, TModelType> > RetrieveTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(this IGGDBFDataSource source, TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
     where TModelType : class
     where TSerializableModelType : class, TModelType, IGGDBFSerializable
 {
     return((await source.RetrieveFullTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(config, token)).ToReadOnly());
 }
Exemplo n.º 4
0
 /// <inheritdoc />
 public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
     where TModelType : class
 {
     return(await DataSource.RetrieveFullTableAsync(config, token));
 }
Exemplo n.º 5
0
        /// <inheritdoc />
        public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
            where TModelType : class
            where TSerializableModelType : class, TModelType, IGGDBFSerializable
        {
            var table = await DataSource.RetrieveFullTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(config, token);

            return(new GGDBFTable <TPrimaryKeyType, TModelType>()
            {
                Version = table.Version,
                TableName = table.TableName,
                TableData = table.TableData
                            .ToDictionary(t => t.Key, t => (TModelType)Converter.Convert <TModelType, TSerializableModelType>(t.Value))
            });
        }
Exemplo n.º 6
0
        /// <inheritdoc />
        public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
            where TModelType : class
            where TSerializableModelType : class, TModelType, IGGDBFSerializable
        {
            await ReloadIfRequiredAsync(token);

            var table = await CreateServiceClient()
                        .RetrieveTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(typeof(TPrimaryKeyType).AssemblyQualifiedName, typeof(TModelType).AssemblyQualifiedName, typeof(TSerializableModelType).AssemblyQualifiedName, token);

            return(table.ConvertFrom <TPrimaryKeyType, TModelType, TSerializableModelType>());
        }
Exemplo n.º 7
0
        private TableRetrievalConfig <TPrimaryKeyType, TSerializableModelType> CastConfig <TPrimaryKeyType, TModelType, TSerializableModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config)
            where TModelType : class
            where TSerializableModelType : class, TModelType, IGGDBFSerializable
        {
            if (config == null)
            {
                return(null);
            }

            Func <TSerializableModelType, TPrimaryKeyType> keyResolutionFuncCasted = (config.KeyResolutionFunction != null) ? m => config.KeyResolutionFunction(m) : null;

            return(new TableRetrievalConfig <TPrimaryKeyType, TSerializableModelType>(keyResolutionFuncCasted, config?.TableNameOverride));
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType, TSerializableModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
            where TModelType : class
            where TSerializableModelType : class, TModelType, IGGDBFSerializable
        {
            var table = await RetrieveFullTableAsync <TPrimaryKeyType, TSerializableModelType>(CastConfig <TPrimaryKeyType, TModelType, TSerializableModelType>(config), token);

            return(table.ConvertFrom <TPrimaryKeyType, TModelType, TSerializableModelType>());
        }
Exemplo n.º 9
0
        /// <inheritdoc />
        public async Task <GGDBFTable <TPrimaryKeyType, TModelType> > RetrieveFullTableAsync <TPrimaryKeyType, TModelType>(TableRetrievalConfig <TPrimaryKeyType, TModelType> config = null, CancellationToken token = default)
            where TModelType : class
        {
            string path = WriteToCurrentDirectory
                                ? Path.Combine(Directory.GetCurrentDirectory(), $"{config.TableNameOverride}.{GGDBFConstants.FILE_EXTENSION_SUFFIX}")
                                : Path.Combine(OutputPath, $"{config.TableNameOverride}.{GGDBFConstants.FILE_EXTENSION_SUFFIX}");

            using FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

            var buffer = ArrayPool <byte> .Shared.Rent((int)fs.Length);

            try
            {
                await fs.ReadAsync(buffer, 0, (int)fs.Length, token);

                return(Serializer.Deserialize <TPrimaryKeyType, TModelType>(buffer, 0, (int)fs.Length));
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
Exemplo n.º 10
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);
            }
        }