Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow override of model element registration in DI #424

Merged
merged 1 commit into from
Feb 9, 2024

Conversation

gfontorbe
Copy link
Contributor

Allow the element registration from the dependency injection to be overridden by another ContainerModule. This comes in handy when working with exported containers from the sprotty-library or when sharing a ContainerModule when only a subset of model elements need a new registration.

This introduces a new helper function overrideModelElement to be used instead of configureModelElement in the ContainerModule

Example:

const moduleA = new ContainerModule((bind, unbind, isBound, rebind) => {
...
configureModelElement(context, 'myElementType', MyElementImplA, MyElementViewA);
}

const moduleB = new ContainerModule((bind, unbind, isBound, rebind) => {
...
overrideModelElement(context, 'myElementType', MyElementImplB, MyElementViewB);
}

const container = new Container();
loadDefaultModules(container);
container.load(moduleA);
container.load(moduleB);

In this scenario, myElementType elements will first be linked to MyElementImplA and MyElementViewA before being linked to MyElementImplB and MyElementViewB.

If the order of the functions container.load(...) are reversed, so is the link between the element type and its implementation/view.

Error handling:
By design, we can only override an element that has already been configured.

  • Trying to use configureModelElement on an element type that is already registered will throw an error
  • Trying to use overrideModelElement on an element type that is not already registered will throw an error

@sailingKieler
Copy link

I like it! 👍

Copy link
Contributor

@spoenemann spoenemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great enhancement!

@gfontorbe gfontorbe merged commit d5e7ff5 into master Feb 9, 2024
2 of 3 checks passed
@gfontorbe gfontorbe deleted the gfontorbe/override-configureElement branch February 9, 2024 16:03
@spoenemann spoenemann added this to the v1.2.0 milestone Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants