0

这个简单的注入器代码如何转化为统一。这是参考this

public static Container RegisterAutoMapper(this Container container)
{
    var profiles = typeof(AutoMapperRegistry).Assembly.GetTypes()
        .Where(t => typeof(Profile).IsAssignableFrom(t))
        .Select(t => (Profile)Activator.CreateInstance(t));

    var config = new MapperConfiguration(cfg =>
    {
        foreach (var profile in profiles)
        {
            cfg.AddProfile(profile);
        }
    });

    container.Register<MapperConfiguration>(() => config);
    container.Register<IMapper>(() => container.GetInstance<MapperConfiguration>()
        .CreateMapper());

    return container;
}

我相信我们扫描配置文件并循环遍历的第一部分非常简单,我可能是错的。接近的东西

public static Container RegisterAutoMapper(this Container container)
{
    var profiles = AppDomain.CurrentDomain
                            .GetAssemblies()
                            .SelectMany(a => a.GetTypes());

    var config = new MapperConfiguration(cfg =>
    {
        foreach (var item in profiles)
        {
            if (item.FullName != "AutoMapper.SelfProfiler`2")
                cfg.AddProfile(Activator.CreateInstance(item) as Profile);
        }
    });


    //container.Resolve...;
    //container.Resolve...;

    return container;
}

我们将如何使用统一来解决这个问题。

4

0 回答 0