如果相同的业务逻辑适用于这两个帐户,但考虑到你不能有一个单一的服务类说话的两个帐户的API,然后是你可以有2个服务类(这只不过是2个不同的春天bean)与默认单例作用域。
class Account1Service{
}
class Account2Service{
}
我也会尝试如果我可以在这里使用继承在这种情况下,如果我有共同的逻辑,可以共享。但请记住,如果你是从一个抽象类,然后抽象类必须放置在src/groovy
就是外面/grails-app/
违抗依赖注入继承服务类。在这种情况下,你可能最终得到(未经测试,但你能不能坚持DRY概念)
// src/groovy
abstract class BrokerageService {
def populateAccountDetails(Long accountId)
def checkAccountStatus(Long accountId)
}
//grails-app/services
class Account1Service extends BrokerageService {
//Implement methods + add logic particular to Account1
//By default transacitonal
}
class Account2Service extends BrokerageService {
//Implement methods + add logic particular to Account2
//By default transacitonal
}
也保持了一份说明,范围singleton
,你会格外小心(最好避免)保持在全球范围的性质服务类。尽量使无国籍尽可能。除非另有情况或业务逻辑的要求使用服务水平范围像session
,flow
或request
,我会一直坚持到默认singleton
范围。
要回答你的第二个问题,你不需要任何实例使用Grails服务类的。当使用适当的命名法时,容器会注入适当的服务类(使用Spring IoC)。
//camelCase lower initial
def account1Service
def account2Service
UPDATE
这是为了应对所提供的附加信息:在上面的例子中,服务类将自动如果你在那里你婉TTO使用的服务按照类命名约定注入OP。
参照上述场景,在默认的singleton
范围内只能有一个service
类来处理事情。最好的部分是,由于您要离开您的网络,并且不担心自己的数据库事务,因此服务类可以设置为非事务性。但这又取决于情况需要。以下是服务类的外观。
//grails-app/service
class BrokerageService{
//Service method to be called from controller or any endpoint
def callServiceMethod(Long accountId){
.......
doSomethingCommonToAllAccounts()
.........
def _ibConfig = [:] << lookupIBGatewayConfigForAccount(accountId)
........
//Configure an IB Gateway according to the credentials
//Call IB Gateway for Account using data got from _ibConfig map
//Call goes here
}
def doSomethingCommonToAllAccounts(){
........
........
}
def lookupIBGatewayConfigForAccount(accountId){
def configMap = [:]
//Here lookup the required IP, account credentials for the provided account
//If required lookup from database, if you think the list of accounts would grow
//For example, if account is JPMorgan, get credentials related to JPMorgan
//put everything in map
configMap << [ip: "xxx.xx.xx.xxx", port: 80, userName: "Dummy"] //etc
return configMap
}
}
服务类的范围是单这意味着有将只有一个堆中类,这也意味着,任何类级别属性(比其他方法)将是有状态的实例。在这种情况下,你只处理将是无状态的方法,并且足以达到目的。每次交易发生时,您都可以获得所需的东西,而无需花费堆积或创建新的BrokerageService
实例。
每笔交易(包括一个账户)最终都会调用服务,从数据库(或配置属性或平面文件或属性文件)查找凭证,然后配置IB网关并呼叫/通话给网关。
当你说服务,你的意思是Grails服务?为什么你觉得这是最好的方式为每个帐户创建服务类的新实例,而不是使用默认的'singleton'范围? – dmahapatro
是的,Grails服务。我不确定你是什么意思。我期望每个服务是一个单身人士,运行相同的代码,但与包含帐号的实例字段。您是否建议同一个单独服务处理这两个帐户的订单?如果是这样,由于API的设计方式,这实际上不起作用。我需要为每个帐户提供专门的服务。 – greymatter
关于可用服务范围的更多详细信息,请参阅Elias访问[本页](http://grails.org/doc/2.2.1/guide/services.html#scopedServices)所述的答案。 – dmahapatro