Welcome home ScopedInstance
This commit is contained in:
parent
c1a026cbf7
commit
f3a9ce1ec1
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,16 +15,14 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
{
|
||||
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(
|
||||
[NotNull] this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
public static IServiceCollection AddMvc([NotNull] this IServiceCollection services)
|
||||
{
|
||||
ConfigureDefaultServices(services, configuration);
|
||||
ConfigureDefaultServices(services);
|
||||
services.TryAdd(MvcServices.GetDefaultServices());
|
||||
return services;
|
||||
}
|
||||
|
|
@ -90,12 +88,12 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
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.AddDataProtection();
|
||||
services.AddRouting();
|
||||
services.AddAuthorization(configuration);
|
||||
services.AddAuthorization();
|
||||
services.AddWebEncoders();
|
||||
services.Configure<RouteOptions>(
|
||||
routeOptions => routeOptions.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint)));
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue