SwEx.AddIn frameworks provides utility class to manage document life cycle by creating a specified instance handler as a wrapper of a model.

Define the document handler either by implementing the IDocumentHandler interface or DocumentHandler class. Later one handles some commonly used events which can be overridden, such as document activation, loading and storing from 3rd party storage and store.

public class MyDocHandler : DocumentHandler
{
    public override void OnInit()
    {
        if (Model is PartDoc)
        {
            (Model as PartDoc).AddItemNotify += OnAddItemNotify;
        }
        //TODO: handle other doc types
    }

    private int OnAddItemNotify(int EntityType, string itemName)
    {
        //Implement
        return 0;
    }

    public override void OnDestroy()
    {
        if (Model is PartDoc)
        {
            (Model as PartDoc).AddItemNotify -= OnAddItemNotify;
        }
    }
}

Call ISwAddInEx.CreateDocumentsHandler method and pass the type of document handler as a generic argument

private IDocumentsHandler<MyDocHandler> m_DocHandler;

public override bool OnConnect()
{
    m_DocHandler = CreateDocumentsHandler<MyDocHandler>();
    return true;
}

Override methods of document handler and implement required functionality attached for each specific SOLIDWORKS model (such as handle events, load, write data etc.)

Framework will automatically dispose the handler. Unsubscribe from the custom events within the Dispose or OnDestroy method. The pointer to the document attached to the handler is assigned to Model property.