Removed core services.

This commit is contained in:
David Fowler 2014-01-22 19:35:13 -08:00
parent f3008e8ff9
commit fb35288326
7 changed files with 0 additions and 401 deletions

View File

@ -1,106 +0,0 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Microsoft.AspNet.CoreServices
{
/// <summary>
/// Helper code for the various activator services.
/// </summary>
public static class ActivatorUtilities
{
/// <summary>
/// Retrieve an instance of the given type from the service provider. If one is not found then instantiate it directly.
/// </summary>
/// <param name="services"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object GetServiceOrCreateInstance(IServiceProvider services, Type type)
{
return GetServiceNoExceptions(services, type) ?? CreateInstance(services, type);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "IServiceProvider may throw unknown exceptions")]
private static object GetServiceNoExceptions(IServiceProvider services, Type type)
{
try
{
return services.GetService(type);
}
catch
{
return null;
}
}
/// <summary>
/// Instantiate an object of the given type, using constructor service injection if possible.
/// </summary>
/// <param name="services"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object CreateInstance(IServiceProvider services, Type type)
{
return CreateFactory(type).Invoke(services);
}
/// <summary>
/// Creates a factory to instantiate a type using constructor service injection if possible.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static Func<IServiceProvider, object> CreateFactory(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
ConstructorInfo[] constructors = type.GetTypeInfo()
.DeclaredConstructors
.Where(IsInjectable)
.ToArray();
if (constructors.Length == 1)
{
ParameterInfo[] parameters = constructors[0].GetParameters();
return services =>
{
var args = new object[parameters.Length];
for (int index = 0; index != parameters.Length; ++index)
{
args[index] = services.GetService(parameters[index].ParameterType);
}
return Activator.CreateInstance(type, args);
};
}
return _ => Activator.CreateInstance(type);
}
private static bool IsInjectable(ConstructorInfo constructor)
{
return constructor.IsPublic && constructor.GetParameters().Length != 0;
}
public static Func<TBase> Create<TBase>(Type instanceType) where TBase : class
{
Contract.Assert(instanceType != null);
NewExpression newInstanceExpression = Expression.New(instanceType);
return Expression.Lambda<Func<TBase>>(newInstanceExpression).Compile();
}
public static Func<TInstance> Create<TInstance>() where TInstance : class
{
return Create<TInstance>(typeof(TInstance));
}
public static Func<object> Create(Type instanceType)
{
Contract.Assert(instanceType != null);
return Create<object>(instanceType);
}
}
}

View File

@ -1,59 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EC38534C-A2D1-413F-97D1-55EEF5D2FB71}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Microsoft.AspNet.CoreServices</RootNamespace>
<AssemblyName>Microsoft.AspNet.CoreServices</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Owin.FileSystems">
<HintPath>..\..\packages\Microsoft.Owin.FileSystems.2.1.0-rc1\lib\net40\Microsoft.Owin.FileSystems.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ActivatorUtilities.cs" />
<Compile Include="ServiceProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServiceProviderExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="project.json" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -1,36 +0,0 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Microsoft.AspNet.CoreServices")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Microsoft.AspNet.CoreServices")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("96e86a39-7c32-4257-af76-d12e449dc25a")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,164 +0,0 @@
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNet.CoreServices
{
/// <summary>
/// The default IServiceProvider.
/// </summary>
public class ServiceProvider : IServiceProvider
{
private readonly IDictionary<Type, Func<object>> _services = new Dictionary<Type, Func<object>>();
private readonly IDictionary<Type, List<Func<object>>> _priorServices = new Dictionary<Type, List<Func<object>>>();
/// <summary>
///
/// </summary>
public ServiceProvider()
{
_services[typeof(IServiceProvider)] = () => this;
}
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
public virtual object GetService(Type serviceType)
{
return GetSingleService(serviceType) ?? GetMultiService(serviceType);
}
private object GetSingleService(Type serviceType)
{
Func<object> serviceFactory;
return _services.TryGetValue(serviceType, out serviceFactory)
? serviceFactory.Invoke()
: null;
}
private object GetMultiService(Type collectionType)
{
if (collectionType.GetTypeInfo().IsGenericType &&
collectionType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
Type serviceType = collectionType.GetTypeInfo().GenericTypeArguments.Single();
Type listType = typeof(List<>).MakeGenericType(serviceType);
var services = (IList)Activator.CreateInstance(listType);
Func<object> serviceFactory;
if (_services.TryGetValue(serviceType, out serviceFactory))
{
services.Add(serviceFactory());
List<Func<object>> prior;
if (_priorServices.TryGetValue(serviceType, out prior))
{
foreach (var factory in prior)
{
services.Add(factory());
}
}
}
return services;
}
return null;
}
/// <summary>
/// Remove all occurrences of the given type from the provider.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public virtual ServiceProvider RemoveAll<T>()
{
return RemoveAll(typeof(T));
}
/// <summary>
/// Remove all occurrences of the given type from the provider.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public virtual ServiceProvider RemoveAll(Type type)
{
_services.Remove(type);
_priorServices.Remove(type);
return this;
}
/// <summary>
/// Add an instance of type TService to the list of providers.
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="instance"></param>
/// <returns></returns>
public virtual ServiceProvider AddInstance<TService>(object instance)
{
return AddInstance(typeof(TService), instance);
}
/// <summary>
/// Add an instance of the given type to the list of providers.
/// </summary>
/// <param name="service"></param>
/// <param name="instance"></param>
/// <returns></returns>
public virtual ServiceProvider AddInstance(Type service, object instance)
{
return Add(service, () => instance);
}
/// <summary>
/// Specify that services of the type TService should be fulfilled by the type TImplementation.
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <returns></returns>
public virtual ServiceProvider Add<TService, TImplementation>()
{
return Add(typeof(TService), typeof(TImplementation));
}
/// <summary>
/// Specify that services of the type serviceType should be fulfilled by the type implementationType.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="implementationType"></param>
/// <returns></returns>
public virtual ServiceProvider Add(Type serviceType, Type implementationType)
{
Func<IServiceProvider, object> factory = ActivatorUtilities.CreateFactory(implementationType);
return Add(serviceType, () => factory(this));
}
/// <summary>
/// Specify that services of the given type should be created with the given serviceFactory.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="serviceFactory"></param>
/// <returns></returns>
public virtual ServiceProvider Add(Type serviceType, Func<object> serviceFactory)
{
Func<object> existing;
if (_services.TryGetValue(serviceType, out existing))
{
List<Func<object>> prior;
if (_priorServices.TryGetValue(serviceType, out prior))
{
prior.Add(existing);
}
else
{
prior = new List<Func<object>> { existing };
_priorServices.Add(serviceType, prior);
}
}
_services[serviceType] = serviceFactory;
return this;
}
}
}

View File

@ -1,23 +0,0 @@
using System;
namespace Microsoft.AspNet.CoreServices
{
public static class ServiceProviderExtensions
{
/// <summary>
/// Retrieve a service of type T from the IServiceProvider.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="services"></param>
/// <returns></returns>
public static T GetService<T>(this IServiceProvider services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
return (T)services.GetService(typeof(T));
}
}
}

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Owin.FileSystems" version="2.1.0-rc1" targetFramework="net45" />
</packages>

View File

@ -1,9 +0,0 @@
{
"dependencies": {
"Microsoft.Owin.FileSystems": "2.0.2"
},
"configurations": {
"net45": {},
"k10": {}
}
}