通用域方法的继承或服务? [英] Inheritance or Service for common Domain methods?
问题描述
我有一些在我的一些域类中很常见的方法。我想减少复制代码的数量,并且想到两个解决方案:
1)将普通方法放在一个baseDomain类中,并在我的域中继承它的方法
2)Put常见的DomainMethodService中可以导入到我的域中的常用方法
我想我应该单独留下继承,除非域共享公共属性,但我不确定。这些方法之一是否比其他方法更有益?
例如,根据参数比较两个域实例的方法:
<$
返回//用于比较基于属性类型的实例的一些逻辑
}
code>
对于这种情况,我会使用 Mixin
: Mixin Reading 和< a href =http://groovy.codehaus.org/Category+and+Mixin+transformations =nofollow>进一步的Mixin阅读
例如:
@Mixin(MyCompare)
class DomainA {
}
@Mixin(MyCompare)
class DomainB {
}
$ b $ class MyCompare {
def比较(instance1,instance2,propertyName){
instance1 [propertyName] == instance2 [propertyname]
}
}
现在,所有DomainA和DomainB的实例都会包含比较
方法。通过这种方式,您可以实现多个Mixin,以便将功能添加到您想要的域类中,而无需扩展超类或在服务中实现它。 (我假设你希望你的比较
方法比这更复杂一点,但你明白了)
一些潜在的问题:
<1>在 mixin
中的私有方法似乎不起作用。
<2>具有 mixin
的循环依赖也很糟糕:MixinClassA在MixinClassB中混合,MixinClassB在MixinClassA中混合(对于您的设置,我认为您不会在其他 mixin
的s)中混入 mixin
的混音。
3)我忘记了方法碰撞会发生什么,所以你应该试验一下。示例:ClassA具有doStuff()方法,并在DoStuffMixin中混合,该方法也具有doStuff()方法。
4)请记住,在您用作 this
这将是使用 mixin
的对象的实例。例如在上面的例子中,你可以删除 instance1
,并用这个
替换它。在运行时这个
将是DomainA或DomainB的一个实例(我认为它是mixin中非常强大的一部分)。
这些都是我能想到的大问题。
I have some methods that are common across a few of my domain classes. I want to reduce the amount of replicated code and two solutions came to mind:
1) Put the common methods in a baseDomain class and inherit the methods from it in my domains
2) Put the common methods in a commonDomainMethodService which I can import into my domains
I am thinking I should leave inheritance alone unless the domains share common properties, but I'm not sure. Is one of these methods more beneficial than the other? Is one more in line with Grails best practices?
For example a method that compares two domain instances based on a parameter:
int compareInstances(instance, otherInstance, propertyName){
return //some logic to compare the instances based on the type of property
}
For that case I would use a Mixin
: Mixin Reading and Further Mixin Reading
For example:
@Mixin(MyCompare)
class DomainA {
}
@Mixin(MyCompare)
class DomainB {
}
class MyCompare {
def compare(instance1, instance2, propertyName) {
instance1[propertyName] == instance2[propertyname]
}
}
Now all instances of DomainA and DomainB would have your compare
method. This way you can implement multiple Mixins to add functionality to the domain classes that you want to without having to extend a super class or implement it in a service. (I'd assume that you'd want your compare
method to be a little more sophisticated than this but you get the idea)
Some potential Gotchas:
1) private methods within mixin
's seem to not work.
2) having a circular dependency of mixin
's is also bad: MixinClassA mixes in MixinClassB which mixes in MixinClassA (for your setup I don't think you would have mixin
's mixing in other mixin
's).
3) I forget what happens with a method collision so you should probably experiment. Example: ClassA has a doStuff() method and mixes in DoStuffMixin which also has a doStuff() method.
4) Keep in mind that in a class that you're using as a mixin
you can refer to this
which will be the instance of the object that is using the mixin
. For instance in the example above you could remove the instance1
and replace it with this
. At runtime this
will be either an instance of DomainA or DomainB (which I feel is a very powerful part of mixins).
Those are the big gotchas I can think of.
这篇关于通用域方法的继承或服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!