Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Without reflection is it possible to determine the class to run

So i'm in a situation, where i would like to have a database of objects that contain classes to run. It would look something like:

id  | Description       | pricingClass
1   | "static pricing"  | staticDeliveryPrice.class
2   | "Dynamic pricing" | dynamicDeliveryPrice.class

The purpose of this is to allow flexibility in my coding. My line of thought is, this would allow for multiple ways of determining how to figure out a price of a deliveryMethod.

The problem I'm worried about is, is reflection bad to use in this scenario? Is there a better way to do it? Is it following the solid principles.(I would say yes, but part of me disagree).

like image 449
Harry Avatar asked Mar 09 '23 22:03

Harry


1 Answers

You're describing a plugin-based architecture. Do you really need this kind of flexibility ? It's usually necessary when:

  • people outside your project need to add new features to your code without your intervention
  • you want to allow runtime modification of your program.

Otherwise, it's completely overkill. You have full control on what goes in your product, so you can simply rely on good old polymorphism to isolate the different implementations of your feature.

Also, it's not a very good thing to put business logic in the database: you mix concerns, you create strong coupling (suddenly renaming classes will become annoying!), and of course, it's harder to reason about what happens in your code by just looking at it.

like image 131
Fabien Benoit-Koch Avatar answered Mar 12 '23 12:03

Fabien Benoit-Koch