How to manage EF 4.2 associations in WCF-based scripts

I am working on a corporate application that will consist of a rich WPF client that communicates with a bunch of web services to retrieve data. These data are POCOs created using Code First EF 4.2.

Now I am facing a conceptual problem that I am trying to wrap around, but could not find a good solution.

1: n Association

So the datamodel looks like this:

public class Person { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Person> Children { get; set; } } 

Serverside I have an interface that takes care of binding the newly created dtos (including new items in Children Collection) to the Datacontext from the client side and saving it. This, of course, only works if these objects are created on the client side and then sent for addition. The service adds new objects and repeatedly returns the updated objects (Id property) basically.

 [ServiceContract] public interface IMyPersonCaretaker { [OperationContract] Person CreatePerson(Person entity) } 

However, when I get existing entities, I cannot edit any associations (add or remove objects - because they have a fixed size). So now I will need to expand the interface to allow this:

 [ServiceContract] public interface IMyPersonCaretaker { [OperationContract] Person CreatePerson(Person entity) [OperationContract] Person AddChild(Person parent, Person child) } 

What seems like a clumsy approach to me, and the interfaces are getting bigger and bigger pretty fast. Is this a complex approach to working with POCOs? How do you do this?

n: m Associations using manual matching

Another part of the datamodel looks like this:

 public class ClassA { public int Id { get; set; } public virtual ICollection<AtoBMapping> Mappings { get; set; } } public class ClassB { public int Id { get; set; } public virtual ICollection<AtoBMapping> Mappings { get; set; } } public class AtoBMapping { public int Id { get; set; } public virtual ClassA A { get; set; } public virtual ClassB B { get; set; } } 

Whenever I try to create an instance of ClassA and ClassB on the client side and add it to each other through the binding, I get an error message when I try to add it to Set in context. The error says that it is not allowed to remove elements from the Mappings property, and I really don’t understand where this comes from.

The second part may be a bit abstract for the description, if someone needs additional information, I am more than ready to add it!

PS: Please do not offer Selftracking organizations, I know about them, but I'm really interested in a solution based solely on EF 4.2 POCOs.

PPS: the code is written manually directly in this window, and not the actual code used, so maybe something is missing, but this is not a question of my question, so I hope this is enough.

+4
source share
1 answer

I have a similar solution where we need a whole group of operations such as CRUD, but our datamodel does not leave the server, we map objects to separate DTOs using automapper, these DTOs are usually WCF DatContract classes and do not have many relationships as a domain model.

Initially, this may seem like a very difficult assessment, but IMO will ultimately pay off because you have explicit control over your interfaces, and in many cases you really don't need to transfer the entire domain model (with all its relationships) to the client, the client can usually display so much data.

another option might be WCF DataServices, they will use RESTful interfaces to transfer your data.

[New option]

another option I've used in the past is just one CRUD service method that accepts a byte array. Use the NetDataContractSerializer to serialize / deserialize object graphs to and from these methods. Then use a custom client that moves the data back and forth, creates the objects, and binds them to the DataContext to perform operations .... something like this

+1
source

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


All Articles