问题描述
到目前为止,在使用WCF时,我一直将整个EF生成的实体或POCO(通过修改T4模板以将POCO和属性上的DataContract和DataMember包括在内)公开为DataContract.
Up to now, when working with WCF, I have always been exposing the whole either EF generated entities, or POCOs(by modifying the T4 template to include DataContract and DataMember on POCOs and properties) as DataContract.
现在,我遇到了一种情况,我无法公开全部内容,需要显式指定我的DataContract作为实体的子集.
Now, I have come across a situation that I cannot expose the whole thing, and need to explicitly specify my DataContract to be a subset of the entities.
值得一提的是,我的一个实体如下所示:
It worth saying that one of my entities is something like below:
我只想公开ID,名称,CategoryId,价格.
And I want to just expose Id, Name, CategoryId, Price.
插入/更新其余字段(ActiveFrom
,InactiveDate
,Supported
)是根据BR确定的,并且客户端不知道,也不应该确实知道关于以下内容的任何信息他们.
Insert/Update of rest of the fields (ActiveFrom
, InactiveDate
, Supported
) is something that will be decided based on the BR, and client doesn’t know , and should not know indeed, anything about them.
我尝试了以下方法,但是每种方法似乎都存在问题/不起作用:
I have tried following approaches, but each of them seems to have problem/not working:
-
使用 AutoMapper :我需要将源对象映射到目标对象,这是一个单向映射,因此对于演示目的我可以将
Product
映射到ProductContract
.但是对于添加/更新产品,它确实无法工作,因为它无法进行双向映射.
Using AutoMapper : I need to map a source object to adestination object, and this is a one way mapping, so forpresentation purposes I can map
Product
toProductContract
. But for adding/updating the product, it doesnot work as it cannot do a two way mapping.
使用反射并为实体和将[DataMember]
属性添加到Metadata类的属性,如下所示(请注意,我还没有包含不需要的字段):
Use the reflection and create a metadata class for the entities andadd the [DataMember]
attribute to the properties of the Metadata class as below(please note that I haven’t included the unwanted fields):
public class ProductMD : AssociatedMetadataTypeTypeDescriptionProvider
{
public ProductMD() :
base(typeof(Product))
{
}
[DataMember]
public int Id{ get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int? CategoryID { get; set; }
[DataMember]
public decimal? Price { get; set; }
}
,然后将ProductMD
用作Product
的属性不接触自动生成的实体的局部类(仅供参考:我有更改了POCO T4模板生成器以包括每个实体上的[DataContract]
):
And then use the ProductMD
as an attribute for the Product
partial class without touching the auto-generated entity (FYI: I havechanged the POCO T4 template generator to include the[DataContract]
on each entity):
[MetadataType(typeof(ProductMD))]
public partial class Product
{
}
但是在客户端,我没有访问产品的任何DataMembers
.
But on the client side, I do not haveaccess to any of the DataMembers
of the Product.
现在我的问题是,获得我想要做的最好的方法是什么(将实体的子集显示为DataContract
)?
Now my question is that, what is the best approach to gain what I want to do (exposing a subset of the entities as DataContract
)?
推荐答案
我会选择选项1-> AutoMapper.
I would go with option 1 -> AutoMapper.
您可以定义两种方式的映射:
You could define two way mapping:
Mapper.CreateMap<Product, ProductContract>();
Mapper.CreateMap<ProductContract, Product>();
很明显,如果在进行映射时ProductContract
中的属性少于域模型中的属性,则只会填充相应的属性.
obviously if in your ProductContract
you have less properties than in your domain model when doing the mapping, only the corresponding properties will be populated.
在进行逆映射更新时,您可以执行以下操作:
When doing the inverse map for updating you could do the following:
ProductContract pc = ...
Product productToUpdate = GetProduct(pc.Id);
Mapper.Map<ProductContract, Product>(pc, product);
// at this stage the product model will have the properties that
// were present in the ProductContract being mapped from them and
// the rest of the properties will stay unmodified, i.e. they will
// have their initial values that were retrieved from the database.
// Now we can update the product:
UpdateProduct(product);
这篇关于如何将实体的一部分公开为DataContract?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!