IgorShare Thoughts and Ideas

Consulting and Training

Archive for the ‘Unity’ Category

Office Applications via Prism of Unity

Posted by Igor Moochnick on 03/19/2011

This article was submitted to the MSDN magazine for publication 2 years ago. It wasn’t published due to budget cuts in the magazine and my small disagreement with Glen Block on the way I’ve used the bootstrappers. Glen was busy this time with MEF and we didn’t have time to resolve our differences.

So, for what it worth, it’s a great article and helped me and my teams in a lot of ways.

Now you’ll be the judge of that if you’ll keep on reading.

This article discusses:

  • VSTO – Visual Studio Tools for Office system
  • Prism v2 – December 19 drop 8 – Composite Application Guidance for WPF and Silverlight
  • Unity Application Block 1.2 – October 2008 drop
  • WPF
  • Visual Studio 2008 Sp1
  • Introduction

    Adding integration with Office suite was always a sweet fruit that only the daring people were willing to touch. This is mostly due to a different development model that the Office suite demands – you have to specifically target your functionality to work with Office applications and develop it separately from your main application. Office integration is based on COM knowledge. At the beginning, it was hard to wrap your head around the Office COM object model, but, with the release of the VSTO (Microsoft Visual Studio Tools for Office System), it became much easier to develop the integration for the .NET applications. The use of VSTO PIA (Primary Interpol Assemblies) makes it easier but does not change the paradigm very much: it is still necessary to develop separate application components that integrate into the Office suite. Until today, it has been hard to reuse and integrate parts of a primary product with the Office suite. We need a way to create GUI applications that can be easily decomposed into separate parts and components, and re-hosted in different ways and configurations. In other words: we need a way to create composite GUI blocks that can be hosted by Office applications and Windows applications equally.

    Note that the biggest benefits of creating composite applications in general, and for Office suite in particular, are maintainability and testability. Your components can be developed separately from each other by distributed teams and tested outside of the Office applications while maintaining high quality levels of the code. This greatly reduces the complexity of the development process.

    The Office Suite applications are, by nature, composite ones. They consist of a lot of different parts and services that have to intercommunicate and coexist in a common environment. Let us take Outlook as an outstanding example. It consists from many different parts, like folder trees, explorers (folder views), inspectors (item views), ribbons, form regions, configuration tabs, etc. All of those components have to be loaded, initialized and composed in a certain way. And do not forget that, after they are loaded, they have to communicate with each other and change their views and representations in reaction to the changes in the state of their peers.

    Let us see a simplified workflow of what happens when we change an active folder in Outlook to another one:

  • A new folder is selected in a folder tree
  • Toolbar is updated to enable or disable actions that are allowed in the selected folder
  • An active Explorer clears its current items list and populates it with the list of items from the new selected folder
  • A first item in the list is automatically selected, which causes the explorer to:
  • Update the toolbar to reflect actions that can be applied to the selected item
  • Locate an appropriate inspector that knows how to render the selected item and render it in an adjoined pane (to the explorer)
  • Locate the appropriate form regions that are associated with the selected item, create appropriate custom task panes and render them
  • Note how complex the described workflow is, and adding your own functionality into it brings certain challenges.

    Outlook is not alone in this case – all the other applications from the Microsoft Office Suite, in their own different contexts, have similarly complex workflows. All of them have menu bars, ribbons tool bars, panes, regions, etc. This leads us to the question: is there a way to solve these different complexities in a common way? The answer is Yes!

    Recently released Prism (i.e. the Composite Application Guidelines for WPF) is a perfect candidate to untangle such a tight logic knot and help us create simple composite applications that can play nicely with the applications from the Office suite. It describes how we can create composite applications from modular blocks and assemble then however we need. Furthermore, this broadens the horizons of the hosting solutions. This article will take you step by step through the implementation of an application that can be executed equally as a standalone or as a hosted one. We will see how it can be hosted within an Office application as well.

    Read the rest of this entry »

    Advertisements

    Posted in Composite Applications, Office API, Prism, Tutorials, Unity, VSTO, WPF | 2 Comments »

    Where is my config file if my main module is not inside the executing folder?

    Posted by Igor Moochnick on 09/04/2008

    Some people ask me: “What if my bootstrapper is loaded by my application from a another (preconfigured) folder and my Prism/Unity config file located in the same (non-executing) folder? How do I load it?”

    It’s pretty simple. You need to take your executing assembly as the base and figure out the required path.

    var assembly = Assembly.GetExecutingAssembly();
    Uri uriPath = new Uri(assembly.CodeBase);
    string path = Path.Combine(Path.GetDirectoryName(uriPath.AbsolutePath), "myApp.config" );
    var configMap = new ExeConfigurationFileMap { ExeConfigFilename = path  };
    var configuration = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
    unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
    
    modulesSection = (ModulesConfigurationSection)configuration.GetSection("modules");
    

    Posted in IOC/DI, Prism, Tutorials, Unity | Leave a Comment »

    Prism: File-configuration-driven bootstrapper

    Posted by Igor Moochnick on 09/04/2008

    If you’d like to extract all your application configuration into a config file, this is what you can do:

    1. Create a config file. I suggest that you use a different file name than the App.config

    2. Override ConfigureContainer method in your bootstrapper

    private UnityConfigurationSection unitySection;
    
    private void GetConfigurationSection()
    {
        if  (unitySection == null)
        {
            var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "myApp.config" };
            var configuration = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
            unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
        }
    }
    
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
    
        GetConfigurationSection();
    
        try
        {
            unitySection.Containers["global"].Configure(Container);
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.WriteLine(e.ToString());
        }
    }
    

    3. Point the module enumerator to the same config file via ConfigurationStore

    protected override IModuleEnumerator GetModuleEnumerator()
    {
        string baseDir = AppDomain.CurrentDomain.BaseDirectory;
        var store = new ConfigurationStore(baseDir);
        return new ConfigurationModuleEnumerator(store);
    }
    

     

    All this is fun, but, in case you have more than one config file that contains a Modules section – the ConfigurationStore will stop reading them as soon as it’ll find a first one. It may be the file that you needed in a first place, but, as well, it may be some other file.

    Here is a simple workaround:

    private UnityConfigurationSection unitySection;
    private ModulesConfigurationSection modulesSection;
    
    private void GetConfigurationSections()
    {
        if ((unitySection == null) && (modulesSection == null))
        {
            var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "myApp.config" };
            var configuration = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
            unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
    
            modulesSection = (ModulesConfigurationSection)configuration.GetSection("modules");
        }
    }
    
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
    
        GetConfigurationSections();
    
        try
        {
            unitySection.Containers["global"].Configure(Container);
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.WriteLine(e.ToString());
        }
    }
    
    protected override IModuleEnumerator GetModuleEnumerator()
    {
        GetConfigurationSections();
    
        return new ConfigurationModuleEnumerator(new MyConfigStore(modulesSection));
    }
    
    public class MyConfigStore : ConfigurationStore
    {
        public ModulesConfigurationSection Section { get; private set; }
    
        public MyConfigStore(ModulesConfigurationSection section)
        {
            Section = section;
        }
    
        public override ModulesConfigurationSection RetrieveModuleConfigurationSection()
        {
            return Section;
        }
    }
    

     

    As you can see, by a simple overload of the ConfigurationStore, you can return exactly the modules section that you need. Note that this section is pre-retrieved by the same function that retrieves the “unity” configuration section.

    Posted in IOC/DI, Prism, Tutorials, Unity, WPF | Leave a Comment »

     
    %d bloggers like this: