1

I am using Unity to resolve a repository that requires two factory classes in the parameter. Below is my repository constructor:

//repo constructor
public MyRepo(IChannelClientFactory<IRemotedVisitorChat> chatFactory, IChannelClientFactory<IRemotedVisitorWebsite> siteFactory  )
        {
            ChatFactory = chatFactory;
            SiteFactory = siteFactory;
        }

ChatFactory and SiteFactory are of type class RemotingClientFactory<T> : IChannelClientFactory<T> that has a contructor of:

RemotingClientFactory(string endpointName)

before, RemotingClientFactory had a parameterless constructor, but in doing some refactoring, I added the string endpointName.

I was registering my repository like this:

container.RegisterType<IMyRepo, MyRepo>(new HierarchicalLifetimeManager(), new InjectionConstructor(typeof (RemotingClientFactory)));

originally, there was only one parameter for MyRepo constructor and RemotingClientFactory didn't take any arguments for its constructor

I am getting lost in the weeds reading the Unity docs. How can I provide RemotingClientFactory with an initialization string?

3
  • Have you tried with semi manual InjectionFactory approach? Commented Sep 21, 2014 at 16:00
  • I was going to use that if I can find an answer to what I was trying to do Commented Sep 21, 2014 at 16:04
  • Are ChatFactory and SiteFactory each classes derived from RemotingClientFactory<IRemotedVisitorChat> and RemotingClientFactory<IRemotedVisitorWebsite> respectively? Commented Sep 21, 2014 at 16:07

1 Answer 1

1

First you need to register your IChannelClientFactory<T> implementations, specifying the endpointName value to use for each. (The string values below should be replaced with the endpointName values you wish to use)

container.RegisterType<IChannelClientFactory<IRemotedVisitorChat>, RemotingClientFactory<IRemotedVisitorChat>>(new InjectionConstructor("ChatEndpointName"));
container.RegisterType<IChannelClientFactory<IRemotedVisitorWebsite>, RemotingClientFactory<IRemotedVisitorWebsite>>(new InjectionConstructor("WebsiteEndpointName"));

Then you can just register your IMyRepo implementation, the longest constructor of MyRepo will be selected by unity and the values be injected upon resolve.

container.RegisterType<IMyRepo, MyRepo>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.