1

I am using Autofac and I am not a IoC master.

Consider this scenario:

public interface IBeforeRequestTask
{
    void Execute();
}

public interface IAfterRequestTask
{
    void Execute();
}

public class TaskExecutor : IBeforeRequestTask, IAfterRequestTask
{
    void IBeforeRequestTask.Execute()
    {
        // code
    }

    void IAfterRequestTask.Execute()
    {
        // code
    }
}

So, we have explicit implementations. How would you register these interfaces?

I think we can't do this:

builder.RegisterType<IBeforeRequestTask> ...
builder.RegisterType<IAfterRequestTask> ...

I read in the Autofac Registration Docs that we must register concrete types.
A similar question but not treating Autofac is this one: TinyIoC: Register multiple interfaces on a single instance

Well, I know that this can be easy by doing 1-1 (one interface per class, then you register the concrete type). But now I am curious about it.
Don't remeber where I saw something like the below code using StructureMap (can be wrong):

action.AddTypesOf<IBeforeRequestTask>();
action.AddTypesOf<IAfterRequestTask>();

Hope I made myself clear. Thank you all.

EDIT
I created this sample to help. ;-)

Community
  • 1
  • 1
Lincoln Pires
  • 338
  • 4
  • 15
  • I don't really understand the question. Accessing members using an interface is done the same way regardless of whether the members are implicitly or explicitly implemented in the actual type. Have you observed a difference or is this a proactive "let's figure this out before we start doing it" type of question? – Lasse V. Karlsen Feb 05 '16 at 14:22
  • 1
    Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Feb 05 '16 at 15:32
  • Thanks @AndreasNiedermair... :-) - Well done. And I learned. +1 for teaching the right way to ask questions (I only read what appear when I was typing the question, things like similar questions and whatever). – Lincoln Pires Feb 05 '16 at 15:38
  • @LasseV.Karlsen You can clone the example on github. ;-) Thanks. – Lincoln Pires Feb 05 '16 at 15:39

1 Answers1

1

I'm not really sure what you want to achieve but if you want do register single class to be available in the container under two interfaces and you want to have single instance of it you can do it like this.

builder.RegisterType<TaskExecutor>().As<IBeforeRequestTask>().As<IAfterRequestTask>().SingleInstance();
Krzysztof Branicki
  • 7,417
  • 3
  • 38
  • 41