[TYPO3-mvc] Using interfaces for dependency injection on domain models
Christian Zenker
christian.zenker at 599media.de
Thu Mar 31 11:19:35 CEST 2011
On Wed, 30 Mar 2011 20:14:30 +0200, Thomas "Thasmo" Deinhamer
<thasmo at gmail.com> wrote:
> What would then be the purpose
> for domain models using interfaces?
The one I described above: If your model could be based on different
tables/ extensions. Take fe_users and tt_address as example. They both
could serve as information on a person, but their fields are named
differently. So if I wanted the integrator to decide which table to use, I
would define an interface that basically holds the getters (and probably
setters) for all the defined values. This way both models could use the
same template.
> E.g. I got several "custom" methods
> inside of some of the domain models,
> which return not direct model properties,
> but calculated data etc.
>
> Wouldn't it make sense to use an
> interface which defines those methods,
> to ensure extended models will also
> use those methods for compatibility?
Especially if their is much custom logic I would *NOT* use an interface. :)
I don't know, what your specific extension does, but in general I would
assume that an integrator does not want to *completely* rewrite your
model, but use at least the logic of some of the methods you have written.
If the integrator would program against the interface he would also have
to copy&paste the logic you wrote in your model. If you encounter a bug in
your logic and fix it, it won't be fixed in the model, the integrator
wrote.
So, if the integrator wanted to work cleanly, he would extend your model
so he won't have to copy&paste your logic. But this makes the interface
useless - the integrator would not be able to change the signature of your
methods or delete methods if he extends your model anyways.
Don't get me wrong: Of course you could use an interface. It's never
"wrong" to use an interface. But I find it annoying to keep both the
interface and the class up to date. Each time you change the signature of
a method or add a method, you have to modify the class and the interface.
Especially your Domain Model will be very likely to get properties added
("Why can't I store an image to that record?", etc.). But if you add a
method to your interface, everyone who built a class upon that interface
has to add that method, too. If he does not, Fatal Errors will be flying
all around, making your plugin (or even the whole TYPO3 installation)
unaccessible. If you just add a method to your class, nothing bad will
happen (exceptions prove the rule) as the method is also present in the
class the integrator extended from your class.
Again: After trading of the advantages and disadvantages of interfaces of
domain model *I* would not use them in general. But it's perfectly fine,
if *you* did.
Christian.
More information about the TYPO3-project-typo3v4mvc
mailing list