Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Let's say I have a repository of functions. These functions work with business objects within a database, CMS or other third party API. For example, my repository interacts with virtual files and documents in a document management system. I want to be able to implement a simulation mode in my repository such that when I execute commands, I want to see the result of it without it actually doing anything. This is to reduce the risk of someone executing a series of commands in my repository and doing something dangerous. I want them to first see the output and confirm before executing.

My question is, what are some good patterns to implement a simulation mode for a repository? I can think of a few off the top of my hat:

Wrap all business objects in another class, such that when you would call File.Delete, your actually calling the wrapper class and it checks in simulation mode is enabled. The problem is that many third party libraries and SDKs don't let you subclass their business objects. How would you pass in the the simulation mode (everytime an object is instantiated?).

Another option would be to implement a SimulatedRepository. However, I don't want to duplicate any existing code in the repository. Meaning, I don't want to simulate everything to the point that it does nothing, I only want to simulate create, update and delete commands, not the business logic. Perhaps I need to implement another layer in my application?

Suggestions would be appreciated. I also wonder if this design philosophy is popular in most enterprise environments and how its done. I would think that allowing administrators to run a simulation and measure results before actually executing them is vital in a mission critical scenario where you are working with another client's data.

share|improve this question

migrated from codereview.stackexchange.com Aug 10 '12 at 0:30

This question came from our site for peer programmer code reviews.

1 Answer 1

One method of achieving the results you're after (albeit it might be considered a little overkill) is by using an adapter pattern. You could feed whatever classes you're using an adapter implementation based on the state at execution time.

For example, you could have an adapter implementation that does nothing but print out what it's about to do. Another implementation could extend that adapter and add the functionality to actually execute the tasks.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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