forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbSetSource.cs
More file actions
36 lines (31 loc) · 1.54 KB
/
Copy pathDbSetSource.cs
File metadata and controls
36 lines (31 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 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.Collections.Concurrent;
using System.Reflection;
using JetBrains.Annotations;
namespace Microsoft.EntityFrameworkCore.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class DbSetSource : IDbSetSource
{
private static readonly MethodInfo _genericCreate
= typeof(DbSetSource).GetTypeInfo().GetDeclaredMethod(nameof(CreateConstructor));
private readonly ConcurrentDictionary<Type, Func<DbContext, object>> _cache
= new ConcurrentDictionary<Type, Func<DbContext, object>>();
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual object Create(DbContext context, Type type)
=> _cache.GetOrAdd(
type,
t => (Func<DbContext, object>)_genericCreate.MakeGenericMethod(t).Invoke(null, null))(context);
[UsedImplicitly]
private static Func<DbContext, object> CreateConstructor<TEntity>() where TEntity : class
=> c => new InternalDbSet<TEntity>(c);
}
}