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?

  • Which programming language and which ORM are you using? – Doc Brown Sep 18 '17 at 20:26
  • @ Doc Brown We're using .Net Core C# and Entity Framework Core – Austin_Anderson Sep 18 '17 at 21:03
  • 1
    Frankly, these kinds of tests may not be the best use of your time. See softwareengineering.stackexchange.com/questions/130925/… – Robert Harvey Sep 18 '17 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 – Austin_Anderson Sep 19 '17 at 13:10
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

  • 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 – Austin_Anderson Sep 22 '17 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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