Showing posts with label Pipeline Development.. Show all posts
Showing posts with label Pipeline Development.. Show all posts

Wednesday, February 3, 2010

Add-In Pipeline Development Part-2

In my previous blog we looked into the architecture of the Add-In Pipeline , now its time to see practical implementation . To build a Add-In project we need following components of pipeline:-


  • Host
  • Host Views
  • Host Side Adapter
  • Contract
  • Add-in Side Adapter
  • Add-In Views
  • Add-In

We create a project for each of the above components. We also need a directory structure to implement our pipeline and for discovery of our Add-ins. Lets start with creation of directory structure, we create following folders to hold our pipeline assemblies.

  • [AddIns] :- folder to hold our add-in assemblies.Inside the folder all the add-in assemblies will be kept inside the subfolder having same name as add-in assembly.
  • [AddInSideAdapters] :- folder to hold the add-in side adapters assembly.
  • [AddInViews] :- folder to hold the add-in views assembly.
  • [Contracts] :- folder to hold the contracts assembly.
  • [HostSideAdapters] : - folder to hold host side adapter assembly.












Host and the host view assembly resides along with above folders without any subfolder.We start with creating a Contract Interface assembly :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assembly System.AddIn.Contract.
  • Add an interface code file from Add new project dialog box.Name the Interface.
  • Add using statement for System.AddIn.Contract and System.AddIn.Pipeline namespace to the Interface code file.
  • Implement IContract interface in the contract interface.
  • Add attribute [AddInContract] to the Interface.
  • Define the members of the interface. The members define the communication protocol to use between Add-In and Host.
[Click Image to see in full]









Here I define a rather simple contract , purpose is to show the basic setup for pipeline project.Although project can be scaled according to your requirments.

Next is the Views for Host and the Add-In . Host View :-

  • Add a new class library project to the same solution in visual studio.
  • Add reference of the assembly System.AddIn.Contract.
  • Add an interface code file from Add new project dialog box.
  • Define members for the Interface .Views have same members as the Contract Interface.

[Click Image to see in full]

In host view no attributes are defined . Add-In view assembly and Host view assembly are similar excepty for the attribute applied to them.

Add-In View :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract and System.AddIn.
  • Add an interface code file from Add new project dialog box.Name the Interface.
  • Add using statement for System.AddIn.Pipeline namespace.
  • Add attribute [AddInBase] to the interface.
  • Define members for the Interface .Views have same members as the Contract Interface.
[Click Image to see in full]


Next is the HostSide Adapter .It will convert the Contract to the Host View :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn, Host View assembly and Contract assembly .
  • Add an class code file from Add new project dialog box.Name the adapter class.
  • Add using statement for namespace System.AddIn.Contract,System.AddIn, Host View namespace and Contract namespace.
  • Implement the Host View interface in adapter class.
  • Add attrinute [HostAdapter] to the adapter class.

    [Click Image to see in full]

Add-In side adapter converts the view to contract.
Add-In side Adapter:-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn, Add-In View assembly and Contract assembly .
  • Add an class code file from Add new project dialog box.Name the adapter class.
  • Add using statement for namespace System.AddIn.Contract,System.AddIn, Add-In View namespace and Contract namespace.
  • Implement the Contract interface in adapter class and inherit the ContractBase class.
  • Add attrinute [AddInAdapter] to the adapter class.

[Click Image to see in full]

Now lets create Add-In :-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn and Add-In View assembly.
  • Add a class code file from Add new project dialog box.Name the Add-In class.
  • Add using statement for namespace System.AddIn.Contract,System.AddIn, Add-In View namespace .
  • Implement the Add-In view interface in add-in class.
  • Add attrinute [AddIn("EmailAddIn")] to the addin class.

[Click Image to see in full]


Finally our host that probes for the add-in and activates the add-in to consume its services.
Host:-

  • Add a new class library project to a new solution in visual studio.
  • Add reference of the assemblies System.AddIn.Contract , System.AddIn and Host View assembly.
  • Add a class code file from Add new project dialog box.Name the Add-In class.
  • Add using statement for namespace System.AddIn.Hosting, System.AddIn, Host View namespace .

