Object creation and WCF

Mar 27, 2008 at 11:57 AM
Hi,
I was building a simple ObjectBuilder project in which object instances were created either by loading a type from a local assembly or from a wcf proxy by using the ChannelFactory<T> method. And then I discovered IoC containers: Windsor, Spring.Net, StructureMap, ... As always, I re-invented the wheel! ;-)
My question is: in Unity, can I create a new object which is a wcf proxy returned by the ChannelFactory<T> method? This way, my application as usual does not know how the object is created, but also where is created!

Thanks, you're great! ;-)
David
Mar 27, 2008 at 3:17 PM
You should have a look at StaticFactoryExtension.

It is an easy way to provide alternative creation strategies for a given type.

container.Configure<StaticFactoryExtension>().RegisterFactory<IWCFService>(container => channelFactory.CreateChannel(endPoint));
Mar 27, 2008 at 3:28 PM
Edited Mar 27, 2008 at 3:30 PM

My initial thoughts would create a wrapper class around your proxy. IBeerService below is your service contract. I just created a dummy interface so I could validate/supply complete example. Does anyone else have another solution? (PS Just saw francois_tanguay's response, that looks cool.)

using System.ServiceModel;
using Microsoft.Practices.Unity;
 
namespace Sample
{
    public interface IBeerService
    {
        /// <summary>
        /// Orders the specified quantity of beer.
        /// </summary>
        /// <param name="quantity">The quantity.</param>
        /// <returns>The price of the beer.</returns>
        double Order(int quantity);
 
        /// <summary>
        /// Drinks the specified quantity of beer.
        /// </summary>
        /// <param name="quantity">The quantity.</param>
        /// <returns><c>true</c> if can drink more, otherwise <c>false</c>.</returns>
        bool Drink(int quantity);
    }
 
    public class WcfBeerService : IBeerService
    {
        private IBeerService proxy;
 
        [InjectionConstructor]
        public WcfBeerService()
        {
            ChannelFactory<IBeerService> factory = new ChannelFactory<IBeerService>("IBeerService_Endpoint");
            proxy = factory.CreateChannel();
        }
 
        #region IBeerService Members
        public double Order(int quantity) { return proxy.Order(quantity); }
        public bool Drink(int quantity) { return proxy.Drink(quantity); }
        #endregion
    }
}



My question is: in Unity, can I create a new object which is a wcf proxy returned by the ChannelFactory<T> method? This way, my application as usual does not know how the object is created, but also where is created!

Mar 27, 2008 at 3:59 PM
If you are interested in building more stable extension of unity with proper strategy and supporting policy which would return wcf proxy, you may want to take a look of how WCF can be integrated to PIAB in following articles below:

Tom Hollander discusses about “Invoking the Policy Injection Application Block at WCF Service Boundaries” he highlights the areas of policy that must be created.

http://blogs.msdn.com/tomholl/archive/2007/12/02/invoking-the-policy-injection-application-block-at-wcf-service-boundaries.aspx

This same topic is furthermore discussed on MSDN magazine article: PIAB and WCF -Integrating the Policy Injection Application Block with WCF Services.

http://msdn2.microsoft.com/en-us/magazine/cc136759.aspx

Regards,
Alexander
Mar 27, 2008 at 5:03 PM
Well, thanks a lot to everyone. I'll read the material suggested and, well, of course the final answer is that Unity can do this, so I'll use it! ;-)
By the way, suppose you have 20 interfaces in an assembly and 20 classes in another assembly that implements those 20 interfaces. Suppose also that the naming convention is "smart": all the interfaces are named IMyEntityNameDataLayer and all the classes are named MyEntityDataLayer. In Unity I think I'll have to make a configuration file, list every interface and for each one define the class that implements it; is there a way to define a mapping rule, for example using a regular expression? In this way, I could say something like this: "For every interface in assembly X, load the corresponding type in assembly Y"; of course "corresponding" means some kind of regular expression replacement...

Thanks again for support.
David
Mar 27, 2008 at 5:13 PM
Just do exactly what you're saying!

Something like:
foreach (Type type in assembly.GetTypes())
{
  foreach (Type interfaceType in type.GetInterfaces())
  {
    container.RegisterType(interfaceType, type);
  }
}
Mar 28, 2008 at 10:54 PM
I thought my solution was a bit naive. Thanks for the feedback.


alexanderQX wrote:
If you are interested in building more stable extension of unity with proper strategy and supporting policy which would return wcf proxy, you may want to take a look of how WCF can be integrated to PIAB in following articles below:

Tom Hollander discusses about “Invoking the Policy Injection Application Block at WCF Service Boundaries” he highlights the areas of policy that must be created.

http://blogs.msdn.com/tomholl/archive/2007/12/02/invoking-the-policy-injection-application-block-at-wcf-service-boundaries.aspx

This same topic is furthermore discussed on MSDN magazine article: PIAB and WCF -Integrating the Policy Injection Application Block with WCF Services.

http://msdn2.microsoft.com/en-us/magazine/cc136759.aspx

Regards,
Alexander