I have an abstract "Action" class that has the types ActionAppointment, ActionCall, ActionEmail and ActionLetter. I am trying to write a function that will be DRY at our service level, so we no longer write CRUD requests.
I have some update logic in our service layer (many other codes have been removed for brevity):
private IServiceResponse UpdateAction<T>(T action, string originalActionStatus) where T : Action { if (action.GetType() == typeof(Action)) { _actionRepository.Update(action); } else if (action.GetType() == typeof(ActionAppointment)) { _actionAppointmentRepository.Update(action as ActionAppointment); } else if (action.GetType() == typeof(ActionCall)) { _actionCallRepository.Update(action as ActionCall); } else if (action.GetType() == typeof(ActionEmail)) { _actionEmailRepository.Update(action as ActionEmail); } else if (action.GetType() == typeof(ActionLetter)) { _actionLetterRepository.Update(action as ActionLetter); } }
Unfortunately, as our repositories are configured, I have to use specially named repositories (i.e. I cannot update the ActionLetter via _actionRepository, even if it is obtained from Action)
I read different templates and it looks like something similar to the Factory template, but I donβt see how to make it work.
Am I missing something stupid?
source share