los*_*wpf 3 dns nhibernate dto automapper
我对这个设计并不熟悉,但我希望得到一些指导.
我有一个后端服务,将DTO发送到WPF智能客户端.在WPF智能客户端上,用户将更改,删除和修改项目,然后发送回更改(客户端 - >服务器).例如,目前我正在处理客户详细信息表单,用户可以在数据网格中添加,删除和更改属于客户的类别.当DTO被发送回服务器时,我想加载与DTO中的ID相关的域对象,并将对DTO所做的更改应用于域对象,包括所有子集合.
我尝试使用UpdateCustomer方法在下面的代码中执行类似的操作.但是,我认为我的观点不合时宜.当代码运行而不是最终列出{个人,公司,非政府组织,政府}时,我最终得到了{个人,B2B,公司,非政府组织,政府}的清单,因为它显然没有从中删除B2B条目原始清单.
我遇到的一个选择是遍历DTO集合并将其与域对象中的集合进行比较,并根据已修改的内容添加,删除和更新.然而,这看起来真的很麻烦.
我需要做什么才能将DTO中的更改应用到我的domiain对象中的子集合中?
非常感谢您的任何帮助,我们将非常感激
亚历克斯
public class Customer
{
public virtual int Id { get; set; }
public virtual IList<Category> Categories { get; private set; }
public virtual string Code { get; set; }
public virtual string Description { get; set; }
public Customer()
{
Categories = new List<Category>();
}
public virtual void AddCategory(string categoryName)
{
Categories.Add(new Category(categoryName));
}
}
public class Category
{
public virtual string CategoryName { get; private set; }
public virtual Customer Customer {get;set;}
public virtual int Id { get; set; }
protected Category(){}
public Category(string name)
{
CategoryName = name;
}
}
}
public void SetUpAutoMapper()
{
Mapper.CreateMap<Category, CategoryDto>();
Mapper.CreateMap<Customer, CustomerDto>();
Mapper.CreateMap<CategoryDto, Category>();
Mapper.CreateMap<CustomerDto, Customer>();
Mapper.AssertConfigurationIsValid();
}
public void SaveCustomer()
{
var customer = new Customer{Code="TESTCUST",Description="TEST CUSTOMER"};
customer.AddCategory("Individual");
customer.AddCategory("B2B");
customer.AddCategory("Healthcare");
customer.AddCategory("NGO");
repository.Save(customer);
}
public CustomerDto GetCustomer(int customerId)
{
var customer = repository.GetCustomer(customerId);
var customerDto = Mapper.Map<Customer,CustomerDto>(customer);
return customerDto;
}
public void UpateCustomer(CustomerDto customerToUpdate)
{
/*imagine that the dto incoming has had the following operations performed on it
-----add new category----
customerToUpdate.Categories.Add(new CategoryDto {CategoryName = "Government"});
---update existing category---
customerToUpdate.Categories[2].CategoryName = "Company";
---remove category---
customerToUpdate.Categories.RemoveAt(1);*/
var customer = repository.GetCustomer(customerToUpdate.Id);
/* How in this bit do I ensure that the child collection changes are
propogated into the underlying customer object retrieved from the database*/
var customer = Mapper.Map<CustomerDto,Customer>(customerToUpdate);
repository.Save(customer);
}
public class CustomerDto
{
public int Id { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public List<CategoryDto> Categories { get; set; }
}
public class CategoryDto
{
public int Id { get; set; }
public string CategoryName { get; set; }
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Customer" table="Customer">
<id name="Id" column="CustomerId">
<generator class="native"/>
</id>
<property name="Code" />
<property name="Description" />
<bag name="Categories" table="Categories" cascade="all" inverse="false">
<key column="FK_CustomerID" />
<one-to-many class="Category"/>
</bag>
</class>
<class name="Category" table="Categories">
<id name="Id" column="CategoryId">
<generator class="native"/>
</id>
<many-to-one name="Customer" column="FK_CustomerId" not-null="true" class="Customer"></many-to-one>
<property name="CategoryName" />
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
我最近做了类似的事情,但EF作为数据.我不知道nhibernate知道相同的方法是否有效.
基本步骤是
.ForMember(dest => dest.Categories, opt => opt.UseDestinationValue())自定义IObject映射器使用了http://groups.google.com/group/automapper-users/browse_thread/thread/8c7896fbc3f72514中的一些代码.
foreach (var child in source.ChildCollection)
{
var targetChild = target.ChildCollection.SingleOrDefault(c => c.Equals(child)); // overwrite Equals or replace comparison with an Id comparison
if (targetChild == null)
{
target.ChildCollection.Add(Mapper.Map<SourceChildType, TargetChildType>(child));
}
else
{
Mapper.Map(child, targetChild);
}
}
Run Code Online (Sandbox Code Playgroud)最后,在sourceCollection中存在检查targetCollection中所有Id的最后一条逻辑,如果不存在则删除它们.
最终并不是那么多代码,并且可以在其他行动中重复使用.
| 归档时间: |
|
| 查看次数: |
3855 次 |
| 最近记录: |