0

Our database has an Attribute table and a WorkItem table that are in a many to many relationship using a WorkItemAttribute table. The WorkItemAttribute table contains the foreign keys for each and a value column specifying the value of the associated attribute.

I wrote a simple CRUD web api and tests dealing with the WorkItemAttributes on a per WorkItemID basis using an ORM provider.

I was then tasked with doing the same for an EnvelopeAttribute table, which is the same as the WorkItemAttribute table but has a foreign key to an Envelope table instead.

I can duplicate everything I wrote for the first ticket, and change all the occurrences of "Envelope" to "WorkItem", but it really bugs me doing so.

Is there any way to refactor the shared logic when all the class names are different, even at the ORM level?

4
  • Which programming language and which ORM are you using?
    – Doc Brown
    Commented Sep 18, 2017 at 20:26
  • @ Doc Brown We're using .Net Core C# and Entity Framework Core Commented Sep 18, 2017 at 21:03
  • 1
    Frankly, these kinds of tests may not be the best use of your time. See softwareengineering.stackexchange.com/questions/130925/… Commented Sep 18, 2017 at 23:57
  • @RobertHarvey the update operation at least has some extra constraints like it needs to validate if the specified record exists and create a warning in our database log table if not for each record specified. That and I'm under utilized at this company so writing tests gives me something to do Commented Sep 19, 2017 at 13:10

1 Answer 1

0

You can use a generic type

IItemRepository<T>
{
     T GetAttributeById(string id);
}

But you may find you have to use reflection, either to get the Id or map to the object.

Sometimes its best just to do the extra typing

1
  • I made my *attribute proxy classes inherit from an abstract class, and ended up going with the generic interface IAttributeCRUD<T> where T: abstractParent this ended up being much cleaner Commented Sep 22, 2017 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.