Autofac differs from many related technologies in that it sticks as close to bare-metal C programming as possible. It is designed around the premise that it is wasteful to work in such a powerful language as C but to lose that power to purely reflection-based APIs like those prevalent in the other. NET containers. The result is that Autofac supports a wide array of application designs with very little additional infrastructure or integration code, and with a lower learning curve. This article uses an example to demonstrate the most basic techniques for creating and wiring together application components, then goes on to discuss the most important features of Autofac.

Author:Akisho Mukasa
Language:English (Spanish)
Published (Last):11 June 2012
PDF File Size:19.15 Mb
ePub File Size:9.35 Mb
Price:Free* [*Free Regsitration Required]

Build the container and store it for later use. During application execution… Create a lifetime scope from the container. Use the lifetime scope to resolve instances of the components. This getting started guide walks you through these steps for a simple console application. Once you have the basics down, you can check out the rest of the wiki for more advanced usage and integration information for WCF, ASP.

NET, and other application types. Write DateTime. Other application types may use additional Autofac integration libraries. The easiest way to do this is through NuGet. A component is an expression,.

NET type, or other bit of code that exposes one or more services and can take in other dependencies. In simple terms, think about a. We also need to store the container so it can be used to resolve types later. You do this by resolving them from a lifetime scope. The container itself is a lifetime scope, and you can technically just resolve things right from the container. It is not recommended to resolve from the container directly, however.

When you resolve a component, depending on the instance scope you define , a new instance of the object gets created. Some components may need to be disposed like they implement IDisposable - Autofac can handle disposing those components for you when the lifetime scope is disposed.

However, the container lives for the lifetime of your application. If you resolve a lot of stuff directly from the container, you may end up with a lot of things hanging around waiting to be disposed. Instead, create a child lifetime scope from the container and resolve from that. It does this to avoid any memory leaks - if IDateWriter or its dependencies are disposable, they will be automatically disposed when the scope is disposed.

Autofac sees that the TodayWriter needs an IOutput in its constructor. Autofac uses the new ConsoleOutput instance to finish constructing the TodayWriter. Autofac returns the fully-constructed TodayWriter for WriteDate to consume. The call to writer. WriteDate goes to the brand new TodayWriter.

The Autofac lifetime scope is disposed. Any disposable items that were resolved from that lifetime scope are also disposed. Later, if you want your application to write a different date, you could implement a different IDateWriter and then change the registration at app startup.

Yay, inversion of control! Note: generally speaking, service location is largely considered an anti-pattern see article. That is, manually creating scopes everywhere and sprinkling use of the container through your code is not necessarily the best way to go. Of course, how you design your app is up to you. Check out the list of integration libraries to see how to integrate Autofac with your application. Learn about the ways to register components that add flexibility.

Learn about Autofac configuration options that allow you to better manage your component registrations. Need Help?


Dependency Injection with Autofac

NET 4. 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. From Wikipedia : In software engineering, inversion of control IoC is a design principle in which custom-written portions of a computer program receive the flow of control from a generic framework. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code. In the NuGet-Solution tab, type in "Autofac" in the search box.


Implement Dependency Injection Using Autofac IoC Container



Subscribe to RSS



Autofac Project


Related Articles