zoukankan      html  css  js  c++  java
  • [转载]AutoMapper 9.0的改造(续)

    载自https://www.cnblogs.com/NCoreCoder/p/11453443.html

    上一篇有一个读者,有疑问,如何自动化注册Dto

    我开篇,做了一个自动化注册的

    复制代码
        public sealed class AutoInjectAttribute : Attribute
        {
            public Type SourceType { get; }
            public Type TargetType { get; }
    
            public AutoInjectAttribute(Type sourceType, Type targetType)
            {
                SourceType = sourceType;
                TargetType = targetType;
            }
        }
    复制代码

    增加了一个特性,在Dto上面打上,参数1是源类型,参数2是Dto类型

    增加一个工厂类保存自动转换的类型

    复制代码
        public class AutoInjectFactory
        {
            public List<(Type,Type)> ConvertList { get; } = new List<(Type, Type)>();
    
            public void AddAssemblys(params Assembly[] assemblys)
            {
                foreach (var assembly in assemblys)
                {
                    var atributes = assembly.GetTypes()
                        .Where(_type => _type.GetCustomAttribute<AutoInjectAttribute>() != null)
                        .Select(_type => _type.GetCustomAttribute<AutoInjectAttribute>());
    
                    foreach (var atribute in atributes)
                    {
                        ConvertList.Add((atribute.SourceType, atribute.TargetType));
                    }
                }
            }
        }
    复制代码

    在原来的AddAutoMapper上找到修改的代码段

    复制代码
            public static IServiceCollection AddAutoMapper(this IServiceCollection service)
            {
                ...略
                service.TryAddSingleton(serviceProvider =>
                {
                    var mapperConfigurationExpression = serviceProvider.GetRequiredService<MapperConfigurationExpression>();
    
                    var instance = new MapperConfiguration(mapperConfigurationExpression);
    
                    instance.AssertConfigurationIsValid();
    
                    return instance;
                });
                ...略
    
                return service;
            }
    复制代码

    改为

    复制代码
            public static IServiceCollection AddAutoMapper(this IServiceCollection service)
            {
                ...略
                service.TryAddSingleton(serviceProvider =>
                {
                    var mapperConfigurationExpression = serviceProvider.GetRequiredService<MapperConfigurationExpression>();
                    var factory = serviceProvider.GetRequiredService<AutoInjectFactory>();
    
                    foreach (var (sourceType,targetType) in factory.ConvertList)
                    {
                        mapperConfigurationExpression.CreateMap(sourceType, targetType);
                    }
    
                    var instance = new MapperConfiguration(mapperConfigurationExpression);
    
                    instance.AssertConfigurationIsValid();
    
                    return instance;
                });
                ...略
    
                return service;
            }
    复制代码

    增加一组扩展方法

    复制代码
        public static class AutoMapperExtension
        {
            ...略
    
            public static void UseAutoInject(this IApplicationBuilder applicationBuilder, params Assembly[] assemblys)
            {
                var factory = applicationBuilder.ApplicationServices.GetRequiredService<AutoInjectFactory>();
    
                factory.AddAssemblys(assemblys);
            }
        }
    复制代码

    在Startup.Configure方法内调用一下

    看看测试

    增加一个测试控制器

    执行结果

  • 相关阅读:
    Nutch的配置(使用MySQL作为数据存储)
    MySQL简单实现多字段模糊查询
    nutch的一些基础整理
    Java分布式爬虫Nutch教程——导入Nutch工程,执行完整爬取
    Nutch2 WebPage写入数据库的过程分析
    Nutch2 WebPage 字段解释
    nutch如何修改regex-urlfilter.txt爬取符合条件的链接
    Run Nutch In Eclipse on Linux and Windows nutch version 0.9
    Linux Mint 17.1 安装全配置
    Ubuntu(Linux Mint):sudo apt-get upgrade升级失败
  • 原文地址:https://www.cnblogs.com/hx215267863/p/11957740.html
Copyright © 2011-2022 走看看