-
Notifications
You must be signed in to change notification settings - Fork 546
/
RepositoryBase.cs
57 lines (48 loc) · 2.1 KB
/
RepositoryBase.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Internal
{
using System.Data.Common;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Data.Entity.Utilities;
internal abstract class RepositoryBase
{
private readonly DbConnection _existingConnection;
private readonly string _connectionString;
private readonly DbProviderFactory _providerFactory;
protected RepositoryBase(InternalContext usersContext, string connectionString, DbProviderFactory providerFactory)
{
DebugCheck.NotNull(usersContext);
DebugCheck.NotEmpty(connectionString);
DebugCheck.NotNull(providerFactory);
var existingConnection = usersContext.Connection;
if (existingConnection != null)
{
_existingConnection = existingConnection;
}
_connectionString = connectionString;
_providerFactory = providerFactory;
}
protected DbConnection CreateConnection()
{
if (_existingConnection != null
&& _existingConnection.State == ConnectionState.Open)
{
return _existingConnection;
}
var connection = _existingConnection == null
? _providerFactory.CreateConnection()
: DbProviderServices.GetProviderServices(_existingConnection).CloneDbConnection(_existingConnection, _providerFactory);
DbInterception.Dispatch.Connection.SetConnectionString(connection,
new DbConnectionPropertyInterceptionContext<string>().WithValue(_connectionString));
return connection;
}
protected void DisposeConnection(DbConnection connection)
{
if (connection != null && _existingConnection == null)
{
DbInterception.Dispatch.Connection.Dispose(connection, new DbInterceptionContext());
}
}
}
}