EDM & # 8594; POCO & # 8594; WCF (.NET4). But passing the collection calls IsReadOnly set to TRUE

Well, that may seem a bit "unorthodox", but ... using VS2010 and the new POCO t4 template for Entity Framework ( Walkthrough: POCO template for Entity Framework ), I can create nice POCOs. Then I can use these POCOs (like DTOs) in the WCF service, essentially going from EDM to the client itself. Tell me what these guys are doing ( POCO with EF 4.0 and WCF 4.0 ), except that everything is created automatically. I understand that the entity and the DTO "should" be different, but in this case I process the client and server, and there are some real advantages to having a DTO in the model and automatically generated.

My problem is that when I transfer an entity that has a relation, the client-created collection (ICollection) is read-only, so I cannot manipulate this relation. For example, when retrieving an existing order, I cannot add the product to the client side of the product collection ... the product collection is read-only.

I would prefer to make a bunch of the client part “order editing” and then send the updated order back, rather than dozens of rounds with servers (for example, AddProductToOrder (product)). I would also prefer not to have a bunch of thunking between Entity and DTO. So all in all it looks good to me ... except for the read-only part.

Is there a solution, or is it too much related to SOA grain?

+3
source share
2 answers

The FixupCollection assigned to your ICollection is recreated as an array when deserialization occurs. Therefore, your product collection is read-only.

To change this, you can use the option (existing at least on VS2010) in the “Add service link” to change the type of the collection to something else (Generic.List or Generic.Observable).

But if you use the option to reuse the type that exists in the existing assembly and refer to the assembly containing your entity, the previous option will not apply to the existing type, and you will still have Array in your product collection.

, ( ), T4, , , FixupCollection,

if (<#=code.FieldName(navProperty)#>.IsReadOnly)
{
    var newCollection = new FixupCollection<<#=code.Escape(navProperty.ToEndMember.GetEntityType())#>>(<#=code.FieldName(navProperty)#>);
    newCollection.CollectionChanged += Fixup<#=navProperty.Name#>;
    <#=code.FieldName(navProperty)#> = newCollection;                   
}
0

"la mouette" :

, , IsReadOnly true wcf ( false).

. , "la mouette", , POCO .

tt, , , .

0

Source: https://habr.com/ru/post/1737712/


All Articles