Grails 服务层次结构
假设我有 10 个 grails 服务。这些服务中的每一项都会调用 REST 服务。因此,我想封装 REST 代码,以便 10 个服务可以轻松地重用它。
在考虑选项时,我可以:
1)使用 REST 相关代码创建另一个服务或 bean,并将其注入 10 个服务中的每一个。
2) 创建一个包含 REST 代码的超类服务,并让所有 10 个 grails 服务扩展该类。
我正在尝试使用选项 2,但遇到了将依赖项注入到超类中的问题。
示例:
class SuperService {
def aString
}
class ExampleService extends SuperService {
}
resources.groovy:
beans = {
superService(SuperService) {
aString = "something"
}
exampleService(ExampleService) {
}
}
当我在运行集成测试时在调试器中运行它时,我看到 aString 的值为 null。显然,这对我来说是个问题。
正如您所期望的,使用以下 resources.groovy: 和 aString = "something" 运行相同的代码
beans = {
superService(SuperService) {
}
exampleService(ExampleService) {
aString = "something"
}
}
。
所以,我更喜欢选项 2,因为它会减少配置接线,但我认为这不是可行的方法。换句话说,如果我必须在每个子类中设置 aString ,则没有任何价值。
想法?
我错过了什么吗?
我也对其他选择持开放态度。
提前致谢, 托德
Let's say I have 10 grails services. Each one of these services will be making calls to a REST service. So, I'd like to encapsulate the REST code so it can be easily resused by the 10 services.
In considering options, I could:
1) create another service or bean with REST related code and inject it every one of the 10 services.
2) create a superclass service containing the REST code and have all 10 grails services extend this class.
I'm trying to go with option 2, but running into problems with dependency injection into the superclass.
Example:
class SuperService {
def aString
}
class ExampleService extends SuperService {
}
resources.groovy:
beans = {
superService(SuperService) {
aString = "something"
}
exampleService(ExampleService) {
}
}
When I run this in an debugger while running an integration test, I see the value of aString is null. Obviously, that's going to be problematic for me.
As you might expect, running the same code with following resources.groovy:
beans = {
superService(SuperService) {
}
exampleService(ExampleService) {
aString = "something"
}
}
and aString = "something".
So, I prefer option 2 because it will be less configuration wiring, but I don't think it's going to be feasible approach. In other words, there's no value if I have to set aString in each one of the subclasses.
Thoughts?
Am I missing something?
I'm open to other options as well.
Thanks in advance,
Todd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就“DRY”而言,选项#2 的想法是正确的——但它实际上只是一行。如果与 superService 对象唯一的共同点是服务,而没有其他方法,那么您实际上并没有为自己节省任何工作。如果有的话,只是声明一个用于依赖注入的超类会使事情变得更加隐藏并且可能更难以维护。
听起来您建议将 REST 方法放入 superService 中,所以不确定为什么您担心将依赖项注入到 superService 中 - 您希望您的方法执行子类能够调用的 REST 交互。或者您正在尝试结合选项 1 和 2?
You have the right idea with option #2 in terms of being DRY -- but it's really just one line. If the only thing in common with the superService object is the service, and no other methods, then you're really not saving yourself any work. If anything, just declaring a super class for a dependency injection makes things more hidden and possibly harder to maintain.
It sounded like your proposing putting the REST methods in the superService, so not sure why you're concerned about dependency injection into the superService -- you'd want your methods there to perform the REST interactions that and subclass would be able to call. Or are you attempting a combination of options 1 and 2?