Giter VIP home page Giter VIP logo

autofac's Introduction

Autofac

Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.

NuGet Packages

You can get Autofac by grabbing the latest NuGet packages or using our NuGet script builder to get exactly what you need. A few older versions remain for download here.

Getting Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on [Stack Overflow](http://stackoverflow.com/questions/tagged/autofac Stack Overflow) or check out the discussion forum.

Getting Started

Our Getting Started tutorial walks you through integrating Autofac with a simple application and gives you some starting points for learning more.

Adding Components

Components are registered with a ContainerBuilder:

var builder = new ContainerBuilder();

Autofac can use a Linq expression, a .NET type, or a pre-built instance as a component:

builder.Register(c => new TaskController(c.Resolve<ITaskRepository>()));

builder.RegisterType<TaskController>();

builder.RegisterInstance(new TaskController());

Or, Autofac can find and register the component types in an assembly:

builder.RegisterAssemblyTypes(controllerAssembly);

Calling Build() creates a container:

var container = builder.Build();

To retrieve a component instance from a container, a service is requested. By default, components provide their concrete type as a service:

var taskController = container.Resolve<TaskController>();

To specify that the component’s service is an interface, the As() method is used at registration time:

builder.RegisterType<TaskController>().As<IController>();
// enabling
var taskController = container.Resolve<IController>();

Expressing Dependencies

When Autofac instantiates a component, it satisfies the component's dependencies by finding and instantiating other components.

Components express their dependencies to Autofac as constructor parameters:

public class TaskController : IController
{
    public TaskController(ITaskRepository tasks) { ... }
}

In this case Autofac will look for another component that provides the ITaskRepository service and call the constructor of TaskController with that component as a parameter.

If there is more than one constructor on a component type, Autofac will use the constructor with the most resolvable parameters.

public TaskController(ITaskRepository tasks)
public TaskController(ITaskRepository tasks, ILog log)

Default parameter values can be used to express optional dependencies (properties can be used instead if you prefer):

public TaskController(ITaskRepository tasks, ILog log = null)

Circular references can be constructed by declaring one of the parameters to be of type Lazy<T>.

public TaskController(Lazy<ITaskRepository> tasks)

Autofac understands an advanced vocabulary of "relationship types" like Lazy<T>, Func<T>, IEnumerable<T> and others, to vary the relationship between a component and its dependencies.

Highlights

Autofac keeps out of your way and places as few constraints on your design as possible.

Simple Extension Points: Activation events like OnActivating(e => e.Instance.Start()) can achieve a lot of customization in very little code.

Robust Resource Management: Autofac takes on the burden of tracking disposable components to ensure that resources are released when they should be.

Flexible Module System: Strike a balance between the deployment-time benefits of XML configuration and the clarity of C# code with Autofac modules.

Status

Autofac moved to GitHub on the 22nd January, 2013. The process of cleaning up the issues list and wiki content is ongoing. You may stumble across some invalid links while we sort out problems from the migration. The code and NuGet packages all remain in a consistent state.

You can get the latest releases from NuGet. Release notes are available on the wiki.

If you're feeling bold, you can get continuous integration builds from MyGet.

There is a growing number of integrations that make using Autofac with your application a snap. Support for several popular frameworks is also available through the "Extras" packages.

Autofac is licensed under the MIT license, so you can comfortably use it in commercial applications (we still love contributions though).

Contributing

Refer to the Readme for Autofac Developers for setting up, building Autofac and generating the related documentation. We also have a contributors guide to help you get started.

autofac's People

Contributors

abdullin avatar adamralph avatar alexandrnikitin avatar alexmg avatar blairconrad avatar brendankowitz avatar carlhoerberg avatar cvertex avatar drusellers avatar ivanferic avatar jamesottaway avatar kalebpederson avatar kcuzner avatar nblumhardt avatar plillevold avatar raishey avatar shiftkey avatar taschmidt avatar tbenade avatar tillig avatar yshayy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.