Thinking about the overall architecture of the application logic:

Current status: The model contains all resources that are used at runtime. In a factory you register a builder which is used to create a resource via the factory. Every time the factory creates a resource, it adds the resource to the model. That means the factory knows about the model and keeps a reference to it.

The resources can be versioned. Each version of a resource is a child of a base item in the model, and the model is designed like a 2-level tree. The 1st level contains the base resource and the 2nd level all versions of it. The model item of the base resource always points to the first child, e.g. the latest version.

Now to the question: What if you want to create a new version of a resource at runtime? These are some options to initiate the workflow:

  1. Create a resource via the factory. Be sure that the new resource is added to the model. This is easy if the factory knows about the model. But is this good design?
  2. Call a method from the model that adds a new version of an existing resource. But for this to work, the model needs to know about the factory, which is not appropriate I think. And maybe the factory already knows about the model (see option 1). From my point of view this design smells.
  3. Call a method from the resource in order to create a new version of itself. Again this is not appropriate I think, because how is ensured that the model knows about the new resource? The resource should not know that the model even exists.

Please share your thoughts about the right place for a method to create a new version. Is one option more commonly used than another? Are there other more popular options?

  • Which derp is downvoting for no obvious reason, and not even making a comment? – user2366975 Mar 2 '18 at 9:03

It depends on who is logically responsible for the resources.

Model as Repository (Option 1)

If the model is simply tasked with holding on to resources that may come from somewhere else (especially if you have multiple sources that can create resources), I would go with your option 1.

Whether the factory should also be responsible for registering the resources with the model depends on your application. If all resources must be registered with the same model (or the model their base version comes from) then it probably makes sense.

Model as Resource Manager (Option 2)

If the resources are owned by the model, it may make more sense to inject the factory into the model and have the model provide a CreateXyzResource() method. The creation of the resource can then be delegated to the factory (which becomes a detail) and the model can register the newly created resource.

In this case, it might not be necessary for the factory to depend on the model because the model can provide the relevant data to the factory.

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.