Search code examples
dependency-injectionstructuremap.net-remoting

Dependecy Injection When Using DotNet Remoting


We are using DotNet Remoting for our application server and also using StructureMap. How do go about setting up proper dependency injection inside the remoting objects so that my code is no longer littered with dependency lookup code like this?

PolicyEntity policy = ObjectFactory.GetInstance<IPolicyDataAccessor> ().FindByPolicyId (policyId);

To be clear, I want to be able to declare a property on my remoting object and have StructureMap inject into it. Then I can just write.

PolicyEntity policy = PolicyDataAccessor.FindByPolicyId (policyId);

Any help will be appreciated.


Solution

  • Since it's not very convenient to create the remoted object from the ObjectFactory, the easiest way is to let the remoting server construct the object as normal and, inside the object constructor which it will call, let the remoted object inject itself with its dependencies using;

    ObjectFactory.BuildUp(this);
    

    That will inject all dependencies, just as if the object was created from the ObjectFactory to begin with.