Silverlight and Unity.Configuration

Aug 19, 2008 at 4:19 PM
Edited Aug 20, 2008 at 2:52 PM
Hi,

Firstly, as a newbie to this forum I would just like to say 'Great Work Everybody' - the discussions and source has really helped me understand the opportunites / drawbacks there is when building Composite Silverlight Applications.

My question is 'I was wondering if anyone out there was considering how Unity.Configuration was going to work with Silverlight?'

My scenario is I want 3rd parties to be able to inject their own modules at run-time (i.e. I don't want to have to reference them in my project) into regions I have specifically exposed to them - basically I'm allowing 3rd parties to extend my application.

Originally I had thought of calling a WCF service which 'discovered' the modules on the server using a Configuration File on the server. The service would return a List<IModule> but obviously this would not work because how would the Silverlight Client serialize IModule. 

So I was wondering whether or not the Client's Isolated Storage could be used, put (very) simply it would do the following: 

  1. The Client's additional configuration (what addons were available) would be obtained using a WCF service, the result being simply a piece of XML
  2. 'Unity for Silverlight' would be altered to search the application installed assemblies directory first, if not found it would check the client's local isolated storage
  3. If not found in either locations it would download the additional assemblies from the server to the client's isolated storage.

Does this sound mad? Has anyone tried this? Does any of it sound unfeasible, i.e. are their any security issues in using the isolated storage?

Any comments / suggestions would be appreciated

Thanks
Richie

Aug 21, 2008 at 8:24 AM

I've got something to work for my situation:

  1. Use a WCF Service to download the configuration for the composite application, i.e. determine what addins are available. The configuration will have the http:// location of the assembly and the name of the class (module) that implememts IModule
  2. Use a WebClient to download the assembly.
  3. Use AssemblyPart.Load(e.Result) to load the assembly
  4. Use Assembly.CreateInstance(...) to create n instance of the class that implements IModule
  5. Execute Initialise(container) on the addin class
  6. The addin is then responsible for its own configuration using the client's container (RegionManagerService etc...)

Having not being party to previous discussions my only concern is I am not using DILight, rather I am using SLUnity should this matter. My reason for using SLUnity is because I need to use property injection rather than constructor injection, that's because the constructor has already been called as part of the CreateInstance.

Based on this solution I can allow addins to be added to my composite application dynamically and by using the client's container they can subscibe to the pre-registered instances of the client's services.

This doesn't solve 'how do I configure the container for the client's implementation?' - for that I still think Unity.Configuration would help. 

What do people think? Any comments / concerns, should I be using SLUnity over DILight? 

Aug 21, 2008 at 10:14 AM

Hi,

First of all, I'd like to say "Sorry" for not replying your last mail that you sent a couple of weeks ago. (WPF is eating the most of my free time lately :( )

If you like to use IModule, I suggest you to give a try the Silverlight-version of CompositeUI. They called it "Spike". AFAIK, they are using XMLModule loader to load the module dynamically. But I'm not sure whether that XMLModule is shipped it or not. If you like to try Spike, you will need to use SLUnity anyway.

For DILight Vs SLUnity, I highly recommend you to use SLUnity. Since we are already having SLUnity, we don't need to use DILight anymore. AFAIK, we don't have any plan to update DILight in future too. 

For SLUnity.Configuration, there is one problem. I want to provide the same feeling of Unity.Configuration while you are using SLUnity.Configuration. But it seems like it's very hard. So, if I'm providing the easiest way then you won't get the same feeling of Unity.Configuration. 

What feature do you want from Unity.Configuration and show me the way (e.g. code sample) that you want to use in your project? 

You just wanna use like that? 

IUnityContainer container = new UnityContainer();
UnityConfigurationSection section
= (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);

OR eventhing that you can use with Unity.Configuration? 

IUnityContainer myContainer = new UnityContainer();
myContainer.Configure<InjectedMembers>()
.ConfigureInjectionFor<MyObject>(
new InjectionConstructor(12, "Hello Unity!"),
new InjectionProperty("MyProperty"),
new InjectionProperty("MyStringProperty", "SomeText"),
new InjectionMethod("InitializeMe", 42.0, 
new ResolvedParameter(typeof(ILogger), "SpecialLogger"))
);


Aug 21, 2008 at 12:20 PM
Edited Aug 21, 2008 at 12:28 PM

Nice to hear from you again Michael.

My example is based on the Silverlight-version of  CompositeUI incorporating the MVVM pattern. As part of the implementation of the IModule.Initialise I am registering my types as follows:

private IUnityContainer container;
private ICustomerViewModel customerVM;
private IFiscalDetailViewModel fiscalDetailVM;

private void RegisterViewsAndServices()
{
 // Setup views
 container.RegisterType<ICustomerView, CustomerView>();
 container.RegisterType<IFiscalDetailView, FiscalDetailView>();

 // Setup view model
 container.RegisterType<ICustomerViewModel, CustomerViewModel>();
 container.RegisterType<IFiscalDetailViewModel, FiscalDetailViewModel>();  

 // Setup model
 container.RegisterType<ICustomerModel, CustomerModel>();
 container.RegisterType<IFiscalDetailModel, FiscalDetailModel>();

 // Resolve the view models
 customerVM = container.Resolve<ICustomerViewModel>(); 

 // the Customer View Model provides the instance of the Trader Context Service
 container.RegisterInstance<ITraderContextService>(customerVM);
 
 fiscalDetailVM = container.Resolve<IFiscalDetailViewModel>();

}

It would be really good if I didn't have to hard code these registrations but rather place them in the unity section of the config file.
In the ViewModel classes being resolved I am currently using Constructor Injection and by decorating properties with the [Dependency] attrribute (thus the reason for using SLUnity over DILight).

Hope this helps
Richie

Aug 28, 2008 at 5:02 PM
Hi,

How would you configure your config file instead of having those hard-codes?

The following should be okay for you??

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
  <section name="unity"
            type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                 Microsoft.Practices.Unity.Configuration, Version=1.1.0.0,
                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<unity>
  <containers>
    <container name="containerOne">
    <types>
        <type type="ICustomerView" mapTo="CustomerView" />
        <type type="IFiscalDetailView" mapTo="FiscalDetailView" />
        <type type="ICustomerViewModel" mapTo="CustomerViewModel" />
        <type type="IFiscalDetailViewModel" mapTo="FiscalDetailViewModel" />
    </types>
   </container>
  </containers>
</unity>
</configuration>



XMLConfigurationManager.LoadUnityConfiguration(new Uri("UnityConfig.xml"), UriKind.Relative));

