Registering Components Flashcards
What is the rule for choosing constructors?
Autofac automatically uses the constructor for your class with the most parameters that are able to be obtained from the container.
If you have:
var builder = new ContainerBuilder(); builder.RegisterType();
What can you say about MyComponent?
It must be a concrete type.
Can you register an abstract/ interface component?
No. There must be an associated concrete class - even if it is created on the fly through a lambda.
You can manually choose a particular constructor to use and override the automatic choice by registering your component with …?
With the UsingConstructor method and a list of types representing the parameter types in the constructor:
Example:
builder.RegisterType()
.UsingConstructor(typeof(ILogger), typeof(IConfigReader));
When can you pass parameters?
At registration time or resolve time.
Why can’t you use RegisterType for abstract / interface components?
You can’t “new up” an abstract or interface component.
What is the statement for RegisterType
builder.RegisterType()
.UsingConstructor(typeof(ILogger), typeof(IConfigReader));
How do you pre-generate an instance of an object and add it to the container for use by registered components?
var output = new StringWriter(); builder.**RegisterInstance**(output).As
How can you remove a static singleton by placing it in a container?
builder.RegisterInstance(MySingleton.Instance).ExternallyOwned();
Show how to use a lambda expression to Register an instance of a concrete class resolved from an interface.
builder.Register(c => new ClassB(c.Resolve()));
“c” is the component context in which the component is created and is an IComponentContext object.
Use lambda express to create complex parameter of DateTime.Now.AddMinutes(5) for UserSession.
builder.Register(c => new UserSession(DateTime.Now.AddMinutes(25)));
What are the three types of parameter injection?
NAMED: .WithParameter(“configSectionName”, “sectionName”);
TYPED: .WithParameter(new TypedParameter(typeof(string), “sectionName”));
RESOLVED: .WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(string)
&& pi.Name == “configSectionName”,
(pi, ctx) => “sectionName”));
Define the three types of parameter injection.
NamedParameter: match target parameters by name
TypedParameter: match target parameters by type (exact type match required)
ResolvedParameter: flexible parameter matching
Why are resolved parameters different from named and type parameters?
Resolved parameters can be used as a way to supply values dynamically retrieved from the container, e.g. by resolving a service by name, whereas Named and Typed parameters can only supply constant values.
How would you register an instance of:
public class ConfigReader : IConfigReader { public ConfigReader(string configSectionName) {
// Store config section name
} // ...read configuration based on the section name. }
builder.Register(c => new ConfigReader(“sectionName”)).As();
With lambda expression components, do you pass parameter values at registration time or at service resolution time? Example?
// Use TWO parameters to the registration delegate: // c = The current IComponentContext to dynamically resolve dependencies // p = An IEnumerable with the incoming parameter set builder.Register((c, p) =\> new ConfigReader(p.Named("configSectionName"))) .As();
RESOLVE:
var reader = scope.Resolve(new NamedParameter(“configSectionName”, “sectionName”));
What is Property Injection?
Property injection uses writeable properties rather than constructor parameters to perform injection.
What is Method Injection?
Method injection sets dependencies by calling a method.
How to call use Method Injection by using a lambda expression component with a method call in the activator?
builder.Register(c => {
var result = new MyObjectType(); var dep = c.Resolve(); result.SetTheDependency(dep);
return result;
});
How to call use Method Injection by adding an activating event handler?
builder .Register() .OnActivating(e =\> { var dep = e.Context.Resolve(); e.Instance.SetTheDependency(dep); });