Developing an Object Model for Win Apps - Centralized Access

In the previous post, I talked about developing an object model, referencing the article written by Omar AL Zabir.  At the heart of it is a class I called Manager.  This class exposes all of the UI elements that the application.  To make the manager class statically available, you have to add this reference at the top.  This creates a new instance of the Manager class and makes it statically available, also including an Instance property.

private readonly static Manager _instance = new Manager();

public static Manager Instance
{
    get { return _instance; }
}

Through this instance object, you can expose UI elements and other objects statically through these definitions:

public static MenuCollection Menus
{
    get { return Instance._menus; }
}

public static ToolbarCollection Toolbars
{
    get { return Instance._toolbars; }
}

public static ToolWindowDocumentCollection ToolWindows
{
    get { return Instance._toolWindows; }
}

The Instance property returns the access to the private variables as described below:

private DocumentCollection _documents = null;
private ToolbarCollection _toolbars = null;
private ToolWindowDocumentCollection _toolWindows = null;

So, we have an Instance object made globally throughout the application, which contains our reference to the _documents, _toolbars, and _toolWindows collections.  These variables don't need to be static; however, the properties should be.

You can also define static methods as helper methods or that have some other functions.  This is one of my methods that I defined to append the base directory path to the partial path:

public static string CreatePath(string partialPath)
{
    //Make sure the partial path is provided
    if (string.IsNullOrEmpty(partialPath))
        throw new ArgumentNullException("partialPath", "The partial path is null");

    //if not starting with a backslash, add it
    if (!partialPath.StartsWith(@"\"))
        partialPath = @"\" + partialPath;

    //Return the directory
    return BaseDirectory + partialPath;
}

These are some of the ways you make objects statically available to the application.  I'll illustrate uses of them soon.

Comments

No Comments