1
votes

First off, thanks for any help. I am using ria services to insert/update/delete entities and save a history of those operations. I want to perform the operation and save the history in ONE call to the service. Right now I am stuck on the insert because I need the new entities ID that is generated on the insert. I might be taking the wrong approach all together (but I hope not). I have overriden the submit method, and am trying to save a snapshot in the history table, I don't want to save a snapshot of the original version:

    public override bool Submit( ChangeSet changeSet )
    {
        //SUBMIT FIRST SO THE OBJECT(S) HAVE AN ID
        var success = base.Submit( changeSet );
        if ( success )
            foreach ( var changeSetEntry in changeSet.ChangeSetEntries )
            {
                if ( changeSetEntry.Entity is MyBusinessEntity )
                {
                    var newBusinessEntity = (MyBusinessEntity) changeSetEntry.Entity;
                    RecordModifiedMyBusinessEntity( changeSetEntry.Operation, newBusinessEntity );
                }
            }
        return success;
    }

    private void RecordModifiedMyBusinessEntity( DomainOperation operation, MyBusinessEntity newBusinessEntity )
    {
        var hist = new BusinessEntityHistory
        {
            ChangedBy = new AuthenticationService().GetUser().FriendlyName,
            ChangedDate = DateTime.Now,
            Operation = operation.ToString(),
            BusinessEntityId = newBusinessEntity.Id,
            Group = newBusinessEntity.Group,
            Priority = newBusinessEntity.Priority,
            ....
        };
        InsertBusinessEntityHistory( hist );
        //HERE IS WHERE I WANT TO CALL SUBMIT CHANGES AGAIN, BUT 1 - IT'S NOT IN THE CHANGESET,
        //AND 2 - THE OBJECT I ALREADY INSERTED IS IN THE CHANGESET (SO IF I SUBMIT AGAIN, IT GETS 
        //INSERTED TWICE AND NO HISTORY IS SAVED.  AND 3 - I CAN'T DO THE HISTORY BEFORE BECAUSE I DON'T 
        //HAVE THE ID, AND I DON'T WANT TO DO A MAX ID + 1 BECAUSE SOMEONE ELSE MIGHT BE 
        //INSERTING INTO THE SAME TABLE
    }
1

1 Answers

2
votes

Here is the solution I ended up going with:

    public override bool Submit( ChangeSet changeSet )
    {
        //submit the changes
        var success = base.Submit( changeSet );
        if ( success )
        {
            //make a new list of change set entries 
            var entries = new List<ChangeSetEntry>();
            //each change set entry needs an id (not to be confused with the entity's id)
            var maxId = 0;
            //iterate through each change and add historical snapshot.
            foreach ( var changeSetEntry in changeSet.ChangeSetEntries )
            {
                var entity = changeSetEntry.Entity;
                var operation = changeSetEntry.Operation;
                var myEntity = entity as MyEntityType;
                if ( myEntity != null )
                {
                    entries.Add( GetHistoryChangeSetEntry( ref maxId, operation, myEntity ) );
                    continue;
                }
            }
            //make new change set with historical snapshots
            var newChangeSet = new ChangeSet( entries );
            //submit the new change set
            base.Submit( newChangeSet );
        }
        return success;
    }

    private ChangeSetEntry GetHistoryChangeSetEntry( ref int maxId, DomainOperation operation, MyEntityType myEntity )
    {
        return new ChangeSetEntry
                {
                    Id = ++maxId,
                    //We are inserting this change set entry
                    Operation = DomainOperation.Insert,
                    Entity = new MyEntityTypesHistory
                                {
                                    ChangedBy = ServiceContext.User.Identity.Name,
                                    ChangedDate = DateTime.Now,
                                    //The operation performed on the original entity
                                    Operation = operation.ToString(),
                                    MyEntityId = myEntity.EntityId,
                                    MyEntityField1 = myEntity.EntityField1,
                                    MyEntityField2 = myEntity.EntityField2
                                }
                };
    }

I had to make a new change set, and new change set entries for it, and submit changes with the new change set.