You can use Snippets in MDriven Designer to capture a reusable pattern from a ViewModel, a class, or another part of your model, then apply that pattern in the same or a different model.
A snippet is a structured copy/paste pattern. It stores selected model content as text, can expose values for replacement when it is applied, and is useful when you repeatedly build the same model structure.
For example, if several classes need an “all instances” ViewModel with Create and Delete actions, build the pattern once, save it as a snippet, and apply it for each class instead of recreating the ViewModel and actions by hand.
When to use snippets
Use a snippet when you want to reuse a pattern without making it a permanent part of every model.
| Need | Use a snippet when | Example |
|---|---|---|
| Reuse a ViewModel pattern | The same ViewModel structure and actions recur for different classes. | Apply an “all instances” grid, Create action, and Delete action to several business classes. |
| Reuse a model fragment | You want to copy a class fragment or another selected model pattern. | Apply a previously captured structure to a new model and adapt its names through replacements. |
| Share a lightweight pattern | You want to export a pattern and import it into another model. | Export a tab-set ViewModel pattern and give it to another model author. |
| Bring in a larger, complete capability | The reusable content is delivered as a mergeable model rather than a small pattern. | Use a mergeable model from Documentation:Ecomdl rather than recreating its contents as a snippet. |
Snippets are not limited to ViewModels. You can use them for parts of classes or other parts of the model. A snippet is appropriate for repeated structure; it does not replace normal modeling decisions about whether a concept should be represented as a reusable class or association.
Create a snippet
Create the pattern in a working model first. Verify that it has the structure, names, actions, and opt-outs you want users of the snippet to receive.
- In MDriven Designer, select and copy the model elements that make up the pattern. Copy/paste is available at all levels of the model.
- Open the Snippets menu and choose Edit Snippets.
- Create a new snippet.
- Enter a clear name and a description that tells another model author what the snippet creates and what they need to provide.
- Paste the copied model content into the snippet definition using the clipboard import control in the snippet editor.
- Add every related element that the snippet must create. A ViewModel and its actions may need to be captured as separate snippet content.
- Define replacements for names or other recurring text that must change when the snippet is applied.
- Save the model.
Include dependent actions explicitly
Capturing a ViewModel does not automatically mean that every related action is included. In the “all instances” example, applying a snippet that contains only the ViewModel creates the ViewModel but does not create its Create and Delete actions.
To make the snippet complete, add the actions to the snippet and mark them as included through the snippet editor. If you discover that an element is missing after applying a snippet, use Undo, update the snippet, and apply it again.
Add replacements
A replacement is text in the stored snippet that the user supplies when applying it. Replacements let one pattern work for multiple model names.
For example, a pattern built for the class Test1 can define Test1 as a replacement. When you apply the snippet to create a ViewModel for Car, enter Car as the replacement value. MDriven Designer replaces occurrences of Test1 in the captured content, including the ViewModel and the included actions.
Use a replacement for every name that must vary between uses. A snippet can have more than one replacement.
| Snippet text | Replacement prompt | Value supplied when applying | Result |
|---|---|---|---|
Test1
|
Class name | Car
|
The applied ViewModel and included actions use Car where the pattern used Test1.
|
Write the replacement description as an instruction. For example: Enter the class name that this all-instances ViewModel should use.
Apply a snippet
- Open the target model and navigate to the part of the model where you want the pattern.
- Open Snippets.
- Select the snippet to apply.
- Enter values for the snippet’s replacements. Leave a value at its default only when that default is correct for the target model.
- Select Apply.
- Inspect the created model content. Check that all expected ViewModel elements, actions, and references were created.
- If the result is not correct, use Undo, adjust the snippet definition or replacement values, and apply it again.
Example: create an all-instances ViewModel pattern
This example captures a repeated UI pattern for a class named Test1.
- Create a ViewModel rooted in
Test1. - Add an “all instances” presentation and place the attributes that should appear in its grid.
- Add a global action that opens the ViewModel.
- Add a Create action for
Test1. - Add a Delete action for
Test1where it should be available. - Opt out of the Delete action where it should not appear, such as a root context that has no
Test1instance. - Copy the ViewModel and the actions, then add them all to a new snippet.
- Define
Test1as a replacement named, for example, Class name. - Apply the snippet for
Carand supplyCaras the replacement value.
The resulting pattern gives Car an all-instances ViewModel and the corresponding actions, while retaining the captured action placement and opt-out behavior.
Import and export snippets
Export a snippet when you want to use it in another model or share a repeatable pattern with other model authors. Import the snippet into the target model before applying it.
- Export the completed snippet under a meaningful name.
- In the target model, open Snippets.
- Import the snippet file.
- Apply the imported snippet and provide its replacement values.
- Save the target model.
Imported snippets are loaded into the model. After you save and reopen the model, the loaded snippet remains available. You can also return to Edit Snippets to inspect or change a loaded snippet.
Use snippets with component patterns
A snippet can describe the model structure that a UI pattern needs. For example, a tab-set pattern can add a placing container, buttons, and expressions that select the active tab. A configurable snippet can ask for the variable used to hold the current tab, allowing the same structure to be applied with a model-specific variable name.
After applying such a snippet, inspect the ViewModel tree. Confirm the added containers, widgets, style expressions, and variables match the pattern’s intended behavior before relying on it in the application.
Design guidelines
- Keep each snippet focused on one repeatable job. For example, create one snippet for an all-instances ViewModel and another for a tab-set layout.
- Give the snippet a task-oriented name, such as All instances with Create and Delete actions.
- Describe prerequisites and replacement values in the snippet description.
- Include all elements required for the pattern to work, especially related actions.
- Use replacements rather than manually editing repeated names after every application.
- Test the snippet in a separate model before distributing it.
- Use a mergeable model when the reusable item is a larger model capability; see Documentation:Ecomdl.
Snippets and model-snippet services
MDriven Designer snippets are reusable patterns that you create, edit, export, import, and apply in the Designer. If you need a model fragment built from a written requirement, see Documentation:User-Paid-Model-Snippet. That service concerns obtaining a model snippet; this page explains the Designer feature used to reuse snippet patterns.
