Welcome home ScopedInstance

This commit is contained in:
Hao Kung 2015-03-11 15:03:29 -07:00
parent c1a026cbf7
commit f3a9ce1ec1
4 changed files with 107 additions and 8 deletions

View File

@ -0,0 +1,12 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.Mvc
{
public interface IScopedInstance<TValue> : IDisposable
{
TValue Value { get; set; }
}
}

View File

@ -0,0 +1,21 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.Mvc
{
public class ScopedInstance<T> : IScopedInstance<T>
{
public T Value { get; set; }
public void Dispose()
{
var disposable = Value as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
}

View File

@ -15,16 +15,14 @@ namespace Microsoft.Framework.DependencyInjection
{ {
public static class MvcServiceCollectionExtensions public static class MvcServiceCollectionExtensions
{ {
public static IServiceCollection AddMvc([NotNull] this IServiceCollection services) public static IServiceCollection AddMvc([NotNull] this IServiceCollection services, IConfiguration config)
{ {
return AddMvc(services, configuration: null); return services.AddMvc();
} }
public static IServiceCollection AddMvc( public static IServiceCollection AddMvc([NotNull] this IServiceCollection services)
[NotNull] this IServiceCollection services,
IConfiguration configuration)
{ {
ConfigureDefaultServices(services, configuration); ConfigureDefaultServices(services);
services.TryAdd(MvcServices.GetDefaultServices()); services.TryAdd(MvcServices.GetDefaultServices());
return services; return services;
} }
@ -90,12 +88,12 @@ namespace Microsoft.Framework.DependencyInjection
return WithControllersAsServices(services, controllerTypes.Select(type => type.AsType())); return WithControllersAsServices(services, controllerTypes.Select(type => type.AsType()));
} }
private static void ConfigureDefaultServices(IServiceCollection services, IConfiguration configuration) private static void ConfigureDefaultServices(IServiceCollection services)
{ {
services.AddOptions(); services.AddOptions();
services.AddDataProtection(); services.AddDataProtection();
services.AddRouting(); services.AddRouting();
services.AddAuthorization(configuration); services.AddAuthorization();
services.AddWebEncoders(); services.AddWebEncoders();
services.Configure<RouteOptions>( services.Configure<RouteOptions>(
routeOptions => routeOptions.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint))); routeOptions => routeOptions.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint)));

View File

@ -0,0 +1,68 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class ScopedInstanceTest
{
[Fact]
public void ScopedInstanceDisposesIDisposables()
{
var disposable = new Disposable();
// Arrange
var scopedInstance = new ScopedInstance<Disposable>
{
Value = disposable,
};
// Act
scopedInstance.Dispose();
// Assert
Assert.True(disposable.IsDisposed);
}
[Fact]
public void ScopedInstanceDoesNotThrowOnNonIDisposable()
{
// Arrange
var scopedInstance = new ScopedInstance<object>()
{
Value = new object(),
};
// Act
scopedInstance.Dispose();
}
[Fact]
public void ScopedInstanceDoesNotThrowOnNull()
{
// Arrange
var scopedInstance = new ScopedInstance<Disposable>()
{
Value = null, // just making it explicit that there is not value set yet.
};
// Act
scopedInstance.Dispose();
// Assert
Assert.Null(scopedInstance.Value);
}
private class Disposable : IDisposable
{
public bool IsDisposed { get; set; }
public void Dispose()
{
IsDisposed = true;
}
}
}
}