sdk/dotnet/src/Microsoft.Datasync.Client/LibraryExtensions.cs
/// <summary>
/// Defines a table for use with offline sync.
/// </summary>
/// <typeparam name="T">The type of entity stored in the table.</typeparam>
/// <param name="store">The offline store.</param>
/// <param name="tableName">The name of the table.</param>
/// <param name="settings">The serializer settings.</param>
Defines a table for use with offline sync.
Defines a table for use with offline sync .
public static void DefineTable<T>(this AbstractOfflineStore store, string tableName, DatasyncSerializerSettings settings)
        {
            if (settings.ContractResolver.ResolveContract(typeof(T)) is not JsonObjectContract contract)
            {
                throw new ArgumentException($"The generic type '{typeof(T).Name}' is not an object");
            }
            if (contract.DefaultCreator == null)
            {
                throw new ArgumentException($"The generic type '{typeof(T).Name}' does not have a parameterless constructor.");
            }
            object definition = contract.DefaultCreator();
            foreach (JsonProperty contractProperty in contract.Properties)
            {
                if (contractProperty.PropertyType.GetTypeInfo().IsEnum)
                {
                    object firstValue = Enum.GetValues(contractProperty.PropertyType).Cast<object>().FirstOrDefault();
                    if (firstValue != null)
                    {
                        contractProperty.ValueProvider.SetValue(definition, firstValue);
                    }
                }
            }
            string json = JsonConvert.SerializeObject(definition, settings);
            JObject jsonDefinition = JsonConvert.DeserializeObject<JObject>(json, settings);
            jsonDefinition[SystemProperties.JsonIdProperty] = string.Empty;
            foreach (JProperty prop in jsonDefinition.Properties().Where(i => i.Value.Type == JTokenType.Null))
            {
                JsonProperty cprop = contract.Properties[prop.Name];
                if (cprop.PropertyType == typeof(string) || cprop.PropertyType == typeof(Uri))
                {
                    jsonDefinition[prop.Name] = string.Empty;
                }
                else if (cprop.PropertyType == typeof(byte[]))
                {
                    jsonDefinition[prop.Name] = Array.Empty<byte>();
                }
                else if (cprop.PropertyType.GetTypeInfo().IsGenericType && cprop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    jsonDefinition[prop.Name] = new JValue(Activator.CreateInstance(cprop.PropertyType.GenericTypeArguments[0]));
                }
                else
                {
                    jsonDefinition[prop.Name] = new JObject();
                }
            }
            store.DefineTable(tableName, jsonDefinition);
        }
public static void DefineTable < T > ( this AbstractOfflineStore store , string tableName , DatasyncSerializerSettings settings ) { if ( settings . ContractResolver . ResolveContract ( typeof ( T ) ) is not JsonObjectContract contract ) { throw new ArgumentException ( $" The generic type ' { typeof ( T ) . Name } ' is not an object " ) ; } if ( contract . DefaultCreator == null ) { throw new ArgumentException ( $" The generic type ' { typeof ( T ) . Name } ' does not have a parameterless constructor. " ) ; } object definition = contract . DefaultCreator ( ) ; foreach ( JsonProperty contractProperty in contract . Properties ) { if ( contractProperty . PropertyType . GetTypeInfo ( ) . IsEnum ) { object firstValue = Enum . GetValues ( contractProperty . PropertyType ) . Cast < object > ( ) . FirstOrDefault ( ) ; if ( firstValue != null ) { contractProperty . ValueProvider . SetValue ( definition , firstValue ) ; } } } string json = JsonConvert . SerializeObject ( definition , settings ) ; JObject jsonDefinition = JsonConvert . DeserializeObject < JObject > ( json , settings ) ; jsonDefinition [ SystemProperties . JsonIdProperty ] = string . Empty ; foreach ( JProperty prop in jsonDefinition . Properties ( ) . Where ( i => i . Value . Type == JTokenType . Null ) ) { JsonProperty cprop = contract . Properties [ prop . Name ] ; if ( cprop . PropertyType == typeof ( string ) || cprop . PropertyType == typeof ( Uri ) ) { jsonDefinition [ prop . Name ] = string . Empty ; } else if ( cprop . PropertyType == typeof ( byte [ ] ) ) { jsonDefinition [ prop . Name ] = Array . Empty < byte > ( ) ; } else if ( cprop . PropertyType . GetTypeInfo ( ) . IsGenericType && cprop . PropertyType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) ) { jsonDefinition [ prop . Name ] = new JValue ( Activator . CreateInstance ( cprop . PropertyType . GenericTypeArguments [ 0 ] ) ) ; } else { jsonDefinition [ prop . Name ] = new JObject ( ) ; } } store . DefineTable ( tableName , jsonDefinition ) ; }
Defines a table for use with offline sync.
Defines a table for use with offline sync .
// Set enum values to their default. // Create a JObject out of the definition. // Ensure we have an ID field. // Set anything with a null in it to the appropriate type.
AbstractOfflineStore
this
tableName
string
The name of the table.
The name of the table .
settings
DatasyncSerializerSettings
The serializer settings.
The serializer settings .
store
The offline store.
The offline store .
typeparam
The type of entity stored in the table.
The type of entity stored in the table .