WCF RIA Services – StructureMap – DomainServiceFactory

Note: The example in this blog post uses the WCF RIA Services PDC Beta, so things can be changed to the final version.

In a preview post I wrote about how to create a DomainServiceFactory where Microsoft Patterns & Practices Unity Dependency Injection framework is used to create a DomainService, to make sure it will inject all it dependencies. This post will be about using StructureMap instead. Before you read this post, I would like you to read my preview post to understand how to create a DomainServiceFactory, because this post will only show you the code to make sure StructureMap can be used instead of Unity.

The first thing I did was to add a ContainerBootstrapper, where I register all my types. In this case a ICustomerRepository and CustomerService:

public static class ContainerBootstrapper
{
    public static void BootstrapStructureMap()
    {
        ObjectFactory.Initialize(x =>
         {
           x.ForRequestedType<ICustomerRepository>().TheDefaultIsConcreteType<CustomerRepository>();
         });
    }
}


Note: I don’t need to add the Concrete type of CustomerService, StructureMap can by default create an instance and if there are any dependencies register, it will inject them, that is why only the ICustomerRepository is registered.

Here is the skeleton of the CustomerService:

[EnableClientAccess()]
public class CustomerService : DomainService
{
    public CustomerService(ICustomerRepository customerRepository)

    public CustomerPM GetCustomerByID(int customerID)

    public void UpdateCustomer(CustomerPM customer)
}

And here is the ICustomerRepository interface:

public interface ICustomerRepository
{
    Customer GetCustomerByID(int customerID);

    void Update(Customer customer);
}

 

The ContainerBootstrapper’s BootstrapStructureMap method should be called when the application is startup or initialized, I decided to use the Application_Start in Global.asax in this example.

protected void Application_Start(object sender, EventArgs e)
{
   DomainService.Factory = new StructureMapDomainServiceFactory();
   
   ContainerBootstrapper.BootstrapStructureMap();
}


The DomainService.Factory is set to a new instance of a StructureMapDomainServiceFactory, the StructureMapDomainServiceFactory will be responsible to use StructureMap’s factory to create a DomainService. Here is the implementation of the StructureMapDomaiNServiceFactory:

public class StructureMapDomainServiceFactory : IDomainServiceFactory
{
     public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
     {
         var service = ObjectFactory.GetInstance(domainServiceType) as DomainService;
          service.Initialize(context);

          return service;
      }

      public void ReleaseDomainService(DomainService domainService)
      {
         domainService.Dispose();
      }
}


By using the ObjectFactory’s GetInstance, we can make sure StrucuterMap creates an instance of the specified type and then inject all dependencies.

Note: There is not exception handling added to the Factory, the reason is that the argument domainServuceType is already a DomainService type, if not the WCF RIA Services wouldn’t work. The GetInstance will throw an exception if the domainServiceType couldn’t be created, but I just let the exception bubble up. It’s up to you to add the exception handling you want to use.

If you want to know when I publish new blog posts, you can follow me on twitter: http://www.twitter.com/fredrikn

No Comments