所以,我有这个例子的例子:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public List<Car> Cars { get; set; }
public List<User> Children { get; set; }
}
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}
那么,有一种更新User
对象,更新Children
和Cars
的方法吗?
如果对象中没有要更新的汽车,那么在新对象中添加它,但是如果要更新的对象中存在并且新对象中没有,则将其从要更新的对象中删除,并更新匹配的所有汽车的所有属性,以及Children
属性。
有可能吗?
答案 0 :(得分:1)
最简单的方法是在目标对象中设置Cars
和Children
属性,以引用源对象中的Cars
和Children
列表。
originalUser.Cars = changedUser.Cars;
originalUser.Children = changedUser.Children;
如果在不同的ORM上下文中创建了对象,则可能无法实现。
另一种方法是比较列表并适当地修改对象。你需要写一个方法来做到这一点。 LINQ非常有帮助。
<强>更新强>
这是一个快速而肮脏(可能非常低效)的扩展方法,可能很有用:
public void UpdateUser(this User user, User changedUser)
{
var changedUserCarIDs = changedUser.Cars.Select(c => c.Id).ToArray();
user.Cars = user.Cars.Where(c => changedUserCarIDs.Contains(c.Id)).ToList();
foreach (var changedCar in changedUser.Cars)
{
var car = user.Cars.SingleOrDefault(c => c.Id == changedCar.Id);
if (car == null) user.Cars.Add(car = new Car());
car.Name = changedCar.Name;
car.Color = changedCar.Color;
}
}
答案 1 :(得分:1)
查找添加和删除的内容:
var removedCars = orgUser.Cars.Except(changedUser.Cars);
var addedCars = changedUser.Cars.Except(orgUser.Cars);
使用相等比较器重载来查找所有已更改的汽车:http://msdn.microsoft.com/en-us/library/bb336390.aspx
为孩子们做同样的事情。
答案 2 :(得分:1)
是的!这可能是解决方案(此解决方案更新所有属性,类属性和集合属性,但如果内部有类,则类需要继承AbstractEntity):
private void UpdateAllProperties<idType, entityType>(entityType currentEntity, entityType newEntity)
where idType : IEquatable<idType>
where entityType : AbstractEntity<idType>
{
var currentEntityProperties = currentEntity.GetType().GetProperties();
var newEntityProperties = newEntity.GetType().GetProperties();
foreach (var currentEntityProperty in currentEntityProperties)
{
foreach (var newEntityProperty in newEntityProperties)
{
if (newEntityProperty.Name == currentEntityProperty.Name)
{
if (currentEntityProperty.PropertyType.BaseType.IsGenericType &&
currentEntityProperty.PropertyType.BaseType.GetGenericTypeDefinition() == typeof(AbstractEntity<>))
{
var idPropertyType = currentEntityProperty.PropertyType.GetProperty("Id").PropertyType;
var entityPropertyType = currentEntityProperty.PropertyType;
this.InvokeUpdateAllProperties(currentEntityProperty.GetValue(currentEntity, null),
newEntityProperty.GetValue(newEntity, null),
idPropertyType, entityPropertyType);
break;
}
else if (currentEntityProperty.PropertyType.GetInterfaces().Any(
x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(ICollection<>)))
{
dynamic currentCollection = currentEntityProperty.GetValue(currentEntity, null);
dynamic newCollection = newEntityProperty.GetValue(newEntity, null);
this.UpdateCollectionItems(currentEntityProperty, currentCollection, newCollection);
dynamic itemsToRemove = Enumerable.ToList(Enumerable.Except(currentCollection, newCollection));
dynamic itemsToAdd = Enumerable.ToList(Enumerable.Except(newCollection, currentCollection));
dynamic itemsAreEqual = Enumerable.ToList(Enumerable.Intersect(currentCollection, newCollection));
for (int i = 0; i < itemsToRemove.Count; i++)
{
currentCollection.Remove(Enumerable.ElementAt(itemsToRemove, i));
}
for (int i = 0; i < itemsToAdd.Count; i++)
{
currentCollection.Add(Enumerable.ElementAt(itemsToAdd, i));
}
break;
}
else
{
currentEntityProperty.SetValue(currentEntity, newEntityProperty.GetValue(newEntity, null), null);
break;
}
}
}
}
}
private void UpdateCollectionItems(PropertyInfo currentEntityProperty, dynamic currentCollection, dynamic newCollection)
{
var collectionType = currentEntityProperty.PropertyType.GetInterfaces().Where(
x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(ICollection<>)).First();
var argumentType = collectionType.GetGenericArguments()[0];
if (argumentType.BaseType.IsGenericType &&
argumentType.BaseType.GetGenericTypeDefinition() == typeof(AbstractEntity<>))
{
foreach (var currentItem in currentCollection)
{
foreach (var newItem in newCollection)
{
if (currentItem.Equals(newItem))
{
var idPropertyType = currentItem.GetType().GetProperty("Id").PropertyType;
var entityPropertyType = currentItem.GetType();
this.InvokeUpdateAllProperties(currentItem, newItem, idPropertyType, entityPropertyType);
}
}
}
}
}
private void InvokeUpdateAllProperties(dynamic currentEntity, dynamic newEntity, dynamic idPropertyType, dynamic entityPropertyType)
{
var method = this.GetType().GetMethod("UpdateAllProperties", BindingFlags.Instance | BindingFlags.NonPublic);
var genericMethod = method.MakeGenericMethod(idPropertyType, entityPropertyType);
genericMethod.Invoke(this, new[] { currentEntity, newEntity });
}
用法的例子:
AbstractEntity:
public abstract class AbstractEntity<idType>
where idType : IEquatable<idType>
{
public idType Id { get; set; }
}
例如:
public class User : AbstractEntity<int>
{
public string Name { get; set; }
public List<Car> OtherCars { get; set; }
public Car MainCar { get; set; }
public bool Equals(Car other)
{
if (this.Id == other.Id)
{
return true;
}
else
{
return false;
}
}
}
public class Car : AbstractEntity<int>
{
public string Name { get; set; }
public string Color { get; set; }
public bool Equals(Car other)
{
if (this.Id == other.Id)
{
return true;
}
else
{
return false;
}
}
}
使用方法:
User currentUser = new User()
{
Id = 1,
Name = "Vinicius",
OtherCars = new List<Car>()
{
new Car()
{
Id = 2,
Name = "Corsa II",
Color = "Azul"
},
new Car()
{
Id = 3,
Name = "Palio",
Color = "Vermelho"
},
new Car()
{
Id = 4,
Name = "Fusca",
Color = "Azul"
}
},
MainCar = new Car()
{
Id = 1,
Name = "Corsa",
Color = "Preto"
}
};
User updatedUser = new User()
{
Id = 1,
Name = "Vinicius Ottoni",
OtherCars = new List<Car>()
{
new Car()
{
Id = 5,
Name = "Voyage",
Color = "Azul"
},
new Car()
{
Id = 6,
Name = "Voyage II",
Color = "Vermelho"
},
new Car()
{
Id = 4,
Name = "Fusca",
Color = "Rosa"
}
},
MainCar = new Car()
{
Id = 2,
Name = "Voyage",
Color = "Vinho"
}
};
this.UpdateAllProperties<int, User>(currentUser, updatedUser);