dotnetjunkie / blog

Files for my weblog
0 stars 1 forks source link

Comments for "The Closure Composition Model" #2

Open dotnetjunkie opened 5 years ago

dotnetjunkie commented 5 years ago

In the section below, you can leave your comments for this blog post: https://blogs.cuttingedge.it/steven/posts/2019/closure-composition-model/.

Please be aware that a comment posted here will be shown in the comment section of the blog post, using JavaScript. This means that you should consider this a comment directly on my weblog—not as a post on GitHub.

This means that I might

didiercauvin commented 4 years ago

Hi Steven and thanks for this article. I have a problem understanding this :

using (ILifetimeScope scope = container.BeginLifetimeScope())
{
    var userContext = scope.Resolve<ClosureUserContext>();
    userContext.UserName = queueContext.UserName;

    // Let Autofac compose the object graph which consists of ClosureUserContext
    var handler = scope.Resolve<IHandler<OrderCancelled>>();

    // Invoking the constructed and initialized graph
    handler.Handle(queueContext.Message);
}

I don't understand where you implement it. I guess it is part of the composition root, but as you call the Handle method, is it part of some kind of proxy?

Regards, Didier

dotnetjunkie commented 4 years ago

Hi Didier, The Handle method is part of the IHandler<T> interface. I didn't show that interface, because it is not central to the discussion, and you can replace it with any other interface. But to paint the picture, this is how the IHandler<T> interface might look like:

public interface IHandler<T>
{
    void Handle(T message);
}

For more information on this kind of design, read this old article of mine.

didiercauvin commented 4 years ago

Thanks Steven for taking the time to answer. In fact, I was talking about the whole using (ILifetimeScope scope = container.BeginLifetimeScope()) block instead of the handler method. I thought it had to be implemented in a proxy but because of the scope.Resolve calls, I don't understand in which class the ILifetimeScope block should be implemented. In the composition root certainly but where exactly?

dotnetjunkie commented 4 years ago

I don't understand in which class the ILifetimeScope block should be implemented. In the composition root certainly but where exactly?

The example in question shows a message that is pulled in from some sort of message queue. So in this case, it is a part of the composition root that is strongly coupled to the messaging infrastructure. It could be, for instance, hooked to some sort of callback mechanism provided by the messaging infrastructure.

Also note that this code example is not realistic, per se, because it requests a handler for a specific message OrderCancelled, while in reality you likely want to make this code more generic, reusable. However, to make the code less abstract, and more understandable to the context of this post, I decided to use concrete types.