Creating Instances of Objects without values, and using build-up.

Nov 30, 2008 at 4:51 PM
I was wondering if this is possible or will be in the future.

I'm using Unity from a web service (WCF).  I want to be able to create an instance of a class from the unity config file.   Unfortunately I don't really need to set a value when I create an Instance, which seems to be a requirement, since the Value attribute is required.  I've done this, just dummying up a typeconverter.   The problem I run into is that I want to be able to have that newly created class, have it's dependencies resolved.  This doesn't appear to happen, and I'm not sure if I can make this happen.

The scenario is that I have a business class that the web service is accessing, which performs some actions, that I was going to also expose event notifications for other classes that needed to know the events took place.   Example.   When a record is processed, I want to be able to have an event be raised from the business object, that a separate class could monitor and do actions on (Hopefully not coupled to the business object).   I wanted the Listener class that I instantiated from the Instances section to be able to retrieve a reference to the business object and a email managment object, so that it could generate the emails.  From that class, I currently don't have access to the Unity container, nor does it seem to resolve the references.

1.  Create class from Instances Section.  
        It shouldn't always require a value.
        It should have any Interfaces resolved.
        It should have access to the Unity Container.

Is there a way to do this either through an instance or another way?  I didn't want to use interception to do this.

Web Service -> Business Object -> DAO     also    NotificationListener -> EmailManagement (to send emails)
                                                                                                              -> Business Object (to register for events)

NotificationListener registered from unity config obtains a reference to the business object to register for events.  It would contain an inteface (reference) also  for the emailManagement object so it can send email alerts based on events raised by the business object.

Any suggestions welcome, or recommendations for other approaches.

Thanks in advance.

Nov 30, 2008 at 7:30 PM
Is there any reason you cannot have the ServiceHost build-up your service implementation?

Integrating Unity with WCF
Unity, WCF and IIS

On our project, we combined some of the work done on the Prism, err Composite Application Guidance for WPF project to create a UnityServiceHost that has a constructor UnityServiceHost(UnityBootstrapper bootstrapper, Type serviceType, Uri[] baseAddresses).

The Common Service Locator also gives a good way to provide access to your container. 

Nov 30, 2008 at 8:32 PM
Thanks for such a fast response.

I've actually got the dependency injection part working for the Web Service.  It's injecting the business layer's and dao's.   I had already found the links above which were really helpful.

I was hoping to create a class that is not referenced from those layers.  I was hoping to be able to create it from the web config, by defining an instance (Instances Section).   This class was the one that needed to have the interfaces resolved to a class in the business logic, and then also a new one for email handling.   I don't want to have reference this class via code in any form or fashion.   It should be capable of getting the business class from the container, but it doesn't have access to the container, nor does it seem that the classes created in the <Instances> section have any dependency injection performed.

This would be a good place for the Common Service Locator, since the new class has no access to the container nor are the dependencies resolved.   (Sad, I wish they were, I hope that I just haven't figured it out yet :).

I'll take a look at the Service Locator to see how to wire it in and to see if it will resolve my delima...    Is there any special work that needs done to add the Service Locator for use?

Dec 1, 2008 at 12:54 AM
I think what you may be looking for is something similar to Composite Application Guidance for WPF's EventAggregator. From the help file: "The EventAggregator service is primarily a container for events that allow decoupling of publishers and subscribers so they can evolve independently. This decoupling is useful in modularized applications because new modules can be added that respond to events defined by the shell or, more likely, other modules.".  I would down the help guidance help file and take a read.

Please note that I do not have any experience with the Composite Application Guidance. I have only dug through their released code to harvest stuff for my own projects.  I have only really used the Modularity and Bootstrapper portions of their library.

ServiceLocator helps solve two problems, 1) how to create code that needs access to ServiceLocator or the container, but shouldn't know how the container created. 2) To create code that is DI aware, but container agnostic.

For ServiceLocator, the basics in Composite Application Guidance is something like this:

Container = new UnityContainer();    
Container.RegisterType(typeof(IServiceLocator), typeof(Microsoft.Practices.Composite.UnityExtensions.UnityServiceLocatorAdapter), new ContainerControlledLifetimeManager());
ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());

Then later on you can use:

IServiceLocator serviceLocator = ServiceLocator.Current;

// get instance of some registred type/interface.
IUnityContainer container = serviceLocator.GetInstance<IUnityContainer>();

Hope this helps a bit. I know it doesn't solve your problem entirely but should help.


Dec 1, 2008 at 1:56 AM
Thanks, You Rock!    I had just figured out most of it, though I didn't register the container or the adapter with the container.   I'll add the extra pieces above.  It works great so far and this is a great addition.

Thanks  for being so responsive, I'm sure everyone appreciates it.

Jan 3, 2009 at 2:40 PM
Edited Jan 3, 2009 at 4:39 PM
Another blog w/webcast (and source) for integrating Unity into a WCF service that will be consumed by a Prism (Silverlight) application => HERE