[Click Image to see in full]











Host provides the code for constructing our pipeline for the communication and add-in discovery. We are assuming that our pipeline directory is in the root directory of the solution. Host provide the code for discovery of add-in and the way to activate them

We use the AddInStore class to build our pipeline directory (AddInStore.Rebuild(Path)) . After our add-in pipeline is built we use AddInStore class to find all the add-ins in the pipeline add-in directory (AddInStore.FindAddIns(typeof(IEmailHostView), Path)) . FindAddIns method returns the collection of the AddInToken type which we use to activate any particular add-in .

AddInToken addInToken = addInTokens[0];
this.emailHostView = addInToken.Activate(AddInSecurityLevel.Internet);

Activating the addintoken returns the Host view interface. Now with the host view returned by token activation we can use the service of the add-in. To see all the thing in action we have to build all the assemblies and place them in there respective folders in Pipeline directory. As previously stated that Host and HostView assemblies resides along with all other pipeline folders and are not in there seperate folders. Now if we launch our host and call the add-in discovery and activation method , we can use the add-in service.

Host hst = new Host();

hst.GetEmailAddIn();

hst.ReceiveMessage("Message");

hst.SendMessage();

That's it . We can build Extensible application with Add-In pipeline development and provide our own sdk to the add-in developers to code add-ins for our Host application. In coming blogs I will like to blog about Managed Extensibility Framework as released with .Net 4.0. It's another way to build extensible application using composition containers and dependency injection.

Sunday, January 31, 2010

Add-In Pipeline Development.

Recently I was on a project related to developing an extensible application.
.Net Framework provide many programming models for creating an extensible applications , some of them are Managed Extensibility Framework, Visual Studio Tools for Applications and Add-in Pipeline Development.

Visual Studio Tools for Application and Addin-Pipeline development leverages the Add-in development model whereas MEF uses the Dipendency Injection to extend application through extension compostion. The project I worked on was related to Visual Studio Tools for Application development. Major part of pipeline development for Host and Add-In is automated in VSTA and pipeline components are automatically managed by VSTA.

Other then VSTA we can develop the pipline for Add-In framework to work in our project from scratch. Extensible application are called Host that uses the service of Add-In's to extend themself. Add-In's are activated in Host application and extends the features or services of the Host application. So , there are two parts ,first the Add-In that provides the services to the Host and the Host application that consumes the services of Add-In.

The communication between the host and the Add-In is carried out by the pipeline that has to be created manually , although tools are available to achive this (VsPipeLine Builder). The pipeline is implemented by using the types in the System.AddIn, System.AddIn.Hosting, System.AddIn.Pipeline, and System.AddIn.Contract namespaces. The Host and Add-In are at the two end of the pipeline. Host application exposes the Object model that Add-In will use to extend the Host. So, both the Host and Add-In should share a comman Object model .
This is achived by a Contract , an interace that contains the method that both Add-In and Host will use . Contract is the abstraction between Host and Add-In through which both exchange 'types' for communication. Types that are communicated between both of them should be Contracts or Serializable .

Lets see the components of the pipeline between Host and the Add-In. We have seen the Contract that is the centeral component of the pipeline. Next there are abstract classes or Interfaces that defines the views of object model used to communicate ( As defined by Contract ), as seen by Add-In and the Host. Both these views are identical , having same members but differs in Attributes applied to them (we will see them later) .

These views represent object types and methods used to communicate as seen by the Add-In and Host. Now we have the Contract that define the protocol of communication between the Add-In and Host, we have the views that Add-In and Host have of Object Model.



The components that are missing are the components responsible for the conversion of the view to contract and contract to view.These component are called the Adapters , Host side adapter and Add-In side adapter. Lets see the communication from Add-In to Host . First the Add-In view of types are converted into contracts to pass to Host by Add-In side adapter .After the contracts are made by the Add-In side adapter , the host side adapter takes on and converts these contracts to the Host side view .In this way the serialized types are passed to the Host by Add-in.
That was the theory part , in next blog we will see the practical implementation.