Lecture thumbnail 0:00 / 0:00 We’re not going to take a look at how Autofac helps you build adapters and not just build them, but

actually inject them correctly.

So let me give you an example where let’s suppose we’re building some sort of gooey application and

that gooey application has a toolbar with a bunch of buttons on it, and pressing a button executes

a command.

So we implement this interface called I Command, which is just going to have a single method called

execute.

And this is what’s going to be implemented in two commands that we’re going to make.

So I’m going to make a couple of derived types.

I’ll make a save command, which will simulate saving a file.

So in this case, I’ll just simulate it by using console dot write line.

So I’ll write line saving a file, and then I’ll duplicate this and I’ll make an open command.

So in this case, I will have an open command like so.

And here I will be opening a file.

Okay, so we have two commands implementing this interface.

Now let’s suppose that these commands are invoked by pressing buttons.

So we have this ID of a button and the button is related to the command that it invokes.

So each button has an AI command called command, and we’ll make a constructor which initializes the

command so that we can inject it.

Okay.

In addition, we’ll have some sort of method called click, and that’s when you click the button, you

execute the command.

So you simply say command dot, execute.

There we go.

Okay, so having implemented this, we might try to start using this right in our container builder.

So in the container builder we might start by registering the command.

So I would say register type save command.

And I would try to register it as I command like so I command.

There we go.

And I would just duplicate this and do this for the open command.

And then, of course, what I want to do is I want to build the actual editor.

I want to build like a gooey editor, which actually gets a row of buttons to display.

So I might go ahead and make a class called Editor.

And in here I would make a field which is an enumerable of button, a bunch of buttons that we’re accepting,

and once again initialize it in the constructor.

And in addition we’ll make a diagnostic method for clicking all the buttons.

So we say for each var BTN in buttons and we simply click each of the buttons like so.

Okay, so I want to make this editor and I’ve already registered the commands, so all I have to do

now is of course register the button.

So register type button and register the editor itself, dot register type editor like so.

And then supposedly what I can do is I can say var editor equals C dot resolve, resolve the editor

and then editor dot click all.

So I’m thinking that I got two buttons here because look, I have this injected enumerable of button,

so I’m expecting two buttons.

Right?

Well, wrong.

We’re not getting two buttons.

Nowhere near.

We get just a single button which does opening a file.

So let me explain to you what’s going on here.

We’re only having a single button registration.

If we had some I button interface and we had button and big button and those were eye buttons, then

we would have two registrations.

At the moment we just have one button and that button refers to an eye command which by default becomes

an open command.

And that’s exactly what we get in the screen.

Now what we want is we want the button to act as an adapter for an eye command to kind of invoke the

appropriate eye command.

So the number of buttons that we want in here is two, not one.

So how do we do this?

How do we set up this connection between the button and the eye command?

How do we tell Autofac that it’s an adapter?

Well, we can do it.

And this is done by calling B dot register.

Register adapter.

So what’s happening here is you have to specify the adapted item, which is I command and who is doing

the adapting, which is button.

And then of course we can provide the lambda which actually initializes the button.

So given an I command, which I’ll call CMD, what we need to do is we need to return a new button which

simply initializes that command.

That’s really all there is to it.

And if we execute this now, we’ll get completely different results.

We get both of these outputs.

So what’s happening here?

Well, in actual fact, what’s happening is you’re getting two buttons, you’re getting one button for

each command.

So what happens is you resolve an I command, look at all the results and for each result, make a button

and inject that button.

So in the I enumerable here we just get two elements instead of one because they have been explicitly

associated using this register adapter invocation.

Now it’s also possible to merge this approach with the approach of providing meta information.

So if you want some metadata provided, then you can do this using this mechanism of adapters as well.

And let me show you how this is done.

So let us suppose that we go up into the button and we have some metadata here which is passed, so

we’ll have private string name and I’ll initialize it in the constructor as well.

So I’ll just initialize this field in the constructor and then we’ll have some sort of a method called

Print Me.

Like so and we’ll console dot write line and we’ll say I am a button.

So I’ll say like this.

I am.

A button.

A button called and then the name.

Okay, so we want to specify this name from the configuration.

That’s all that we want to do.

And it is in fact possible.

So it will be a similar register adapter call in here, except not quite like this.

Instead, what we’ll say is we’ll say B dot register adapter and here, instead of specifying a command,

we’ll specify a meta I command.

So meta I command allows you to provide additional information and in addition we’ll have the button

as before.

I need to have a using statement which for some reason I’m not getting automatically, so I’ll just

write it down using autofac dot features dot metadata.

All right, so we’re done with the meta, almost done because we still have to finish off this invocation.

So here we take a lambda as always.

So the lambda that we’re getting is slightly different.

So we take the command.

But remember, command is an meta object now.

So in order to get something from it, you have to use dot value.

And in order to get the metadata, well, you use the metadata.

So we make a new button just as before and we say that, well, here is the button.

It’s initialized with cmd dot value that refers to the actual command.

And if we want metadata, we say cmd dot metadata and specify some metadata like name for example,

and we can cast that to a string like so.

Okay, so this is the invocation for registering the command, being decorated by a button with metadata

included.

And then of course all we have to do is we have to actually supply the metadata somewhere and then actually

invoke the whole thing.

So to supply the metadata, what I can do is I can specify it here.

So for example, here I can specify that the name here for this command will be save and that the metadata

here with metadata that the name is going to be open.

So that’s the metadata.

We specify the actual metadata here.

We specify the handling of the metadata in the register adapter invocation.

And then, of course, we have to actually make use of all of this.

So let’s see how we can get that done.

So instead of clicking all the elements, what I’m going to do is I’m going to say for each var BTN

in editor dot buttons and remember buttons in is not available yet.

So let’s jump into the editor.

Let’s actually expose the buttons.

So let’s see if we can do it the smart way by generating a property of some kind on this.

And it’s going to be read only let’s just do it like this.

And of course, the modern C-sharp syntax will be even nicer.

So let’s make an expression body.

And then for each of the buttons, what I’m going to do is I’m going to say BTN, print me, print me

like so, and let’s see what this actually comes up with.

All right.

So as you can see, we’re passing the metadata and we have a button called save and a button called

Open where Save and Open are the names which were being passed in from the outside.

Play Play Play Play Stop Start Play