4

I have already done the following:

  1. Register an instance of my Logger in unity via a ILogger interface.
  2. Created an interface, ILoggableObject, that has a method, Hook(ILogger logger), to inject my logger.

I would like to accomplish this:

Everytime I ask for any resolution from unity, if that object implements ILoggableObject, automatically inject the ILogger interface via the Hook method.

I think this is possible via interception or policies?

Any help would be awesome.

asked Dec 6, 2010 at 16:05
7
  • 3
    Why don't you use the constructor injection, which is the default mechanism? Simply rename the Hook method to a public constructor (with the same arguments). Commented Dec 6, 2010 at 16:10
  • Why not just pass ILogger into whatever objects ctor that needs to use the logging behavior? Commented Dec 6, 2010 at 16:11
  • That is how I was doing it. The problem with that approach is when your objects start using multiple interfaces, you end up with a constructor that has 10 parameters. Then, another developer who wants to inherit from that base class has to supply all of the constructor parameters that he / she has no need to see. Commented Dec 6, 2010 at 16:15
  • You can use Named parameter for calling constructor to avoid this. Commented Dec 6, 2010 at 16:34
  • 5
    @poindexter12: You should never have a constructor that accepts 10 parameters, 5-6 at most. If you do, then theres clearly something wrong with it. And a class like that should be refactored into smaller parts. Commented Dec 6, 2010 at 16:41

3 Answers 3

2

What you are looking for is TypeInterception in Unity. See here: http://msdn.microsoft.com/en-us/library/ff660861(PandP.20).aspx

Also here http://msdn.microsoft.com/en-us/library/ff660848(v=PandP.20).aspx

You want to intercept the call to the constructor and inject the Logger on behalf of the calling code without them being any wiser.

While I haven't done it before I believe you can do what you want using Intercept.NewInstance() http://msdn.microsoft.com/en-us/library/ff662093(PandP.20).aspx

answered Dec 6, 2010 at 17:40
Sign up to request clarification or add additional context in comments.

Comments

1

That is a horrible way to (attempt to) do Dependency Injection. Use Constructor Injection instead and inject (via the constructor) the ILogger into the consumer that right now has the Hook method.

answered Dec 6, 2010 at 16:12

4 Comments

This doesn't answer my question at all. While I appreciate the feedback, I am converting away from that method because of my comment above.
However, that reason is a fallacy: too many constructor arguments aren't an indication that Constructor Injection is at fault, but rather that your class has too many responsibilities: stackoverflow.com/questions/2420193/…
Okay, I read your blog post. It's great. I agree with all of your principles. I don't think you are attempting to answer my question though. While you like constructor injection, others may like property or method injection. The principal is the same. I agree that it would be nice for me to aggregate all of my ancillary services into one aggregate service. I will look into doing this immediately. My question still stands. If I have one aggregate service that I would like injected automatically via a method or property, how can I do this automatically?
I'm sorry that I wasn't more constructive, but it looks like you got the answer you were looking for. For the record, though, I don't agree that the choice between Constructor, Property or Method Injection is a matter of taste. They are each appropriate in different situations.
0

While I really like Unity and IoC/DI, I'm not sure that it is the right tool to accomplish what you want to do.

You might consider looking at aspect-oriented programming using PostSharp or a similar tool. This would allow you to add logging or other cross-cutting concerns without changing the classes being instrumented.

answered Dec 6, 2010 at 17:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.