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.

Author:Kazratilar Tetilar
Language:English (Spanish)
Genre:Personal Growth
Published (Last):22 September 2009
PDF File Size:10.97 Mb
ePub File Size:4.7 Mb
Price:Free* [*Free Regsitration Required]

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. The Example Application The application is a console program that checks a list of memos, each with a due date, and notifies the user of the ones that are overdue.

A lot of simplifications have been made in the example code. In the interests of brevity, XML comments, argument checking, and exception handling are elided from all of the code samples. The TextWriter type is a standard.

NET class used throughout the. AsQueryable ; The IQueryable interface introduced in. NET 3. Out ; checker. For one thing, the code that did this at startup would get increasingly complex over time, and would potentially need to be reorganized every time the dependencies of a class change. More importantly, it is hard to switch the implementations of services; e. This is the problem of composability, and is worth an article in itself. When using Autofac, accessing the MemoChecker is separate from creating it: container.

CheckNow ; The container. So, how does the container work out how to create a MemoChecker? Component Registrations A dependency injection container is a collection of registrations that map services to components. A service, in this context, is a way of identifying a particular functional capability — it could be a textual name, but is more often an interface type.

A registration captures the dynamic behaviour of the component within the system. The most noticeable aspect of this is the way in which instances of the component are created.

Autofac can accept registrations that create components using expressions, provided instances, or with Reflection based on System. The following sets up a registration for the MemoChecker component: builder.

These are retrieved inside the lambda expression by calling the Resolve method on the container, which is passed in as the parameter c. The expression being provided to Register has a return type of MemoChecker, so Autofac will use this as the default service for this registration unless another is specified with the As method. The As call can be included for the sake of being more explicit: builder.

This is important, because it eliminates one point of reliance on the order in which components are registered. Out: builder. RegisterInstance memos ; builder. RegisterInstance Console. ExternallyOwned ; The memos and Console. Out are provided to the container as instances that are already created. For an explanation of ExternallyOwned , see Deterministic Disposal. Registering a Component with its Implementation Type Autofac can also create components the way that other containers do, using reflection many optimise this scenario with MSIL-generation.

This means that you can tell Autofac about the type that provides a service, and it will work out how to call the most appropriate constructor, with parameters chosen according to the other available services. The MemoChecker registration could have been replaced with: builder. GetTypes if typeof IController. IsAssignableFrom t builder. Register t ; This makes large numbers of components available without the overhead of registering each one, and you should definitely consider it in these situations.

Autofac provides shortcuts for registering batches of components this way: builder. RegisterAssemblyTypes Assembly. The most important difference to note is that each component is now configured independently of all the others. As more components are added to the system, they can be understood purely in terms of the services they expose and the services they require. This is one effective means of controlling architectural complexity.

Deterministic Disposal IDisposable is both a blessing and a curse. Unfortunately, which component should do this cleanup, and when, is not always easy to determine. The problem is made worse by designs that allow for multiple implementations of the same service. In the example application, it is feasible that many different implementations of IMemoDueNotifier may be deployed.

Some of these will be created in a factory, some will be singletons, some will need disposal, and some will not. Components that use a notifier have no way of knowing whether they should try to cast it to IDisposable and call Dispose or not.

The kind of bookkeeping that results is both error-prone and tedious. Autofac solves this problem by tracking all of the disposable objects created by the container. This is important because true to the spirit of separating usage from configuration concerns, the MemoChecker service can be used wherever necessary — even created indirectly as a dependency of another component — without worrying as to whether or not it should be cleaned up.


Implement Dependency Injection Using Autofac IoC Container

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.


Dependency Injection with Autofac



Autofac Project


Related Articles