Click or drag to resize

Module Class

Base class for user-defined modules. Modules can add a set of related components to a container (Load(ContainerBuilder)) or attach cross-cutting functionality to other components (AttachToComponentRegistration(IComponentRegistryBuilder, IComponentRegistration). Modules are given special support in the XML configuration feature - see http://code.google.com/p/autofac/wiki/StructuringWithModules.
Inheritance Hierarchy

Namespace:  Autofac
Assembly:  Autofac (in Autofac.dll) Version: 6.0.0+39696a967e8826f7f1ebc8c1ff4523c9dd75abe0
Syntax
public abstract class Module : IModule

The Module type exposes the following members.

Constructors
  NameDescription
Protected methodModule
Initializes a new instance of the Module class
Top
Properties
  NameDescription
Protected propertyThisAssembly
Gets the assembly in which the concrete module type is located. To avoid bugs whereby deriving from a module will change the target assembly, this property can only be used by modules that inherit directly from Module.
Top
Methods
  NameDescription
Protected methodAttachToComponentRegistration
Override to attach module-specific functionality to a component registration.
Protected methodAttachToRegistrationSource
Override to perform module-specific processing on a registration source.
Public methodConfigure
Apply the module to the component registry.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodLoad
Override to add registrations to the container.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks
Provides a user-friendly way to implement IModule via ContainerBuilder.
Examples
Defining a module:
public class DataAccessModule : Module
{
    public string ConnectionString { get; set; }

    public override void Load(ContainerBuilder moduleBuilder)
    {
        moduleBuilder.RegisterGeneric(typeof(MyRepository<>))
            .As(typeof(IRepository<>))
            .InstancePerMatchingLifetimeScope(WebLifetime.Request);

        moduleBuilder.Register(c => new MyDbConnection(ConnectionString))
            .As<IDbConnection>()
            .InstancePerMatchingLifetimeScope(WebLifetime.Request);
    }
}
Using the module...
var builder = new ContainerBuilder();
builder.RegisterModule(new DataAccessModule { ConnectionString = "..." });
var container = builder.Build();
var customers = container.Resolve<IRepository<Customer>>();
See Also