+-------------+ +--------+ +----------+
| repository +-------->+service +------->+controller|
+-------------+ +-^------+ +------------+
+-------------+ | +^ ++------>+controller2 |
| repository2 +-----------+ | | +------------+
+-------------+ | +------>+controller3 |
+------------+ | +------------+
| repository3+---------------+
+------------+
I need to have the ability to help a number of totally different implementations of my repository interface. I take advantage of my interface in by enterprise service and I can choose what ever implementation I need. To date so good.
When I attempt to insert a number of entries with the identical key, db connector will throw an exception. In case of jdbcTemplate, this might be DuplicateKeyException, in case of Hibernate, it will probably be some form of Constraint Exception. Not supreme, however nonetheless ok.
I need to have the ability to inform the service shopper what precisely went mistaken any why, the place passing the exception isn’t good enaugh (as in response 500). Getting muddy.
So my choices are both to catch that exception within the controller, which then I’ve to implement in every controller (which appears muddy waters to me), or, I can throw some form of exception within the enterprise service (which appears higher) after which deal with that within the controller. I believe that is higher as a result of then I will be catching my very own certainly one of a form exception, and I am positive what is going on on.
The issue is, most of my enterprise service implementation is definitely an current interface implementation, which implies that in an effort to do that, I’ve to override the interface I am utilizing with new declarations which may have exceptions. This isn’t what I need.
What’s the correct strategy to deal with this type of scenario? I am utilizing Spring however I would love a extra basic reply.