Share via


How to: Override Methods That Run When a Notification Occurs

When a domain model changes, you might want to be notified of the changes. To receive notification, you can override methods that are used by the domain model or by elements in the model.

Two helpful methods that you can override when a notification occurs include:

  • OnValueChanged

  • OnValueChanging

To override a method that runs when notification occurs

  1. Open a domain-specific language solution.

  2. In Solution Explorer, expand the GeneratedCode folder.

    Note

    Each domain-specific language solution has two GeneratedCode folders. Under the DSL node, the GeneratedCode folder contains the classes that users can override in the generated designer. Under the DslPackage node, the GeneratedCode folder contains the classes that users can override in the Domain-Specific Language Designer.

  3. Locate the generated class that has the method that you want to override.

  4. Under the DSL node or the DslPackage node, depending on which node contains the class that you want to override, add a C# file.

    This file will contain your custom code.

  5. In the C# file, create a partial class for the method that you want to override.

  6. Add the method override with your custom notification instructions.

Example

The following example overrides DomainClasses.cs and notifies the user when a property for the ExampleElement domain class has changed.

using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslDesign = global::Microsoft.VisualStudio.Modeling.Design;

namespace msft.FieldChangeSample
    {
    public partial class ExampleElement
    {
        internal sealed partial class NamePropertyHandler
        {

            protected override void OnValueChanged(ExampleElement element, string oldValue, string newValue)
            {
                System.Windows.Forms.MessageBox.Show("Value Has Changed");
                base.OnValueChanged(element, oldValue, newValue);
            }
        }
    }
}

See Also

Tasks

How to: Register to be Notified on an Event

Reference

OnValueChanged

Other Resources

Domain-Specific Language Tools Glossary

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.