views:

15

answers:

0

I've been running into some problems with my design. Here is what I have now...

Controller

...
<AcceptVerbs(HttpVerbs.Post)> _
Public Function Edit(ByVal id As Integer, ByVal dryShots As Integer, ByVal clock As Integer) As ActionResult

    Dim job As Shift_Summary_Job = _shiftSummaryJobService.GetShiftSummaryJob(id)
    _shiftSummaryJobService.UpdateJob(job.ID, dryShots, clock)

    Return RedirectToAction("Details", "Summary", _
        New With { _
            .endDate = job.Shift_Summary.Date.ToString("MM-dd-yyyy"), _
            .shift = job.Shift_Summary.Shift.ID, _
            .machine = job.Shift_Summary.Machine.Number _
        })

End Function
...

ShiftSummaryJobService

...
Public Sub UpdateJob(ByVal shiftSummaryJobID As Integer, ByVal dryShots As Integer, ByVal clock As Integer) Implements IShiftSummaryJobService.UpdateJob


    Dim job As Shift_Summary_Job = GetShiftSummaryJob(shiftSummaryJobID)
    Dim op As [Operator]

    Try

        op = _operatorService.GetOperator(clock)

    Catch ex As Exception

        op = _operatorService.Create(clock)

    End Try


    job.DryShots = dryShots
    job.Operator = op

    _repository.Update(job)

End Sub
...

ShiftSummaryJobRepository

...
    Public Sub Update(ByVal shiftSummaryJob As Shift_Summary_Job) Implements IShiftSummaryJobRepository.Update

        _db.SubmitChanges()

    End Sub
...

When the update executes the db.SubmitChanges(), I get an exception: NotSupportedException - An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.

I am using StructureMap to manage the dependencies and I thought it would cache the data context so that all repositories would use the same "copy".

[For](Of DCSDataContext)() _
    .HybridHttpOrThreadLocalScoped _
    .Use(Function() New DCSDataContext())

So, my question is...how would you design/setup the service and repository layers so that services can call each other to build or change an object, which is then passed to the repository to be saved or updated? Or, is there another design approach that would be better?

This code was working at one point, until I made a change in another area and then it was broken all of a sudden.