将实体转换为视图模型以与web api一起使用

时间:2013-08-08 15:12:21

标签: c# entity-framework asp.net-mvc-4 asp.net-web-api

我目前正在努力寻找一种更好的方法来使用ViewModel对象填充Entitiy个对象。我有以下Web Api控制器方法:

[HttpGet]
public IEnumerable<ClientSearchViewModel> FindClients(string query)
{
    var clients = _clientService.SearchClient(query).ToList();
    var returnClients = new List<ClientSearchViewModel>();
    foreach (var client in clients)
    {
        returnClients.Add(new ClientSearchViewModel(client));
    }
    return returnClients;
}

我在ClientSearchViewModel构造函数中执行此操作:

public ClientSearchViewModel(Client client)
{
    this.Id = client.Id;
    this.FirstName = client.PersonName.FirstName;
    this.LastName = client.PersonName.LastName;
}

除了浏览返回的对象列表并创建新的ViewModel列表之外,还有其他方法吗?

2 个答案:

答案 0 :(得分:1)

我强烈建议使用映射插件,例如:

AutoMapper

ValueInjector

这样的插件允许您在内部或数据层中使用的对象与外部对象(DTO / ViewModels)之间进行映射。它们处理开箱即用的许多事情,例如自动映射具有相同类型的任何类似命名的属性,但也允许在特定的属性或类型映射中进行大量控制,以便在需要更多自定义的时候

对于这两者的简要比较,没有比听到作者自己回答的好多了:AutoMapper vs ValueInjecter

就我个人而言,我发现ValueInjector使用起来更快,同时整体控制力更强,但我也发现它比AutoMapper更不易读/直观,因为AutoMapper可能需要更多的代码来实现类似的目标。因此,我会选择你找到的那个和/或你的团队会更喜欢你的语法,以及你能够轻松掌握概念与你真正需要多少力量。

答案 1 :(得分:1)

所以我有同样的miff ...我不能说我已经对我的解决方案进行了基准测试,但它似乎确实运行得相当快......

3位:

public static T Transform<T>(this object convertFrom) where T : class, new()
        {
            return (T) (new ServiceExtension().Transform(convertFrom, typeof (T)));

        }

private class ServiceExtension
        {
            public object Transform(object convertFrom, Type convertTo)
            {
                object _t = Activator.CreateInstance(convertTo);
                if (convertFrom == null) return _t;

                var convertType = convertFrom.GetType();
            foreach (
                var property in _t.GetType().GetProperties().Where(f => f.CanWrite && f.GetSetMethod(true).IsPublic)
                )
            {
                if (property.GetCustomAttributes(typeof (TransformAttribute), true).Any())
                {
                    var transform =
                        (property.GetCustomAttributes(typeof (TransformAttribute), true).FirstOrDefault() as
                         TransformAttribute);
                    var transformname = transform.RelatedField ?? property.Name;

                    if (convertType.GetProperty(transformname) == null)
                        throw new ArgumentException(
                            string.Format(
                                "We were unable to find property:\"{0}\" on {1}.  Please check the RelativeField value on the {2} for \"{0}\"",
                                transformname, convertFrom.GetType().Name, convertTo.Name));

                    var theValue = convertType.GetProperty(transformname).GetValue(convertFrom);

                    if (isCollection(theValue))
                    {
                        foreach (var item in (theValue as ICollection))
                        {
                            var someVal = new object();
                            var newToType = property.PropertyType.GetGenericArguments().FirstOrDefault();

                            if (!String.IsNullOrEmpty(transform.FullyQualifiedName))
                                someVal =
                                    Transform(
                                        item.GetType().GetProperty(transform.FullyQualifiedName).GetValue(item),
                                        newToType);

                            else
                                someVal = Transform(item, newToType);
                            if (property.GetValue(_t) == null)
                                throw new NullReferenceException(
                                    string.Format(
                                        "The following property:{0} is null on {1}.  Likely this needs to be initialized inside of {1}'s empty constructor",
                                        property.Name, _t.GetType().Name));
                            property.PropertyType.GetMethod("Add")
                                    .Invoke(property.GetValue(_t), new[] {someVal});
                            //property.SetValue(_t, theValue.Transform(theValue.GetType()));
                        }
                    }
                    else
                        property.SetValue(_t, theValue);
                }
                //property.SetValue(_t, property.GetValue(convertFrom, null), null);
            }

            return _t;
        }

        public bool isCollection(object o)
        {
            return o is ICollection
                   || typeof (ICollection<>).IsInstanceOfType(o);
        }
    }


public class TransformAttribute : Attribute
    {
        public string RelatedField { get; private set; }
        public string FullyQualifiedName { get; set; }

    public TransformAttribute()
    {
    }

    public TransformAttribute(string relatedField)
    {
        RelatedField = relatedField;
    }

}

这样最终结果是:myObject.Transform()

但是这些装饰可以让你解释POCO和ViewModel之间的差异