I have a get method in which I do some filtering on some array properties of the object I want to return:
public async Task<WorkflowStepDto> GetCurrentWorkflowStep(string entity, string status, string clientId = null)
{
var workflowStep = await _workflowStepRepository.GetAll().Include("WorkflowEntity")
.Include("WorkflowStepActions").Include("WorkflowStepConfigurations")
.FirstOrDefaultAsync(s => s.Name == status && s.WorkflowEntity.Name == entity);
var workflowRoles = User.Claims.Where(c => c.Type == "workflowRole").Select(c => c.Value).ToArray();
workflowStep.WorkflowStepActions =
workflowStep.WorkflowStepActions
.Where(s => (workflowRoles.Contains(s.WorkflowRole) ||
(workflowRoles.Length == 0 && string.IsNullOrEmpty(s.WorkflowRole)) ||
s.WorkflowRole == "*")).OrderBy(a => a.Priority).ToList();
workflowStep.WorkflowStepConfigurations = workflowStep.WorkflowStepConfigurations
.Where(c => workflowRoles.Contains(c.WorkflowStepRoleId) ||
(workflowRoles.Length == 0 && string.IsNullOrEmpty(c.WorkflowStepRoleId)))
.OrderBy(c => c.Priority).Take(1).ToList();
return workflowStep.MapTo<WorkflowStepDto>();
}
For example property WorkflowStepActions
needs to be filtered out to show only the actions the user has access to, and the same goes for WorkflowStepConfigurations
.
The problem is the UnitOfWork
pattern. Even though this is a GET
method and I don't want to save any data, at the end of the method the UnitOfWork saves the changes to the database, deleting the WorkflowStepActions
that have been filtered out in the current call.
I tried disabling the UnitOfWork alltogether using [UnitOfWork(IsDisabled=false)]
, but then I get an error saying that the repository cannot be accessed because it has been disposed.
Is there any way to AcceptChanges
to a UnitOfWork, or to make it stop saving changes automatically?
Try this workaround, but at this case
GetCurrentWorkflowStep
should be standalone, i.e. not implicitly called by your another code: