-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid using ActivatorServices for common DbContext constructor (#21254)
Improvement to #18575 This change checks if the DbContext has the usual constructor taking just options: ```C# MyContext(DbContextOptions<MyContext> options) ``` If it does, then we create a delegate (once in a singleton service) and use that to create context instances. This avoids using ActivatorServices and falling out of constructor injection (other than the delegate) for the common case.
- Loading branch information
1 parent
d814eba
commit 63d84c9
Showing
5 changed files
with
121 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) .NET Foundation. 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 System.Linq.Expressions; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public class DbContextFactorySource<TContext> : IDbContextFactorySource<TContext> | ||
where TContext : DbContext | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public DbContextFactorySource() | ||
=> Factory = CreateActivator(); | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual Func<IServiceProvider, DbContextOptions<TContext>, TContext> Factory { get; } | ||
|
||
private static Func<IServiceProvider, DbContextOptions<TContext>, TContext> CreateActivator() | ||
{ | ||
var constructors | ||
= typeof(TContext).GetTypeInfo().DeclaredConstructors | ||
.Where(c => !c.IsStatic && c.IsPublic) | ||
.ToArray(); | ||
|
||
if (constructors.Length == 1) | ||
{ | ||
var parameters = constructors[0].GetParameters(); | ||
|
||
if (parameters.Length == 1) | ||
{ | ||
var isGeneric = parameters[0].ParameterType == typeof(DbContextOptions<TContext>); | ||
if (isGeneric | ||
|| parameters[0].ParameterType == typeof(DbContextOptions)) | ||
{ | ||
var optionsParam = Expression.Parameter(typeof(DbContextOptions<TContext>), "options"); | ||
var providerParam = Expression.Parameter(typeof(IServiceProvider), "provider"); | ||
|
||
return Expression.Lambda<Func<IServiceProvider, DbContextOptions<TContext>, TContext>>( | ||
Expression.New( | ||
constructors[0], | ||
isGeneric | ||
? optionsParam | ||
: (Expression)Expression.Convert(optionsParam, typeof(DbContextOptions))), | ||
providerParam, optionsParam) | ||
.Compile(); | ||
} | ||
} | ||
} | ||
|
||
return (p, _) => ActivatorUtilities.CreateInstance<TContext>(p); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) .NET Foundation. 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.EntityFrameworkCore.Internal | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public interface IDbContextFactorySource<TContext> | ||
where TContext : DbContext | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
Func<IServiceProvider, DbContextOptions<TContext>, TContext> Factory { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters