3

In a shared library I have a function that is widely used. I'm implementing a requirement for the function to override its behavior when the entities are in a specific state.

I have decided on implementing this requirement with the decorator pattern.

All of my clinets program against an interface for this service and most of them use a IoC container, others use bastard injection.

How should I force or guide the clients to use my decorator? Without my decorator the service wouldn't do the new requirement for the system.

I was thinking of having the service implementation internal and have a public factory for creating the service as an interface but then my factory couldn't use the lifestyles of the IoC container.

Any suggestions?

9
  • @Ixrec I deleted my previous question, I then decided to create a new one more specific without any implementation details. Do you understand my problem now? – Jakob Aug 16 '15 at 15:37
  • "Without my decorator the services wouldn't work as required" - what's the problem then? If the service wouldn't work without the decorator, your clients will be forced to modify their code to use the decorator. After all, it's their interest to have their code working... – Idan Arye Aug 16 '15 at 15:39
  • @IdanArye the service would work but would not do that extra thing required. If I would implement this into the service without the decorator pattern then my clients would only have to get a new version and everything would work. Do you catch my drift? – Jakob Aug 16 '15 at 15:44
  • "If I would implement this into the service without the decorator pattern then my clients would only have to get a new version and everything would work" - then why don't you do exactly this? – Doc Brown Aug 16 '15 at 17:07
  • @DocBrown Because then my unit test would be more complex, the operation is complex and I don't want to complicate my core service and its test because of a override in some situations. – Jakob Aug 16 '15 at 17:20
4

I would suggest using a facade object to hide the detail of the requirement for the decorator. The facade would simply create the service and its decorator behind the scenes, and then forward client requests to them. By presenting a simpler interface you make it easier for clients to use your service, avoid the need for a separate factory api and make it easier for you to add new features in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.