2

I want register type, than resolve type, and then register instance using resolved values. Something like this:

//Register type:
builder.RegisterType<ValidateImportMandatoryColumns>().Named<IValidateImport>("MandatoryColumn").As<IValidateImport>();
builder.RegisterType<ValidateImportNonMandatoryColumns>().Named<IValidateImport>("NonMandatoryColumns").As<IValidateImport>(); 

//Resolve
var t1 = Container.ResolveNamed<IValidateImport>("MandatoryColumn");
var t2 = Container.ResolveNamed<IValidateImport>("NonMandatoryColumns");

//Create list with resolved values:
List<IValidateImport> allValidators = new List<IValidateImport>(){t1,t2};

//Register Instance:
builder.RegisterInstance(allValidators).As<List<IValidateImport>>();

This is not working. I can't resolve and than register again. Do you know how to do this with Autofac? Maybe is approach wrong, so please tell me if you have better idea. Goal is to inject list of validators with different types that use same interface.

1
  • Based on the content of your question, and your stated goal, I've updated the title. Commented May 15, 2015 at 13:50

1 Answer 1

3

Autofac has built in support for collection. If you want to resolve all IValidateImport, you can resolve IEnumerable<IValidateImport>

var allValidators = container.Resolve<IEnumerable<IValidateImport>>(); 

See Implicit Relationship Types for more information for more information.


By the way, if you want to update a container, which is not required here, you can use the following piece of code.

var builder = new ContainerBuilder(); 
// do some registration 

var container = builder.Build();


var updater = new ContainerBuilder();
// do other registraitons

// update the container 
updater.Update(container);
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.