Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Castle Windsor to automatically inject a property?

I have a property on my classes for logging service.

private ILogger logger = NullLogger.Instance;
public ILogger Logger
{
    get { return logger; }
    set { logger = value; }
}

And I have this in my component registration:

container.AddFacility<LoggingFacility>(x => new LoggingFacility(LoggerImplementation.Log4net));

However, Windsor doesn't seem to inject the Logger - am I missing something?

like image 635
Chris Haines Avatar asked Jun 29 '09 14:06

Chris Haines


People also ask

What is Castle Windsor dependency injection?

Introduction. Dependency Injection (DI) is an established design pattern describing how objects acquire their dependencies. This pattern is often facilitated by an Inversion of Control (IoC) container, which is used at runtime. to resolve and inject dependencies as objects are instantiated.

What is property based injection?

Property injection is a type of dependency injection where dependencies are provided through properties. Visit the Dependency Injection chapter to learn more about it. Let's understand how we can perform property injection using Unity container. Consider the following example classes.


1 Answers

The lambda parameter for AddFacility is actually a creation callback (it gets called when the facility is created), not a factory.

Use this instead:

container.AddFacility("logging", new LoggingFacility(LoggerImplementation.Log4net, "path_to_log4net.config"));

BTW Windsor automatically injects property dependencies whenever it can.

like image 62
Mauricio Scheffer Avatar answered Sep 18 '22 12:09

Mauricio Scheffer