Castle Windsor Documentation

repository·master·Indexed 23 days ago

https://github.com/castleproject/windsor

A mature Inversion of Control (IoC) container for .NET designed to manage object dependencies and lifetimes. It features a robust framework for component registration via installers, support for various lifestyles (including Scoped and Transient), and specialized integration facilities for ASP.NET Core 2.x and ActiveRecord/NHibernate.

Tokens
79.1K
Snippets
221
Records
371
Agent score
81%

What's inside Castle Windsor

  1. Overview of Castle Windsor extension points

    master

    Castle Windsor is designed to be extended rather than providing every capability out of the box. It exposes several layers of extension points that allow you to modify or augment the container's behavior. These range from low-level subsystems to high-level facilities and lifecycle management.

    Common extension points include:

    • Facilities: The primary extension point, often encompassing multiple other extension points.
    • Lifestyle Managers: Control when instances are created, reused, or disposed.
    • Lifecycle Concerns: Execute logic during component creation or decommissioning.
    • Handler Selectors: Custom logic for selecting components (useful for multi-tenant apps).
    • Component Activators: Control the actual instantiation process of components.
    • Resolvers: Override the logic used to resolve components.
    • ComponentModel Construction Contributors: Inspect or modify the ComponentModel during registration.
    • Lazy Component Loaders: Enable just-in-time registration (e.g., pulling from MEF or WCF).
    • Model Interceptors Selectors: Dynamically select interceptors for specific components.
    • Release Policy: Handle how components are tracked and released.
    • SubSystems: The innermost extension point, rarely swapped.
    • Container Events: Subscribe to events occurring within the container.
  2. Use Castle Windsor for Inversion of Control in .NET

    master
    Castle Windsor is a mature Inversion of Control (IoC) container for the .NET ecosystem. It provides a robust framework for managing object lifetimes and dependencies within your applications.
  3. Use XML configuration in Castle Windsor

    master

    While the Fluent Registration API is the recommended way to register components, Castle Windsor supports XML configuration for several container-related tasks. You can store configuration in app.config, web.config, custom dedicated files, or embedded assembly resources.

    XML configuration is useful for:

    • Providing configuration-time properties to components (e.g., connection strings).
    • Registering installers.
    • Registering and configuring facilities.
    • Registering and configuring components (though code-based registration is generally preferred).

    Note that XML registration is less powerful than the Fluent Registration API and some tasks can only be accomplished via code.

  4. WCF Integration Facility capabilities

    master

    The WCF Integration facility enables integration with Windows Communication Foundation (WCF) by providing several key features:

    Client-side features

    • Dynamic Proxy Replacement: Windsor replaces standard WCF client-side remoting proxies with its own Dynamic Proxy. This allows you to apply interceptors to WCF proxies just like any other Windsor component.
    • Asynchronous Calls: Enables performing asynchronous calls without the need for code-generated client-side proxies.
    • Channel Recycling: Provides the ability to transparently recycle the channel when it gets closed or faulted.

    Server-side features

    • WCF Lifestyles: Provides specialized lifestyles such as PerWcfSession and PerWcfOperation for managing component lifetimes within WCF service contexts.

    General features

    • Makes services and WCF proxies available as services in your application.
    • Supports non-default constructors and dependency injection into WCF services.
    • Allows easy service setup using extensions.
  5. Windsor ASP.NET MVC Tutorial Roadmap

    master

    The tutorial is divided into two main phases: getting started and advanced implementation.

    Phase 1: Getting Started

    • Getting Windsor: Downloading and adding Windsor assemblies to your project.
    • Plugging Windsor In: Creating a custom controller factory to bridge ASP.NET MVC and Windsor.
    • Writing Your First Installer: Creating Windsor installers and testing them (validating conventions).
    • Putting It All Together: Integrating all components into a working application.

    Phase 2: Advanced Implementation

    • Logging Support: Using Windsor's logging facility.
    • Persistence Layer: Creating custom facilities, registering externally created objects, and setting up NHibernate.
    • Lifestyles: Using different lifestyles, specifically per-web-request.
    • Satisfying Dependencies: Understanding dependency resolution and specification approaches.
    • Diagnosing Issues: Troubleshooting exceptions thrown by the container when dependencies are missing.
  6. What is a handler in Castle Windsor

    master
    In Castle Windsor, a handler is a type that implements the IHandler interface. Handlers are responsible for the lifecycle of components: they are used by the container to resolve components for specific services and to release them once they are no longer needed. Additionally, handlers provide access to the ComponentModel, which allows developers to programmatically inspect component metadata and configuration.
  7. What is a Service in Windsor?

    master

    In Castle Windsor, a service is an abstract contract that describes a cohesive unit of functionality. It defines what a unit of functionality does without specifying how it is implemented.

    In .NET, services are typically defined using interfaces. A good service contract is high-level and does not leak implementation details (such as specific database types or internal class names) to the consumer.

    public interface ICoffeeShop
    {
       Future<Coffee> GetCoffee(CoffeeRequest request);
    }
  8. What is Inversion of Control (IoC)?

    master
    Inversion of Control (IoC) is a design principle where a framework manages the flow of control and makes invocations on the developer's objects, rather than the developer's code making calls to an API. This inversion allows developers to extend frameworks or build applications where the framework is responsible for orchestrating the execution based on specific stimuli.
  9. What is a Dependency in Windsor?

    master

    A dependency is a requirement that a component needs to fulfill its service contract. Components rarely work in isolation; they delegate tasks to other services or rely on external configuration.

    There are two main types of dependencies:

    1. Service Dependencies: A component depends on the services provided by other components. This enables decoupled code where components interact through abstract interfaces rather than concrete implementations.
    2. Configuration Dependencies: Non-service values required by a component, such as connectionStrings, server names, timeouts, or other configuration parameters.

    In C#, dependencies are typically declared via constructor arguments or settable properties.

  10. What is the Typed Factory Facility?

    master

    The Typed Factory Facility provides automatically generated Abstract Factories that allow you to create components while remaining agnostic to the presence of the Windsor container.

    Instead of using the Service Locator pattern or referencing the container directly (both of which are considered inferior approaches), you should use typed factories whenever you need to pull a component from the container manually. This is particularly useful in scenarios where an object needs dependencies supplied after its initial creation, such as when handling external events.

  11. What is a Component in Windsor?

    master

    A component is a concrete implementation of one or more services. While a service is an abstract contract, a component is the actual entity managed by the container that performs the work.

    Key characteristics of components:

    • Implementation: Usually a class that implements a service interface.
    • Multiplicity: You can have multiple components that implement the same service (e.g., two different coffee shop implementations).
    • Shared Implementation: Multiple distinct components can be backed by the same class but configured differently (e.g., two instances of a database session factory class, each with different connection strings).
    • Multiple Services: A single component can expose multiple different services.
    • Exclusion: Classes that do not provide services (like DTOs or pure domain models) are generally not registered as components in the container.
    public class Starbucks: ICoffeeShop
    {
       public Future<Coffee> GetCoffee(CoffeeRequest request)
       {
          // some implementation
       }
    }
  12. What is a Component Activator?

    master

    A Component Activator is a mechanism used by Windsor to instantiate components and perform setup logic, such as setting properties or decorating services with proxies.

    Important Lifetime Note: Activators are responsible for instantiation. Every call to the activator's Create method must return a new instance. If you need to manage component lifetimes (e.g., Singletons or Scoped instances), you should use a LifeStyle manager instead of trying to manage state within an activator.