forked from autofac/Autofac
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
247 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
====================== | ||
Common Service Locator | ||
====================== | ||
====================== | ||
|
||
The `Autofac.Extras.CommonServiceLocator <http://www.nuget.org/packages/Autofac.Extras.CommonServiceLocator/>`_ package allows you to use Autofac as the backing store for services in places where you require `Microsoft Common Service Locator <http://www.nuget.org/packages/CommonServiceLocator/>`_ integration. | ||
|
||
The Autofac.Extras.CommonServiceLocator package will also work in conjunction with the :doc:`Autofac Microsoft Enterprise Library integration package <entlib>`. | ||
|
||
To use the Common Service Locator integration, build your Autofac container as normal, then simply set the current service locator to an ``AutofacServiceLocator``. | ||
|
||
.. sourcecode:: csharp | ||
|
||
var builder = new ContainerBuilder(); | ||
|
||
// Perform registrations and build the container. | ||
var container = builder.Build(); | ||
|
||
// Set the service locator to an AutofacServiceLocator. | ||
var csl = new AutofacServiceLocator(container); | ||
ServiceLocator.SetLocatorProvider(() => csl); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,34 @@ | ||
==================== | ||
Enterprise Library 5 | ||
==================== | ||
==================== | ||
|
||
`The Autofac.Extras.EnterpriseLibraryConfigurator package <http://www.nuget.org/packages/Autofac.Extras.EnterpriseLibraryConfigurator/>`_ provides a way to use Autofac as the backing store for dependency injection in `Microsoft Enterprise Library 5 <http://entlib.codeplex.com/releases/view/43135>`_ instead of using Unity. It does this in conjunction with :doc:`the Autofac Common Service Locator implementation <csl>`. | ||
|
||
**In Enterprise Library 6, Microsoft removed the tightly-coupled dependency resolution mechanisms from the application blocks so there's no more need for this configurator past Enterprise Library 5.** | ||
|
||
Using the Configurator | ||
====================== | ||
|
||
The simplest way to use the configurator is to set up your Enterprise Library configuration in your ``app.config`` or ``web.config`` and use the ``RegisterEnterpriseLibrary()`` extension. This extension parses the configuration and performs the necessary registrations. You then need to set the ``EnterpriseLibraryContainer.Current`` to use an ``AutofacServiceLocator`` from :doc:`the Autofac Common Service Locator implementation <csl>`. | ||
|
||
.. sourcecode:: csharp | ||
|
||
var builder = new ContainerBuilder(); | ||
builder.RegisterEnterpriseLibrary(); | ||
var container = builder.Build(); | ||
var csl = new AutofacServiceLocator(container); | ||
EnterpriseLibraryContainer.Current = csl; | ||
|
||
Specifying a Registration Source | ||
================================ | ||
|
||
The ``RegisterEnterpriseLibrary()`` extension does allow you to specify your own ``IConfigurationSource`` so if your configuration is not in ``app.config`` or ``web.config`` you can still use Autofac. | ||
|
||
.. sourcecode:: csharp | ||
|
||
var config = GetYourConfigurationSource(); | ||
var builder = new ContainerBuilder(); | ||
builder.RegisterEnterpriseLibrary(config); | ||
var container = builder.Build(); | ||
var csl = new AutofacServiceLocator(container); | ||
EnterpriseLibraryContainer.Current = csl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,146 @@ | ||
========== | ||
FakeItEasy | ||
========== | ||
========== | ||
|
||
The `FakeItEasy <http://fakeiteasy.github.io>`_ integration package allows you to automatically create fake dependencies for both concrete and fake abstract instances in unit tests using an Autofac container. You can `get the Autofac.Extras.FakeItEasy package on NuGet <https://nuget.org/packages/Autofac.Extras.FakeItEasy>`_. | ||
|
||
Getting Started | ||
=============== | ||
|
||
Given you have a system under test and a dependency: | ||
|
||
.. sourcecode:: csharp | ||
|
||
public class SystemUnderTest | ||
{ | ||
public SystemUnderTest(IDependency dependency) | ||
{ | ||
} | ||
} | ||
|
||
public interface IDependency | ||
{ | ||
} | ||
|
||
When writing your unit test, use the ``Autofac.Extras.FakeItEasy.AutoFake`` class to instantiate the system under test. Doing this will automatically inject a fake dependency into the constructor for you. | ||
|
||
.. sourcecode:: csharp | ||
|
||
[Test] | ||
public void Test() | ||
{ | ||
using (var fake = new AutoFake()) | ||
{ | ||
// The AutoFake class will inject a fake IDependency | ||
// into the SystemUnderTest constructor | ||
var sut = fake.Create<SystemUnderTest>(); | ||
} | ||
} | ||
|
||
Configuring Fakes | ||
================= | ||
|
||
You can configure the automatic fakes and/or assert calls on them as you would normally with FakeItEasy. | ||
|
||
.. sourcecode:: csharp | ||
|
||
[Test] | ||
public void Test() | ||
{ | ||
using (var fake = new AutoFake()) | ||
{ | ||
// Arrange - configure the fake | ||
A.CallTo(() => fake.Create<IDependency>().GetValue()).Returns("expected value"); | ||
var sut = fake.Create<SystemUnderTest>(); | ||
|
||
// Act | ||
var actual = sut.DoWork(); | ||
|
||
// Assert - assert on the fake | ||
A.CallTo(() => fake.Create<IDependency>().GetValue()).MustHaveHappened(); | ||
Assert.AreEqual("expected value", actual); | ||
} | ||
} | ||
|
||
public class SystemUnderTest | ||
{ | ||
private readonly IDependency dependency; | ||
|
||
public SystemUnderTest(IDependency strings) | ||
{ | ||
this.dependency = strings; | ||
} | ||
|
||
public string DoWork() | ||
{ | ||
return this.dependency.GetValue(); | ||
} | ||
} | ||
|
||
public interface IDependency | ||
{ | ||
string GetValue(); | ||
} | ||
|
||
Configuring Specific Dependencies | ||
================================= | ||
|
||
You can configure the ``AutoFake`` to provide a specific instance for a given service type: | ||
|
||
.. sourcecode:: csharp | ||
|
||
[Test] | ||
public void Test() | ||
{ | ||
using (var fake = new AutoFake()) | ||
{ | ||
var dependency = new Dependency(); | ||
fake.Provide(dependency); | ||
|
||
// ...and the rest of the test. | ||
} | ||
} | ||
|
||
You can also configure the ``AutoFake`` to provide a specific implementation type for a given service type: | ||
|
||
.. sourcecode:: csharp | ||
|
||
[Test] | ||
public void Test() | ||
{ | ||
using (var fake = new AutoFake()) | ||
{ | ||
// Configure a component type that doesn't require | ||
// constructor parameters. | ||
fake.Provide<IFoo, ConcreteFoo>(); | ||
|
||
// Configure a component type that has some | ||
// constructor parameters passed in. | ||
fake.Provide<IBar, ConcreteBar>("baz", 123, false); | ||
|
||
// ...and the rest of the test. | ||
} | ||
} | ||
|
||
Options for Fakes | ||
================= | ||
|
||
You can specify options for fake creation using optional constructor parameters on ``AutoFake``: | ||
|
||
.. sourcecode:: csharp | ||
|
||
using(var fake = new AutoFake( | ||
// Create fakes with strict behavior (unconfigured calls throw exceptions) | ||
strict: true, | ||
|
||
// Calls to fakes of abstract types will call the base methods on the abstract types | ||
callsBaseMethods: true, | ||
|
||
// Calls to fake methods will return null rather than generated fakes | ||
callsDoNothing: true, | ||
|
||
// Provide an action to perform upon the creation of each fake | ||
onFakeCreated: f => { ... })) | ||
{ | ||
// Use the fakes/run the test. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,55 @@ | ||
===================================== | ||
Managed Extensibility Framework (MEF) | ||
===================================== | ||
===================================== | ||
|
||
[TODO: Add documentation about ``RegisterMetadataRegistrationSources()``] | ||
|
||
The Autofac MEF integration allows you to expose extensibility points in your applications using the `Managed Extensibility Framework <http://msdn.microsoft.com/en-us/library/dd460648(VS.100).aspx>`_. | ||
|
||
To use MEF in an Autofac application, you must reference the .NET framework ``System.ComponentModel.Composition.dll`` assembly and get the `Autofac.Mef <http://www.nuget.org/packages/Autofac.Mef/>`_ package from NuGet. | ||
|
||
Consuming MEF Extensions in Autofac | ||
=================================== | ||
|
||
The Autofac/MEF integration allows MEF catalogs to be registered with the ``ContainerBuilder``, then use the ``RegisterComposablePartCatalog()`` extension method. | ||
|
||
.. sourcecode:: csharp | ||
|
||
var builder = new ContainerBuilder(); | ||
var catalog = new DirectoryCatalog(@"C:\MyExtensions"); | ||
builder.RegisterComposablePartCatalog(catalog); | ||
|
||
All MEF catalog types are supported: | ||
|
||
* ``TypeCatalog`` | ||
* ``AssemblyCatalog`` | ||
* ``DirectoryCatalog`` | ||
|
||
Once MEF catalogs are registered, exports within them can be resolved through the Autofac container or by injection into other components. For example, say you have a class with an export type defined using MEF attributes: | ||
|
||
.. sourcecode:: csharp | ||
|
||
[Export(typeof(IService))] | ||
public class Component : IService { } | ||
|
||
Using MEF catalogs, you can register that type. Autofac will find the exported interface and provide the service. | ||
|
||
.. sourcecode:: csharp | ||
|
||
var catalog = new TypeCatalog(typeof(Component)); | ||
builder.RegisterComposablePartCatalog(catalog); | ||
var container = builder.Build(); | ||
|
||
// The resolved IService will be implemented | ||
// by type Component. | ||
var obj = container.Resolve<IService>(); | ||
|
||
Providing Autofac Components to MEF Extensions | ||
============================================== | ||
|
||
Autofac components aren't automatically available for MEF extensions to import. To provide an Autofac component to MEF, the ``Exported()`` extension method must be used: | ||
|
||
.. sourcecode:: csharp | ||
|
||
builder.RegisterType<Component>() | ||
.Exported(x => x.As<IService>().WithMetadata("SomeData", 42)); |