IUnityContainer container = new UnityContainer();
UnityConfigurationSection section = XMLConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);

If yes, I will try to commit the latest code to SVN this weekend. The code are still under testing.. 

If no, please let me know how you like to configure in config and how you like to call in code.


Aug 29, 2008 at 7:57 AM

Hello,

This is me again. I wanna update the sample that I posted in previous thread. 

The code will be like that. Do you think it's okay for your project? 

 private void Application_Startup(object sender, StartupEventArgs e) {

XMLConfigurationManager.LoadUnityConfigurationAsync(new Uri("http://localhost:1656/SampleWeb/UnityConfigs.xml"));

XMLConfigurationManager.OnConfigLoaded += new XMLConfigurationManager.XmlConfigHandler(XMLConfigurationManager_OnConfigLoaded);
this.RootVisual = new Page();


}

void XMLConfigurationManager_OnConfigLoaded(object sender, EventArgs e) {
UnityConfigurationSection uConfigSection = XMLConfigurationManager.ConfigurationSection;
IUnityContainer myContainer = new UnityContainer();
uConfigSection.Configure(myContainer);
}

Aug 29, 2008 at 10:21 AM
Michael,

That is exactly what I want and I would be very willing to help test any early developments you have.

Cheers
Richie
Aug 29, 2008 at 11:20 AM

Hi Richie,

I have sent the sourcecode of SLUnity.Configuration (+ existing projects) and Sample. Currently, we will need to have a XML file on ASP.NET project and Silverlight project "XMLConfigurationManager" will read that XML and will register or resolve based on XML file. 

I believe that people may not like to put those configurations in XML. Instead, I should probably create WCF service that return those configuration in XML formats.Plus, we need to think about security too. But I think https or ASP.NET form-authentication will help. 

Please feel free to email me or post here if you have any suggestion or comment. 

Nov 27, 2009 at 6:41 AM

Hello Michael,

I chanced upon to see this discussion. Is it possible share the sourcecode for SLUnity (and a sample)? Currently, all my Unity configuration is done in the code and I feel this should move to a configuration file. Thanks!

-Chidu

Apr 27, 2010 at 12:45 PM

Hi all,

just whanted to point out that a new library named Sonic.Net is out in codeplex here that takes care of unity Configuration task in xaml resource file. Works both for silverlight and Wpf and its inspired by the Composite feature for ModuleCatalog configuration.

Best Regards

Constantinos Leftheris