Registering Components Flashcards

1
Q

What is the rule for choosing constructors?

A

Autofac automatically uses the constructor for your class with the most parameters that are able to be obtained from the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If you have:

var builder = new ContainerBuilder(); builder.RegisterType();

What can you say about MyComponent?

A

It must be a concrete type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you register an abstract/ interface component?

A

No. There must be an associated concrete class - even if it is created on the fly through a lambda.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

You can manually choose a particular constructor to use and override the automatic choice by registering your component with …?

A

With the UsingConstructor method and a list of types representing the parameter types in the constructor:

Example:

builder.RegisterType()

.UsingConstructor(typeof(ILogger), typeof(IConfigReader));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When can you pass parameters?

A

At registration time or resolve time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why can’t you use RegisterType for abstract / interface components?

A

You can’t “new up” an abstract or interface component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the statement for RegisterType

A

builder.RegisterType()
.UsingConstructor(typeof(ILogger), typeof(IConfigReader));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you pre-generate an instance of an object and add it to the container for use by registered components?

A
var output = new StringWriter();
builder.**RegisterInstance**(output).As
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you remove a static singleton by placing it in a container?

A

builder.RegisterInstance(MySingleton.Instance).ExternallyOwned();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Show how to use a lambda expression to Register an instance of a concrete class resolved from an interface.

A

builder.Register(c => new ClassB(c.Resolve()));

“c” is the component context in which the component is created and is an IComponentContext object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Use lambda express to create complex parameter of DateTime.Now.AddMinutes(5) for UserSession.

A

builder.Register(c => new UserSession(DateTime.Now.AddMinutes(25)));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the three types of parameter injection?

A

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”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define the three types of parameter injection.

A

NamedParameter: match target parameters by name

TypedParameter: match target parameters by type (exact type match required)

ResolvedParameter: flexible parameter matching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why are resolved parameters different from named and type parameters?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.
}
A

builder.Register(c => new ConfigReader(“sectionName”)).As();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

With lambda expression components, do you pass parameter values at registration time or at service resolution time? Example?

A
// 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”));

17
Q

What is Property Injection?

A

Property injection uses writeable properties rather than constructor parameters to perform injection.

18
Q

What is Method Injection?

A

Method injection sets dependencies by calling a method.

19
Q

How to call use Method Injection by using a lambda expression component with a method call in the activator?

A

builder.Register(c => {

var result = new MyObjectType();
var dep = c.Resolve();
result.SetTheDependency(dep);

return result;

});

20
Q

How to call use Method Injection by adding an activating event handler?

A
builder .Register() .OnActivating(e =\> { var dep = e.Context.Resolve(); e.Instance.SetTheDependency(dep); });