示例#1
0
        /// <summary>
        /// Configures Dommel JSON support using the specified <see cref="DommelJsonOptions"/>.
        /// </summary>
        /// <param name="options"></param>
        public static void AddJson(DommelJsonOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.EntityAssemblies == null || options.EntityAssemblies.Length == 0)
            {
                throw new ArgumentException("No entity assemblies specified.", nameof(options));
            }

            // Add SQL builders with JSON value support
            DommelMapper.AddSqlBuilder("sqlconnection", new SqlServerSqlBuilder());
            DommelMapper.AddSqlBuilder("sqlceconnection", new SqlServerCeSqlBuilder());
            DommelMapper.AddSqlBuilder("sqliteconnection", new SqliteSqlBuilder());
            DommelMapper.AddSqlBuilder("npgsqlconnection", new PostgresSqlBuiler());
            DommelMapper.AddSqlBuilder("mysqlconnection", new MySqlSqlBuilder());

            // Add a custom SqlExpression<T> factory with JSON support
            DommelMapper.SqlExpressionFactory = (type, sqlBuilder) =>
            {
                if (!(sqlBuilder is IJsonSqlBuilder))
                {
                    throw new InvalidOperationException($"The specified SQL builder type should be assignable from {nameof(IJsonSqlBuilder)}.");
                }

                var sqlExpression = typeof(JsonSqlExpression <>).MakeGenericType(type);
                return(Activator.CreateInstance(sqlExpression, sqlBuilder));
            };

            // Add a Dapper type mapper with JSON support for
            // properties annotated with the [JsonData] attribute.
            var jsonTypeHander = options.JsonTypeHandler?.Invoke() ?? new JsonObjectTypeHandler();
            var jsonTypes      = new List <Type>();

            foreach (var assembly in options.EntityAssemblies)
            {
                foreach (var type in assembly.ExportedTypes)
                {
                    foreach (var property in type.GetRuntimeProperties())
                    {
                        var jsonAttr = property.GetCustomAttribute <JsonDataAttribute>();
                        if (jsonAttr != null)
                        {
                            SqlMapper.AddTypeHandler(property.PropertyType, jsonTypeHander);
                            jsonTypes.Add(property.PropertyType);
                        }
                    }
                }
            }

            // Set a property resolver which considers the types discovered above
            // as primitive types so they will be used in insert and update queries.
            DommelMapper.SetPropertyResolver(new JsonPropertyResolver(jsonTypes));
        }
示例#2
0
 public JsonSqlExpression(IJsonSqlBuilder sqlBuilder, DommelJsonOptions options) : base(sqlBuilder)
 {
     _options = options;
 }