Exemplo n.º 1
0
        /// <summary>
        /// Initialize<br/>
        /// 初始化<br/>
        /// </summary>
        public DapperEntityMappings(
            IEnumerable <IDatabaseInitializeHandler> handlers,
            IEnumerable <IEntityMappingProvider> providers)
        {
            Mappings = new ConcurrentDictionary <Type, IDapperEntityMapping>();
            // Build entity mappings
            var entityProviders = providers
                                  .GroupBy(p => ReflectionUtils.GetGenericArguments(
                                               p.GetType(), typeof(IEntityMappingProvider <>))[0])
                                  .ToList();

            foreach (var group in entityProviders)
            {
                var builder = Activator.CreateInstance(
                    typeof(DapperEntityMappingBuilder <>).MakeGenericType(group.Key),
                    handlers, group.AsEnumerable());
                Mappings[group.Key] = (IDapperEntityMapping)builder;
            }
            // Setup dommel mappings
            FluentMapper.Initialize(config => {
                var addMap = config.GetType().FastGetMethod(nameof(config.AddMap));
                foreach (var mapping in Mappings)
                {
                    addMap.MakeGenericMethod(mapping.Key).FastInvoke(config, mapping.Value);
                }
                config.ForDommel();
                DommelMapper.SetPropertyResolver(new ZKWebPropertyResolver());
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize<br/>
        /// 初始化<br/>
        /// </summary>
        public DapperEntityMappings()
        {
            Mappings = new ConcurrentDictionary <Type, IDapperEntityMapping>();
            // Build entity mappings
            var providers   = Application.Ioc.ResolveMany <IEntityMappingProvider>();
            var entityTypes = providers
                              .Select(p => ReflectionUtils.GetGenericArguments(
                                          p.GetType(), typeof(IEntityMappingProvider <>))[0])
                              .Distinct().ToList();

            foreach (var entityType in entityTypes)
            {
                var builder = Activator.CreateInstance(
                    typeof(DapperEntityMappingBuilder <>).MakeGenericType(entityType));
                Mappings[entityType] = (IDapperEntityMapping)builder;
            }
            // Setup dommel mappings
            FluentMapper.Initialize(config => {
                var addMap = config.GetType().FastGetMethod(nameof(config.AddMap));
                foreach (var mapping in Mappings)
                {
                    addMap.MakeGenericMethod(mapping.Key).FastInvoke(config, mapping.Value);
                }
                config.ForDommel();
                DommelMapper.SetPropertyResolver(new ZKWebPropertyResolver());
            });
        }
 /// <summary>
 /// Configures the specified configuration for Dapper.FluentMap.Dommel.
 /// </summary>
 /// <param name="config">The Dapper.FluentMap configuration.</param>
 /// <returns>The Dapper.FluentMap configuration.</returns>
 public static FluentMapConfiguration ForDommel(this FluentMapConfiguration config)
 {
     DommelMapper.SetColumnNameResolver(new DommelColumnNameResolver());
     DommelMapper.SetKeyPropertyResolver(new DommelKeyPropertyResolver());
     DommelMapper.SetTableNameResolver(new DommelTableNameResolver());
     DommelMapper.SetPropertyResolver(new DommelPropertyResolver());
     return(config);
 }
Exemplo n.º 4
0
 public Repository()
 {
     DommelMapper.SetTableNameResolver(new DefaultTableNameResolver());
     DommelMapper.SetKeyPropertyResolver(new DefaultKeyPropertyResolver());
     DommelMapper.SetColumnNameResolver(new DefaultColumnNameResolver());
     DommelMapper.SetPropertyResolver(new DefaultPropertyResolver());
     DommelMapper.SetForeignKeyPropertyResolver(new DefaultForeignKeyPropertyResolver());
 }
Exemplo n.º 5
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));
        }
        static DataSource()
        {
            SqlMapper.AddTypeHandler <JObject>(new JObjectHandler());

            FluentMapper.Initialize(config =>
            {
                config.AddMap(new TokenDbMap());
                config.ApplyToDommel();
            });

            DommelMapper.SetPropertyResolver(new DommelPropertyResolverForCustomTypes());
        }
Exemplo n.º 7
0
        private void CreateMapper()
        {
            FluentMapper.Initialize(configuration =>
            {
                configuration.ForDommel();
                DommelMapper.SetPropertyResolver(new CustomPropertyResolver());
            });

            foreach (KeyValuePair <Type, IEntityMap> key in FluentMapper.EntityMaps)
            {
                ICustomDapperMapper customMapper = key.Value as ICustomDapperMapper;
                if (customMapper != null)
                {
                    customMapper.Map();
                }
            }

            SqlMapper.AddTypeHandler(new NullableLongHandler());
        }