简体   繁体   中英

Is it bad to use classes from other frameworks?

Is it bad to use classes, enums etc from other frameworks? I have some finder methods in my repository which is based on JPA, and I want to pass the fetch type (whether it should load the objects lazily or eagerly) and the FetchType enum from JPA framework suit the purpose. But is it better to create your own enum/class? I understand that it makes a coupling between my app and the framework, but my app is already using JPA.

It seems you have answered your own question.

... the FetchType enum from JPA framework suit the purpose.

If JPA is already a dependency for your application, and their types are suitable for your use cases, there is no reason you should reinvent the wheel to avoid using JPA types.

PS Gamb makes a very good point in his answer when he suggests constraining use of the types to a persistence layer.

Indeed, you can use the FetchType enumeration but consider where are you using it. If you have a layered design, avoid using that enum in any layer but the Persistence layer, otherwise you'll have to import references from JPA in higher layers (if you need to).

Another approach (Since you have only EAGER and LAZY ) is to define if a field should be fetched and assign that condition a boolean value, like a lazyLoad flag.

Like robjb already said, there is no need to reinvent the wheel. This is the whole purpose of frameworks: Give the developer Tools and Helpers to simplify standard tasks. So I suggest to use the Framework-Classes. And if they don't match you needs to 100%, you can always extend them to suid your needs - again, there is no reason to reinvent the whole wheel if all that is missing is a tiny screw :-P

Even if you are already using the framework, I would keep the upper layers of your logic free of it as long as it is possible. Reasons:

  • someone reading your code and seeing you use class from the framework will assume that it is used to communicate for your framework (ok, that is weak).

  • in the event you want to change, it will be easier if you only used the classes from the framework to interact with it.

Nothing of this is written in stone, though. If developing the class yourself would be a high investment of time, you can decide to go for it and hope that nobody wants to change the engine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM