Lecture thumbnail 0:00 / 6:11 In addition to supporting the adapter pattern, the Autofac container also supports the decorator pattern.
So we’re going to see how exactly this can be used.
So let’s suppose we have an interface called I reporting service and there is just a single method called
report that we need to implement and then we’ll have a concrete implementation.
So we’ll have a class called Reporting Service which will of course implement this reporting service
interface I reporting service like.
So we’ll implement the report method here and I will just console dot write line.
Here is your report.
Now what I want to have is I want to have another component which is able to provide a reporting service
which does logging as well.
So when you start building a report, it tells you that you’re commencing the log.
And when you start building the report, you are stopping the log.
And I’m going to do it in a typical decorator fashion.
So I’ll have another class and this class is going to be reporting service with logging.
Okay?
So it’s also going to implement the I reporting service interface and it’s going to also be a decorator
around an I reporting service.
So here is the thing that we are being which is being decorated.
So this is the component being decorated and I’ll inject it in the constructor as always.
And then what we’re going to do is we’re going to build the report by, first of all, right.
Lining the fact that we are commencing the log and we’ll terminate it by saying that we are finishing
the log or ending the log.
And in between we’ll use the decorated object to actually do the report.
So this is a typical decorator, nothing particularly fancy about it.
We have a reporting service and then we have a decorator which reuses some other service and decorates
it with reporting functionality.
So the only question is can we get autofac to play along and to provide us the appropriate objects when
we actually resolve it in the container.
So here’s the container builder and you container builder as before.
And then of course the question is, well, what do we register and how do we register it?
Because we do need to register the reporting service obviously.
So we say register type reporting service that should be a capital R and we register it as an I reporting
service like so.
Okay.
So far, so good.
But now we need we have a bit of a problem, actually, because if you have a reporting service, which
is an eye reporting service, then during the creation of a reporting service with logging, for example,
you might inject an eye reporting service again.
So just to kind of illustrate if this was reporting service with logging, things will go really badly
because reporting service with logging will inject itself into reported service with logging, which
will inject itself again into a reporting service with logging as an eye reporting service to infinity.
Now what do we want?
What do we want to actually work?
What we want is we want a reporting service with logging, but we want it to inject a reporting service.
On the other hand, we do want the eye reporting service to yield us a reporting service with logging
and not an ordinary reporting service.
So this is getting really confusing right now.
And let me show you how this entire setup can be done using Autofac.
So here we’re going to register the type called reporting service as an eye reporting service, and
we’ll give it a name.
Let’s call it reporting.
Okay.
So the reason why we’re doing this, of course, is that we want a named service.
So instead of as we need to have named here.
So now that we have this named service, we can invoke something else.
We can invoke register decorator.
And this is exactly what we’re currently doing.
So we’re currently building a decorator, the reporting service with logging.
And the way we do it is we first of all, specify the service we’re going to provide, which is reporting
service.
But then when we have the arguments, we provide a lambda here.
So we need to provide a lambda which has the component context as well as the service.
And the idea here is we’re making a new reporting service with logging and we’re feeding it the service.
Feeding it in the service like so.
And here is the key thing.
So the key thing is the second argument, which is called from key.
So this key specifies what exactly we’re going to be using when we resolve the dependencies of the reporting
service by feeding the service in here.
So here are the key would be reporting and it’s the key which corresponds to the definition that we
have above obviously with a small R.
So this is how you register a decorator.
And of course, now that you’ve registered the decorator, what you can do is you can do using var,
C equals B dot build.
And then of course we can say var R equals C resolve.
We can resolve an AI reporting service and we can do the report like so and let’s see what we actually
get.
So as you can see, we have commencing log then here is your report, then ending log.
So interestingly enough, we are resolving the reporting service with logging, but when we construct
the reporting service with logging, the thing that gets injected here is not another reporting service
with logging, but it is in actual fact an ordinary reporting service.
So this is how you effectively leverage the decorator pattern using the autofac container.
Play Play Play Play Play Play Play Play Stop Start Play information alert