1

If I have something like this :

        var container = new Container();
        container.RegisterInstance("a", serviceKey: "a");
        container.RegisterInstance("b", serviceKey: "b");

How do I register a list of string which will contain "a" and "b"?

I also tried resorting to params but with no success.

    class T
    {
        public string[] x;

        public T(string s, params string[] strs)
        {
            x = (new[] {s}).Union(strs).ToArray();
        }
    }

    static void Main(string[] args)
    {
        var container = new Container();
        container.RegisterInstance("a", serviceKey: "a");
        container.RegisterInstance("b", serviceKey: "b");
        container.Register<T>(made: Made.Of(() => new T(Arg.Of<string>("a"))));
        container.Register<T>(made: Made.Of(() => new T(Arg.Of<string>("a"), Arg.Of<string>("b"))));

EDIT:

I managed to have this running, but it might ,not be a good workarround:

        container.RegisterInstance(typeof(IEntityManager), (IEntityManager)ctx.GetObject("entityManager_candidate"), serviceKey: "candidate");
        container.RegisterInstance("XYZ_CANDIDATE", serviceKey: "candidate");
        container.Register<IDalCandidate>(serviceKey: "candidate",
            made: Made.Of(() => new DalCandidate(Arg.Of<IEntityManager>("candidate"), Arg.Of<string>("candidate"))));
        container.RegisterInstance(typeof(IEntityManager), (IEntityManager)ctx.GetObject("entityManager_resource"), serviceKey: "resource");
        container.RegisterInstance("XYZ_RESOURCE", serviceKey: "resource");
        container.Register<IDalCandidate>(serviceKey: "resource",
            made: Made.Of(() => new DalCandidate(Arg.Of<IEntityManager>("resource"), Arg.Of<string>("resource"))));
        //container.RegisterMapping<IDalCandidate, DalCandidate>()
        var lst = new List<IDalCandidate>
        {
            container.Resolve<IDalCandidate>("resource"),
            container.Resolve<IDalCandidate>("candidate")
        };
        container.RegisterInstance(typeof(IList<IDalCandidate>), lst);
        container.Register<ISearchWeightedCandidateManager, SearchWeightedCandidateManager>();
Serge Intern
  • 2,669
  • 3
  • 22
  • 39
  • I am not sure what you want to achieve. You can inject string values with string keys as KeyValuePair[], then you can use normal Linq. If you need all types of service keys, then use KeyValuePair[]. More info here https://bitbucket.org/dadhi/dryioc/wiki/RegisterResolve#markdown-header-resolving-as-keyvaluepair-wrapper – dadhi Mar 24 '16 at 17:07
  • I need a list of to be inject into some constructors (SearchWeightedCandidateManager requires a IList of IDalCandidate). – Serge Intern Mar 25 '16 at 08:27
  • Then inject a list, what is the problem? DryIoc supports IList wrapper. – dadhi Mar 25 '16 at 10:21
  • I don't know how to do that without actually instantiating the list. How to "tell" DryIoc "When I need ObjectA, instantiate it with a List containing a new ObjectB and a new ObjectC? So that everything gets instantiated when ObjectA gets created." – Serge Intern Mar 25 '16 at 11:05
  • It might be confusing... Can you just show me how you would handle A(IList lst), B(string s); when your goal is to have a singleton A containing a List of two B (constructed with different strings). – Serge Intern Mar 25 '16 at 11:07

1 Answers1

1

Update: better answer

Looking at your sample again.. Basically you have some kind of multi-tenancy, and then injecting all the tenants in some consumer.

c.Register<Dal>(serviceKey: "x");
c.Register<Dal>(serviceKey: "y");

// using delegate for brevity, better convert to method
Func<object, Func<RequestInfo, bool>> getCondition = 
     key => r => r.Parent.Enumerate().Any(p => p.ServiceKey == key);
var inX = getCondition("x");
var inY = getCondition("y");

c.RegisterInstance("a", condition: inX);
c.RegisterInstance("b", condition: inY);

// register the rest of dependencies in X or in Y

c.Register<Manager>(); // normally injects Dal[]

That's demonstrate the idea. Hope it will be more relevant for your case than my first answer.

dadhi
  • 4,807
  • 19
  • 25
  • What happen if I store different string with servicekeys for different usage before this code gets called? I don't understand why you feel lists are a design smell. – Serge Intern Mar 25 '16 at 15:15
  • Then you might use different approach for getting strings required for B. Or you may use specific service key format or type to distinguish strings required for different services. I am Ok with the lists, but your setup relies on conventions which may be configured wrong, producing hard to find bugs. – dadhi Mar 25 '16 at 17:49