Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

up front tidy up of FakeItEasy extras code

  • Loading branch information...
commit fe78c8a8814550c462b5a322d813110133e153c8 1 parent 93c9f76
Adam Ralph adamralph authored
78 Extras/Source/Autofac.Extras.FakeItEasy/AutoFake.cs
View
@@ -38,14 +38,10 @@ namespace Autofac.Extras.FakeItEasy
[SecurityCritical]
public class AutoFake : IDisposable
{
+ private readonly IContainer _container;
private bool _disposed;
/// <summary>
- /// <see cref="IContainer"/> that handles the component resolution.
- /// </summary>
- public IContainer Container { get; private set; }
-
- /// <summary>
/// Initializes a new instance of the <see cref="AutoFake" /> class.
/// </summary>
/// <param name="strict">
@@ -68,7 +64,7 @@ public class AutoFake : IDisposable
var builder = new ContainerBuilder();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
builder.RegisterSource(new FakeRegistrationHandler(strict, callsBaseMethods, callsDoNothing, onFakeCreated));
- Container = builder.Build();
+ this._container = builder.Build();
}
/// <summary>
@@ -77,80 +73,90 @@ public class AutoFake : IDisposable
[SecuritySafeCritical]
~AutoFake()
{
- Dispose(false);
+ this.Dispose(false);
}
/// <summary>
- /// Disposes internal container.
+ /// Gets the <see cref="IContainer"/> that handles the component resolution.
/// </summary>
- [SecuritySafeCritical]
- public void Dispose()
+ public IContainer Container
{
- Dispose(true);
- GC.SuppressFinalize(this);
+ get { return this._container; }
}
/// <summary>
- /// Handles disposal of managed and unmanaged resources.
+ /// Disposes internal container.
/// </summary>
- /// <param name="disposing">
- /// <see langword="true" /> to dispose of managed resources (during a manual execution
- /// of <see cref="Autofac.Extras.FakeItEasy.AutoFake.Dispose()"/>); or
- /// <see langword="false" /> if this is getting run as part of finalization where
- /// managed resources may have already been cleaned up.
- /// </param>
- private void Dispose(bool disposing)
+ [SecuritySafeCritical]
+ public void Dispose()
{
- if (!_disposed)
- {
- if (disposing)
- {
- Container.Dispose();
- }
- _disposed = true;
- }
+ this.Dispose(true);
+ GC.SuppressFinalize(this);
}
/// <summary>
/// Resolve the specified type in the container (register it if needed)
/// </summary>
- /// <typeparam name="T">Service</typeparam>
+ /// <typeparam name="T">The type of the service.</typeparam>
/// <param name="parameters">Optional parameters</param>
/// <returns>The service.</returns>
public T Create<T>(params Parameter[] parameters)
{
- return Container.Resolve<T>(parameters);
+ return this.Container.Resolve<T>(parameters);
}
/// <summary>
/// Resolve the specified type in the container (register it if needed)
/// </summary>
- /// <typeparam name="TService">Service</typeparam>
+ /// <typeparam name="TService">The type of the service.</typeparam>
/// <typeparam name="TImplementation">The implementation of the service.</typeparam>
/// <param name="parameters">Optional parameters</param>
/// <returns>The service.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The component registry is responsible for registration disposal.")]
public TService Provide<TService, TImplementation>(params Parameter[] parameters)
{
- Container.ComponentRegistry.Register(
+ this.Container.ComponentRegistry.Register(
RegistrationBuilder.ForType<TImplementation>().As<TService>().InstancePerLifetimeScope().CreateRegistration());
- return Container.Resolve<TService>(parameters);
+ return this.Container.Resolve<TService>(parameters);
}
/// <summary>
/// Resolve the specified type in the container (register specified instance if needed)
/// </summary>
- /// <typeparam name="TService">Service</typeparam>
+ /// <typeparam name="TService">The type of the service.</typeparam>
+ /// <param name="instance">The instance to register if needed.</param>
/// <returns>The instance resolved from container.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The component registry is responsible for registration disposal.")]
public TService Provide<TService>(TService instance)
where TService : class
{
- Container.ComponentRegistry.Register(
+ this.Container.ComponentRegistry.Register(
RegistrationBuilder.ForDelegate((c, p) => instance).InstancePerLifetimeScope().CreateRegistration());
- return Container.Resolve<TService>();
+ return this.Container.Resolve<TService>();
+ }
+
+ /// <summary>
+ /// Handles disposal of managed and unmanaged resources.
+ /// </summary>
+ /// <param name="disposing">
+ /// <see langword="true" /> to dispose of managed resources (during a manual execution
+ /// of <see cref="Autofac.Extras.FakeItEasy.AutoFake.Dispose()"/>); or
+ /// <see langword="false" /> if this is getting run as part of finalization where
+ /// managed resources may have already been cleaned up.
+ /// </param>
+ private void Dispose(bool disposing)
+ {
+ if (!this._disposed)
+ {
+ if (disposing)
+ {
+ this.Container.Dispose();
+ }
+
+ this._disposed = true;
+ }
}
}
}
59 Extras/Source/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs
View
@@ -46,7 +46,12 @@ internal class FakeRegistrationHandler : IRegistrationSource
private readonly Action<object> _onFakeCreated;
/// <summary>
+ /// Initializes a new instance of the <see cref="FakeRegistrationHandler" /> class.
/// </summary>
+ /// <param name="strict">Whether fakes should be created with strict semantics.</param>
+ /// <param name="callsBaseMethods">Whether fakes should call base methods.</param>
+ /// <param name="callsDoNothing">Whether calls to fakes should do nothing.</param>
+ /// <param name="onFakeCreated">An action to perform on a fake when it is created.</param>
[SecurityCritical]
public FakeRegistrationHandler(bool strict, bool callsBaseMethods, bool callsDoNothing, Action<object> onFakeCreated)
{
@@ -56,25 +61,34 @@ public FakeRegistrationHandler(bool strict, bool callsBaseMethods, bool callsDoN
this._onFakeCreated = onFakeCreated;
// NOTE (adamralph): inspired by http://blog.functionalfun.net/2009/10/getting-methodinfo-of-generic-method.html
- Expression<Action> create = () => CreateFake<object>();
- _createMethod = (create.Body as MethodCallExpression).Method.GetGenericMethodDefinition();
+ Expression<Action> create = () => this.CreateFake<object>();
+ this._createMethod = (create.Body as MethodCallExpression).Method.GetGenericMethodDefinition();
}
/// <summary>
- /// Retrieve a registration for an unregistered service, to be used
+ /// Gets whether the registrations provided by this source are 1:1 adapters on top
+ /// of other components (I.e. like Meta, Func or Owned.)
+ /// </summary>
+ public bool IsAdapterForIndividualComponents
+ {
+ get { return false; }
+ }
+
+ /// <summary>
+ /// Retrieve registrations for an unregistered service, to be used
/// by the container.
/// </summary>
/// <param name="service">The service that was requested.</param>
- /// <param name="registrationAccessor"></param>
- /// <returns>
- /// Registrations for the service.
- /// </returns>
+ /// <param name="registrationAccessor">A function that will return existing registrations for a service.</param>
+ /// <returns>Registrations providing the service.</returns>
[SecuritySafeCritical]
- public IEnumerable<IComponentRegistration> RegistrationsFor
- (Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
+ public IEnumerable<IComponentRegistration> RegistrationsFor(
+ Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
{
if (service == null)
+ {
throw new ArgumentNullException("service");
+ }
var typedService = service as TypedService;
if (typedService == null ||
@@ -82,44 +96,39 @@ public IEnumerable<IComponentRegistration> RegistrationsFor
(typedService.ServiceType.IsGenericType && typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typedService.ServiceType.IsArray ||
typeof(IStartable).IsAssignableFrom(typedService.ServiceType))
+ {
return Enumerable.Empty<IComponentRegistration>();
+ }
- var rb = RegistrationBuilder.ForDelegate((c, p) => CreateFake(typedService))
+ var rb = RegistrationBuilder.ForDelegate((c, p) => this.CreateFake(typedService))
.As(service)
.InstancePerLifetimeScope();
return new[] { rb.CreateRegistration() };
}
- public bool IsAdapterForIndividualComponents
- {
- get { return false; }
- }
-
/// <summary>
/// Creates a fake object.
/// </summary>
/// <param name="typedService">The typed service.</param>
- /// <returns></returns>
+ /// <returns>A fake object.</returns>
[SecuritySafeCritical]
private object CreateFake(TypedService typedService)
{
- var specificCreateMethod =
- _createMethod.MakeGenericMethod(new[] { typedService.ServiceType });
- return specificCreateMethod.Invoke(this, null);
+ return this._createMethod.MakeGenericMethod(new[] { typedService.ServiceType }).Invoke(this, null);
}
[SecuritySafeCritical]
private T CreateFake<T>()
{
- var fake = A.Fake<T>(ApplyOptions);
+ var fake = A.Fake<T>(this.ApplyOptions);
- if (_callsBaseMethods)
+ if (this._callsBaseMethods)
{
A.CallTo(fake).CallsBaseMethod();
}
- if (_callsDoNothing)
+ if (this._callsDoNothing)
{
A.CallTo(fake).DoesNothing();
}
@@ -130,14 +139,14 @@ private T CreateFake<T>()
[SecuritySafeCritical]
private void ApplyOptions<T>(IFakeOptionsBuilder<T> options)
{
- if (_strict)
+ if (this._strict)
{
options.Strict();
}
- if (_onFakeCreated != null)
+ if (this._onFakeCreated != null)
{
- options.OnFakeCreated(x => _onFakeCreated(x));
+ options.OnFakeCreated(x => this._onFakeCreated(x));
}
}
}
8 Extras/Tests/Autofac.Extras.Tests.FakeItEasy/AutoFakeFixture.cs
View
@@ -126,12 +126,12 @@ public abstract class Bar : IBar
public bool Gone
{
- get { return _gone; }
+ get { return this._gone; }
}
public virtual void Go()
{
- _gone = true;
+ this._gone = true;
}
public IBar Spawn()
@@ -151,12 +151,12 @@ public class Baz : IBaz
public bool Gone
{
- get { return _gone; }
+ get { return this._gone; }
}
public virtual void Go()
{
- _gone = true;
+ this._gone = true;
}
}
24 Extras/Tests/Settings.StyleCop
View
@@ -0,0 +1,24 @@
+<StyleCopSettings Version="105">
+ <Analyzers>
+ <Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
+ <Rules>
+ <Rule Name="ElementsMustBeDocumented">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ <Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
+ <Rules>
+ <Rule Name="ElementsMustAppearInTheCorrectOrder">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ </Analyzers>
+</StyleCopSettings>
1  GlobalAssemblyInfo.cs
View
@@ -9,4 +9,3 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]
-
53 Settings.StyleCop
View
@@ -0,0 +1,53 @@
+<StyleCopSettings Version="105">
+ <GlobalSettings>
+ <CollectionProperty Name="RecognizedWords">
+ <Value>Func</Value>
+ </CollectionProperty>
+ <BooleanProperty Name="WriteCache">False</BooleanProperty>
+ <BooleanProperty Name="AutoCheckForUpdate">False</BooleanProperty>
+ </GlobalSettings>
+ <Analyzers>
+ <Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
+ <Rules>
+ <Rule Name="PropertySummaryDocumentationMustMatchAccessors">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ <Rule Name="FileMustHaveHeader">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ <Rule Name="FileHeaderMustShowCopyright">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings>
+ <BooleanProperty Name="IgnorePrivates">True</BooleanProperty>
+ </AnalyzerSettings>
+ </Analyzer>
+ <Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
+ <Rules>
+ <Rule Name="FieldNamesMustNotBeginWithUnderscore">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ <Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
+ <Rules>
+ <Rule Name="UsingDirectivesMustBePlacedWithinNamespace">
+ <RuleSettings>
+ <BooleanProperty Name="Enabled">False</BooleanProperty>
+ </RuleSettings>
+ </Rule>
+ </Rules>
+ <AnalyzerSettings />
+ </Analyzer>
+ </Analyzers>
+</StyleCopSettings>
Please sign in to comment.
Something went wrong with that request. Please try again.