0

I want to do something like this Autofac line below with DryIoC

builder.RegisterType<TenantDBContext>
().InstancePerLifetimeScope().WithParameter(new 
NamedParameter("connectionString", ""));

I have a CoreDBContext that has the connection string of TenantDBContext. Is it valid to pass the connection string at the point of registering my context in DryIoc?

Harsh Wardhan
  • 2,110
  • 10
  • 36
  • 51

1 Answers1

2
container.Register<DbContext>(
    Reuse.InCurrentScope,
    made: Parameters.Of.Name("connectionString", _ => ""));

Update:

Asuming that you want to automatically select from multiple constructors:

Here is live snippet

container.Register<DbContext>(
    Reuse.InCurrentScope,
    made: Made.Of(FactoryMethod.ConstructorWithResolvableArguments,
        Parameters.Of.Name("connectionString", _ => "")));

Note, that version above is brittle to parameter name change, so you may consider strongly-typed constructor expression:

container.Register<DbContext>(
    Made.Of(() => new DbContext("")),
    reuse: Reuse.InCurrentScope);
dadhi
  • 4,807
  • 19
  • 25
  • Thank you @dadhi.I replaced with your code in my IocDependencyResolver class but i got this error "Unspecified how to select single constructor from type DryIocSample.Data.TenantDBContext with multiple public constructors: Void .ctor(); Void .ctor(System.String)" . Could it be because i didn't resolve the parameter name in my repository like i did with Autofac with code below get { return _container.Resolve(new NamedParameter("connectionString", _tenantConfig.Configuration.Connectionstring)); } Thanks for your help. – Kemi Awosile May 22 '17 at 09:55
  • arrrgggggggggg!!! still not working. Looks like i'm missing something, shouldn't i resolve this in my repository too ? – Kemi Awosile May 23 '17 at 04:52
  • Hi, I fixed a typo and provided the link to full sample. – dadhi May 23 '17 at 06:08