开发者

How to insert 2 new related DTOs using RIA Service?

开发者 https://www.devze.com 2023-03-30 17:51 出处:网络
I am using RIA Service in our Silverlight application. Database entities are not directly exposed to a client but I have a set of POCO classes for it. Then in CRUD methods for these POCO classes they

I am using RIA Service in our Silverlight application. Database entities are not directly exposed to a client but I have a set of POCO classes for it. Then in CRUD methods for these POCO classes they are converted to database entities and saved to database. The problem arises on the server side when client creates 2 new POCO entities which are related. Insert method is called on the server for each POCO entity separately and I may create corresponding new database entities there and add them to object context. But I see no way to add relation between these created database entities. Is there a solution for that?

For example, I have these 2 POCO entities (simplified):

[DataContract(IsReference = true)]
public partial class Process
{
    [DataMember]
    [Key]
    public string Name
    {
       get; set;
    }

    [DataMember]
    public long StepId
    {
       get; set;
    }

    [DataMember]
    [Association("StepProcess", "StepId", "Id", IsForeignKey=true)]
    public Step Step
    {
       get; set;
    }
}

[DataContract(IsReference = true)]
public partial class Step
{
    [DataMember]
    [Key]
    public long Id
    {
       get; set;
    }

    [DataMember]
    public string Name
    {
       get; set;
    }
}

And I have these 2 Insert methods in my domain service class:

public void InsertProcess(Process process)
{
    var dbProcess = new DBProcess();
    dbProcess.Name = process.Name;
    //dbProcess.StepId = process.StepId;  Cannot do that!
    this.ObjectContext.AddToDBProcess(dbProcess);
}

public void InsertStep(Step step)
{
    var dbStep = new DBStep();
    dbStep.Name = step.Name;
    this.ObjectContext.AddToDBSteps(dbStep);

    this.ChangeSet.Associate<Step, DBStep>
            (step, dbStep, (dto, enti开发者_如何学Pythonty) =>
            {
                dto.Id = entity.Id;
            });
}

Client adds a new Process, then creates and adds a new Step to it and then calls SubmitChanges(). Process.StepId is not filled with a correct value as there is no correct Step.Id for the newly created step yet, so I cannot just copy this value to database entity. So the question is how to recreate relations between newly created database entities the same as they are in newly created DTOs?

I know about Composition attribute but it is not suitable for us. Both Process and Step are independent entities (i.e. steps may exist without a process).


There are two ways to solve this:

  1. Have each call return the primary key for the item after it is created, then you can store the resulting PKey in the other POCO to call the second service.

  2. Create a Service method that takes both POCOs as parameters and does the work of relating them for you.


Thanks, although both these suggestions are valid but they are also applicable only for simple and small object hierarchies, not my case. I end up using approach similar to this. I.e. I have a POCO to database objects map. If both Process and Step are new, in InsertProcess method process.Step navigation property is filled with this new step (otherwise StepId can be used as it referenced to existing step). So if this process.Step is in the map I just fill corresponding navigation property in DBProcess, otherwise I create new instance of DBStep, put it to the map and then set it to DBProcess.Step navigation property. This new empty DBStep will be filled in InsertStep method later.

0

精彩评论

暂无评论...
验证码 换一张
取 消