Skip to content

Commit

Permalink
API review cleanup
Browse files Browse the repository at this point in the history
Mostly making classes internal.
Also:
 - Changing ModelValidator base class to IModelValidator interface
 - Make AddDbContext not an extension method
 - Move ModelSourceHelpers helper method to ModelSource
  • Loading branch information
ajcvickers committed Mar 11, 2015
1 parent cb1a8f4 commit 37e0d9e
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 273 deletions.
19 changes: 9 additions & 10 deletions src/EntityFramework.Core/EntityFramework.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,27 @@
<Compile Include="Infrastructure\IDatabaseFactory.cs" />
<Compile Include="Infrastructure\IDbContextOptions.cs" />
<Compile Include="Infrastructure\IOptionsBuilderExtender.cs" />
<Compile Include="Infrastructure\LoggingModelValidator.cs" />
<Compile Include="Infrastructure\ModelValidator.cs" />
<Compile Include="Infrastructure\EntityFrameworkServicesBuilder.cs" />
<Compile Include="Infrastructure\ModelValidatorBase.cs" />
<Compile Include="Infrastructure\IAccessor.cs" />
<Compile Include="Internal\DbContextActivator.cs" />
<Compile Include="Internal\DbContextOptionsParser.cs" />
<Compile Include="Internal\DbContextServices.cs" />
<Compile Include="Internal\DbSetFinder.cs" />
<Compile Include="Internal\DbSetInitializer.cs" />
<Compile Include="Internal\DbSetSource.cs" />
<Compile Include="Internal\IModelValidator.cs" />
<Compile Include="Internal\LoggingModelValidator.cs" />
<Compile Include="Internal\ModelSource.cs" />
<Compile Include="Internal\ModelValidator.cs" />
<Compile Include="Metadata\IModelBuilderFactory.cs" />
<Compile Include="ModelBuilder.cs" />
<Compile Include="Query\EntityLoadInfo.cs" />
<Compile Include="Query\ExpressionTreeVisitors\ExpressionStringBuilder.cs" />
<Compile Include="Query\ExpressionTreeVisitors\ExpressionTreeVisitorBase.cs" />
<Compile Include="Query\IIncludableQueryable.cs" />
<Compile Include="Infrastructure\DbContextActivator.cs" />
<Compile Include="DbSet`.cs" />
<Compile Include="EntityState.cs" />
<Compile Include="Extensions\EntityFrameworkTaskExtensions.cs" />
<Compile Include="Infrastructure\DbContextOptionsParser.cs" />
<Compile Include="Infrastructure\ModelSourceHelpers.cs" />
<Compile Include="Internal\InternalDbSet.cs" />
<Compile Include="Metadata\BasicModelBuilder.cs" />
<Compile Include="Metadata\KeyExtensions.cs" />
Expand Down Expand Up @@ -233,10 +236,6 @@
<Compile Include="Extensions\EntityFrameworkServiceCollectionExtensions.cs" />
<Compile Include="Extensions\EntityFrameworkQueryableExtensions.cs" />
<Compile Include="Infrastructure\Database.cs" />
<Compile Include="Infrastructure\DbSetFinder.cs" />
<Compile Include="Infrastructure\DbSetInitializer.cs" />
<Compile Include="Infrastructure\DbSetSource.cs" />
<Compile Include="Infrastructure\ModelSource.cs" />
<Compile Include="Infrastructure\IDbContextOptionsExtension.cs" />
<Compile Include="Infrastructure\IModelSource.cs" />
<Compile Include="Infrastructure\Annotation.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using JetBrains.Annotations;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.ChangeTracking;
using Microsoft.Data.Entity.ChangeTracking.Internal;
using Microsoft.Data.Entity.Infrastructure;
Expand Down Expand Up @@ -42,12 +41,12 @@ public static class EntityFrameworkServiceCollectionExtensions
/// <para>
/// The data store you are using will also define extension methods that can be called on the returned
/// <see cref="EntityFrameworkServicesBuilder" /> to register the services for the data store. For example,
/// when using EntityFramework.SqlServer you would call
/// when using EntityFramework.SqlServer you would call
/// <c>collection.AddEntityFramework(config).UseSqlServer()</c>.
/// </para>
/// <para>
/// For derived contexts to resolve their services from the <see cref="IServiceProvider" /> you must chain a call
/// to the <see cref="AddDbContext{TContext}" /> extension method on the returned
/// to the <see cref="EntityFrameworkServicesBuilder.AddDbContext{TContext}" /> method on the returned
/// <see cref="EntityFrameworkServicesBuilder" />.
/// This will ensure services are resolved from the <see cref="IServiceProvider" /> and any Entity Framework
/// configuration from the supplied <paramref name="configuration" /> will be honored.
Expand All @@ -59,7 +58,7 @@ public static class EntityFrameworkServiceCollectionExtensions
/// The configuration being used for the current application. Providing this allows configuration under the
/// 'entityFramework' node to be applied to contexts that are resolved from the <see cref="IServiceProvider" />.
/// For this configuration to be applied you must register any derived contexts using the
/// <see cref="AddDbContext{TContext}" /> extension method on the returned
/// <see cref="EntityFrameworkServicesBuilder.AddDbContext{TContext}" /> method on the returned
/// <see cref="EntityFrameworkServicesBuilder" />.
/// </para>
/// </param>
Expand Down Expand Up @@ -90,7 +89,7 @@ public static EntityFrameworkServicesBuilder AddEntityFramework(
.AddSingleton<ClrCollectionAccessorSource>()
.AddSingleton<CollectionTypeFactory>()
.AddSingleton<EntityMaterializerSource>()
.AddSingleton<ModelValidator, LoggingModelValidator>()
.AddSingleton<IModelValidator, LoggingModelValidator>()
.AddSingleton<MemberMapper>()
.AddSingleton<FieldMatcher>()
.AddSingleton<OriginalValuesFactory>()
Expand Down Expand Up @@ -128,57 +127,5 @@ public static EntityFrameworkServicesBuilder AddEntityFramework(

return new EntityFrameworkServicesBuilder(serviceCollection, configuration);
}

/// <summary>
/// Registers the given context as a service in the <see cref="IServiceCollection" />.
/// You use this method when using dependency injection in your application, such as with ASP.NET.
/// For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
/// </summary>
/// <remarks>
/// This method will ensure services that the context uses are resolved from the
/// <see cref="IServiceProvider" /> and any Entity Framework configuration
/// found in the configuration passed to <see cref="AddEntityFramework" /> will be honored.
/// </remarks>
/// <typeparam name="TContext"> The type of context to be registered. </typeparam>
/// <param name="builder"> The builder returned from <see cref="AddEntityFramework" />. </param>
/// <param name="optionsAction">
/// <para>
/// An optional action to configure the <see cref="DbContextOptions" /> for the context. This provides an
/// alternative to performing configuration of the context by overriding the
/// <see cref="DbContext.OnConfiguring" /> method in your derived context.
/// </para>
/// <para>
/// If an action is supplied here, the <see cref="DbContext.OnConfiguring" /> method will still be run if it has
/// been overridden on the derived context. <see cref="DbContext.OnConfiguring" /> configuration will be applied
/// in addition to configuration performed here.
/// </para>
/// <para>
/// You do not need to expose a constructor parameter for the <see cref="DbContextOptions" /> to be passed to the
/// context. If you choose to expose a constructor parameter, you must type it as the generic
/// <see cref="DbContextOptions{T}" /> as that is the type that will be registered in the
/// <see cref="IServiceCollection" /> (in order to support multiple context types being registered in the
/// same <see cref="IServiceCollection" />).
/// </para>
/// </param>
/// <returns>
/// A builder that allows further Entity Framework specific setup of the <see cref="IServiceCollection" />.
/// </returns>
public static EntityFrameworkServicesBuilder AddDbContext<TContext>(
[NotNull] this EntityFrameworkServicesBuilder builder,
[CanBeNull] Action<DbContextOptionsBuilder> optionsAction = null)
where TContext : DbContext
{
Check.NotNull(builder, nameof(builder));

var serviceCollection = ((IAccessor<IServiceCollection>)builder).Service;
var configuration = ((IAccessor<IConfiguration>)builder).Service;

serviceCollection.AddSingleton(p => DbContextOptionsParser.DbContextOptionsFactory<TContext>(p, configuration, optionsAction));
serviceCollection.AddSingleton<DbContextOptions>(p => p.GetRequiredService<DbContextOptions<TContext>>());

serviceCollection.AddScoped(typeof(TContext), DbContextActivator.CreateInstance<TContext>);

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// 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 JetBrains.Annotations;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
Expand Down Expand Up @@ -37,6 +39,51 @@ public EntityFrameworkServicesBuilder(
_configuration = configuration;
}

/// <summary>
/// Registers the given context as a service in the <see cref="IServiceCollection" />.
/// You use this method when using dependency injection in your application, such as with ASP.NET.
/// For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
/// </summary>
/// <remarks>
/// This method will ensure services that the context uses are resolved from the
/// <see cref="IServiceProvider" /> and any Entity Framework configuration
/// found in the configuration passed to <see cref="EntityFrameworkServiceCollectionExtensions.AddEntityFramework" />
/// extension method will be honored.
/// </remarks>
/// <typeparam name="TContext"> The type of context to be registered. </typeparam>
/// <param name="optionsAction">
/// <para>
/// An optional action to configure the <see cref="DbContextOptions" /> for the context. This provides an
/// alternative to performing configuration of the context by overriding the
/// <see cref="DbContext.OnConfiguring" /> method in your derived context.
/// </para>
/// <para>
/// If an action is supplied here, the <see cref="DbContext.OnConfiguring" /> method will still be run if it has
/// been overridden on the derived context. <see cref="DbContext.OnConfiguring" /> configuration will be applied
/// in addition to configuration performed here.
/// </para>
/// <para>
/// You do not need to expose a constructor parameter for the <see cref="DbContextOptions" /> to be passed to the
/// context. If you choose to expose a constructor parameter, you must type it as the generic
/// <see cref="DbContextOptions{T}" /> as that is the type that will be registered in the
/// <see cref="IServiceCollection" /> (in order to support multiple context types being registered in the
/// same <see cref="IServiceCollection" />).
/// </para>
/// </param>
/// <returns>
/// A builder that allows further Entity Framework specific setup of the <see cref="IServiceCollection" />.
/// </returns>
public virtual EntityFrameworkServicesBuilder AddDbContext<TContext>([CanBeNull] Action<DbContextOptionsBuilder> optionsAction = null)
where TContext : DbContext
{
_serviceCollection.AddSingleton(p => DbContextOptionsParser.DbContextOptionsFactory<TContext>(p, _configuration, optionsAction));
_serviceCollection.AddSingleton<DbContextOptions>(p => p.GetRequiredService<DbContextOptions<TContext>>());

_serviceCollection.AddScoped(typeof(TContext), DbContextActivator.CreateInstance<TContext>);

return this;
}

/// <summary>
/// Gets the <see cref="IServiceCollection" /> being configured.
/// </summary>
Expand Down
19 changes: 0 additions & 19 deletions src/EntityFramework.Core/Infrastructure/ModelSourceHelpers.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

using System;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.DependencyInjection;

namespace Microsoft.Data.Entity.Infrastructure
namespace Microsoft.Data.Entity.Internal
{
public static class DbContextActivator
{
Expand All @@ -22,8 +21,6 @@ public static IServiceProvider ServiceProvider

public static TContext CreateInstance<TContext>([NotNull] IServiceProvider serviceProvider)
{
Check.NotNull(serviceProvider, nameof(serviceProvider));

try
{
_serviceProvider = serviceProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;

namespace Microsoft.Data.Entity.Infrastructure
namespace Microsoft.Data.Entity.Internal
{
public class DbContextOptionsParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@
using JetBrains.Annotations;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.Infrastructure
namespace Microsoft.Data.Entity.Internal
{
public class DbSetFinder
{
private readonly ThreadSafeDictionaryCache<Type, IReadOnlyList<DbSetProperty>> _cache
= new ThreadSafeDictionaryCache<Type, IReadOnlyList<DbSetProperty>>();

public virtual IReadOnlyList<DbSetProperty> FindSets([NotNull] DbContext context)
{
Check.NotNull(context, nameof(context));

return _cache.GetOrAdd(context.GetType(), FindSets);
}
public virtual IReadOnlyList<DbSetProperty> FindSets([NotNull] DbContext context) => _cache.GetOrAdd(context.GetType(), FindSets);

private static DbSetProperty[] FindSets(Type contextType)
{
Expand All @@ -42,11 +37,6 @@ public struct DbSetProperty
{
public DbSetProperty([NotNull] Type contextType, [NotNull] string name, [NotNull] Type entityType, bool hasSetter)
{
Check.NotNull(contextType, nameof(contextType));
Check.NotNull(name, nameof(name));
Check.NotNull(entityType, nameof(entityType));
Check.ValidEntityType(entityType, "entityType");

ContextType = contextType;
Name = name;
EntityType = entityType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
// 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 System.Linq;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.Infrastructure
namespace Microsoft.Data.Entity.Internal
{
public class DbSetInitializer
{
private readonly DbSetFinder _setFinder;
private readonly ClrPropertySetterSource _setSetters;
private readonly DbSetSource _setSource;

/// <summary>
/// This constructor is intended only for use when creating test doubles that will override members
/// with mocked or faked behavior. Use of this constructor for other purposes may result in unexpected
/// behavior including but not limited to throwing <see cref="NullReferenceException" />.
/// </summary>
protected DbSetInitializer()
{
}

public DbSetInitializer(
[NotNull] DbSetFinder setFinder,
[NotNull] ClrPropertySetterSource setSetters,
[NotNull] DbSetSource setSource)
{
Check.NotNull(setFinder, nameof(setFinder));
Check.NotNull(setSetters, nameof(setSetters));
Check.NotNull(setSource, nameof(setSource));

_setFinder = setFinder;
_setSetters = setSetters;
_setSource = setSource;
}

public virtual void InitializeSets([NotNull] DbContext context)
{
Check.NotNull(context, nameof(context));

foreach (var setInfo in _setFinder.FindSets(context).Where(p => p.HasSetter))
{
_setSetters
Expand All @@ -50,12 +33,7 @@ public virtual void InitializeSets([NotNull] DbContext context)
}
}

public virtual DbSet<TEntity> CreateSet<TEntity>([NotNull] DbContext context)
where TEntity : class
{
Check.NotNull(context, nameof(context));

return (DbSet<TEntity>)_setSource.Create(context, typeof(TEntity));
}
public virtual DbSet<TEntity> CreateSet<TEntity>([NotNull] DbContext context) where TEntity : class
=> (DbSet<TEntity>)_setSource.Create(context, typeof(TEntity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.Infrastructure
namespace Microsoft.Data.Entity.Internal
{
public class DbSetSource
{
Expand All @@ -19,24 +18,13 @@ private static readonly MethodInfo _genericCreate
private readonly ThreadSafeDictionaryCache<Type, Func<DbContext, object>> _cache
= new ThreadSafeDictionaryCache<Type, Func<DbContext, object>>();

public virtual object Create(
[NotNull] DbContext context,
[NotNull] Type type)
{
Check.NotNull(context, nameof(context));
Check.NotNull(type, nameof(type));

var factory = _cache.GetOrAdd(
public virtual object Create([NotNull] DbContext context, [NotNull] Type type)
=> _cache.GetOrAdd(
type,
t => (Func<DbContext, object>)_genericCreate.MakeGenericMethod(type).Invoke(null, null));

return factory(context);
}
t => (Func<DbContext, object>)_genericCreate.MakeGenericMethod(type).Invoke(null, null))(context);

[UsedImplicitly]
private static Func<DbContext, object> CreateConstructor<TEntity>() where TEntity : class
{
return c => new InternalDbSet<TEntity>(c);
}
=> c => new InternalDbSet<TEntity>(c);
}
}
Loading

0 comments on commit 37e0d9e

Please sign in to comment.