【发布时间】:2011-12-27 18:33:11
【问题描述】:
我需要使用模式工厂的想法将我的 Person 类实体中的实体属性 Address 与我的 FactoryEntities 类中的表达式 linq 相关联,看看这就是我所拥有的并且我想要做的:
Address address = new Address();
address.Country = "Chile";
address.City = "Santiago";
address.ZipCode = "43532";
//Factory instance creation object
//This is idea
Person person = new FactoryEntity<Person>().AssociateWithEntity(p=>p.Address, address);
public class Person: Entity
{
public string Name{ get; set; }
public string LastName{ get; set; }
public Address Address{ get; set; }
}
public class Address: Entity
{
public string Country{ get; set; }
public string City{ get; set; }
public string ZipCode{ get; set; }
}
public class FactoryEntity<TEntity> where TEntity : Entity
{
public void AssociateWithEntity<TProperty>(Expression<Func<TEntity, TProperty>> entityExpression, TProperty newValueEntity) where TProperty : Entity
{
if (instanceEntity == null || instanceEntity.IsTransient())
throw new ArgumentNullException();
/*TODO: Logic the association and validation
How set the newValueEntity into the property of entityExpression (x=>x.Direccion = direccion*/
}
}
【问题讨论】:
-
您拥有的
propertyInfo是TEntity,而不是TProperty。您不能使用它来访问不同类型对象的属性。协会应该走哪条路?你在这里尝试做的事情对我来说没有意义。 -
这是一个非常有漏洞的代码,不容易说出你想要的,请澄清它。
-
很抱歉解释不清楚,但我想做的是占用一个工厂,允许我将对象关联到拦截验证关联,以便正确检查
标签: c# linq expression-trees