用于双向映射的简单约定自动映射(来自/来自ViewModels的实体)

Omu*_*Omu 2 c# mapping asp.net-mvc automapping automapper

更新:这个东西已经演变成一个很好的项目,请访问http://valueinjecter.codeplex.com


检查一下,我刚写了一个简单的自动播放器,它从属性中获取具有相同名称和类型的一个对象的值并将其放入另一个对象中,并且可以为您可能需要的每种类型添加异常(ifs,switch)

那么告诉我你怎么看?

我这样做了所以我可以这样做:

Product –> ProductDTO

ProductDTO –> Product
Run Code Online (Sandbox Code Playgroud)

它是如何开始的:

我在我的Inputs/Dto/ViewModels for DropDowns中使用"object"类型,因为我向html发送了一个IEnumerable <SelectListItem>并且我收到了一个选定键的字符串数组

 public void Map(object a, object b)
    {
        var pp = a.GetType().GetProperties();
        foreach (var pa in pp)
        {
            var value = pa.GetValue(a, null);

            // property with the same name in b
            var pb = b.GetType().GetProperty(pa.Name);
            if (pb == null)
            {
                //no such property in b
                continue;
            }

            if (pa.PropertyType == pb.PropertyType)
            {
                pb.SetValue(b, value, null);
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)

更新: 实际用法:
Build方法(Input = Dto):

        public static TI BuildInput<TI, T>(this T entity) where TI: class, new()
        {
            var input = new TI();
            input = Map(entity, input) as TI;
            return input;
        }

        public static T BuildEntity<T, TI, TR>(this TI input)
            where T : class, new()
            where TR : IBaseAdvanceService<T>
        {               
            var id = (long)input.GetType().GetProperty("Id").GetValue(input, null);
            var entity = LocatorConfigurator.Resolve<TR>().Get(id) ?? new T();
            entity = Map(input, entity) as T;
            return entity;
        }

        public static TI RebuildInput<T, TI, TR>(this TI input)
            where T: class, new()
            where TR : IBaseAdvanceService<T>
            where TI : class, new()
        {

                return input.BuildEntity<T, TI, TR>().BuildInput<TI, T>();
            }
Run Code Online (Sandbox Code Playgroud)

在控制器中:

    public ActionResult Create()
    { 
        return View(new Organisation().BuildInput<OrganisationInput, Organisation>());
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(OrganisationInput o)
    {
        if (!ModelState.IsValid)
        {
            return View(o.RebuildInput<Organisation,OrganisationInput, IOrganisationService>());                
        }
        organisationService.SaveOrUpdate(o.BuildEntity<Organisation, OrganisationInput, IOrganisationService>());
        return RedirectToAction("Index");
    }
Run Code Online (Sandbox Code Playgroud)

真正的Map方法

public static object Map(object a, object b)
        {
            var lookups = GetLookups();

            var propertyInfos = a.GetType().GetProperties();
            foreach (var pa in propertyInfos)
            {
                var value = pa.GetValue(a, null);

                // property with the same name in b
                var pb = b.GetType().GetProperty(pa.Name);
                if (pb == null)
                {
                    continue;
                }

                if (pa.PropertyType == pb.PropertyType)
                {
                    pb.SetValue(b, value, null);
                }
                else if (lookups.Contains(pa.Name) && pa.PropertyType == typeof(LookupItem))
                {
                    pb.SetValue(b, (pa.GetValue(a, null) as LookupItem).GetSelectList(pa.Name), null);
                }
                else if (lookups.Contains(pa.Name) && pa.PropertyType == typeof(object))
                {
                    pb.SetValue(b, pa.GetValue(a, null).ReadSelectItemValue(), null);
                }
                else if (pa.PropertyType == typeof(long) && pb.PropertyType == typeof(Organisation))
                {
                    pb.SetValue(b, pa.GetValue<long>(a).ReadOrganisationId(), null);
                }
                else if (pa.PropertyType == typeof(Organisation) && pb.PropertyType == typeof(long))
                {
                    pb.SetValue(b, pa.GetValue<Organisation>(a).Id, null);
                }
            }

            return b;
        }
Run Code Online (Sandbox Code Playgroud)

Rub*_*ink 6

只需使用AutoMapper.这很好,但它会成长为一个迷你项目.

只有一些事情AM(真正的)做的是:

  • 报告是否具有无法映射到的属性
  • 展平物体
  • 提供钩子供您自定义某些方面,而不是在大的switch语句中
  • 将Expression.Compile用于perf原因而不是直接反射

但它肯定是一个有趣的空间,而自动映射的想法肯定是有用的.

有点像DI在1533行与NInject或其朋友 - 酷,但为什么?

我认为你已经阅读了关于Jimmy博客上双向映射文章和评论


Jim*_*ard 6

您可能想要添加的一件事是缓存反射位.如果您将对象映射两次,则可能不希望再次查找所有反射内容.此外,像GetValue和SetValue这样的东西很慢,我切换到后期委托+ Reflection.Emit以加快速度.

  • 缓存肯定有助于AutoMapper - GetFields和GetProperties调用可能成为瓶颈 - 在dotTrace之类的操作中运行它,你可以看到花费的时间.这种类型的编码非常酷,玩得开心! (2认同)