Skip to content

Commit 78e88f9

Browse files
ODataReadOnlySupport (#7218)
Add readOnlySqlReplica support for OData controllers.
1 parent a2059de commit 78e88f9

25 files changed

Lines changed: 460 additions & 114 deletions

src/NuGetGallery.Core/Entities/EntitiesContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public EntitiesContext(string connectionString, bool readOnly)
5353
public EntitiesContext(DbConnection connection, bool readOnly)
5454
: base(connection, contextOwnsConnection: true)
5555
{
56-
ReadOnly = readOnly;
56+
ReadOnly = readOnly;
5757
}
5858

5959
public bool ReadOnly { get; private set; }
@@ -73,7 +73,7 @@ public EntitiesContext(DbConnection connection, bool readOnly)
7373
/// </summary>
7474
public DbSet<User> Users { get; set; }
7575

76-
DbSet<T> IEntitiesContext.Set<T>()
76+
DbSet<T> IReadOnlyEntitiesContext.Set<T>()
7777
{
7878
return base.Set<T>();
7979
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Data.Entity;
65
using System.Threading.Tasks;
76
using NuGet.Services.Entities;
87

98
namespace NuGetGallery
109
{
11-
public interface IEntitiesContext : IDisposable
10+
public interface IEntitiesContext : IReadOnlyEntitiesContext
1211
{
1312
DbSet<Certificate> Certificates { get; set; }
14-
DbSet<Package> Packages { get; set; }
1513
DbSet<PackageDeprecation> Deprecations { get; set; }
1614
DbSet<PackageRegistration> PackageRegistrations { get; set; }
1715
DbSet<Credential> Credentials { get; set; }
@@ -23,9 +21,7 @@ public interface IEntitiesContext : IDisposable
2321
DbSet<SymbolPackage> SymbolPackages { get; set; }
2422

2523
Task<int> SaveChangesAsync();
26-
DbSet<T> Set<T>() where T : class;
2724
void DeleteOnCommit<T>(T entity) where T : class;
28-
void SetCommandTimeout(int? seconds);
2925
IDatabase GetDatabase();
3026
}
3127
}

src/NuGetGallery.Core/Entities/IEntityRepository.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Threading.Tasks;
76

87
namespace NuGetGallery
98
{
10-
public interface IEntityRepository<T>
9+
public interface IEntityRepository<T> : IReadOnlyEntityRepository<T>
1110
where T : class, new()
1211
{
1312
Task CommitChangesAsync();
1413
void InsertOnCommit(T entity);
1514
void InsertOnCommit(IEnumerable<T> entities);
1615
void DeleteOnCommit(T entity);
1716
void DeleteOnCommit(IEnumerable<T> entities);
18-
IQueryable<T> GetAll();
1917
}
2018
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Data.Entity;
6+
using NuGet.Services.Entities;
7+
8+
namespace NuGetGallery
9+
{
10+
public interface IReadOnlyEntitiesContext : IDisposable
11+
{
12+
DbSet<Package> Packages { get; set; }
13+
14+
DbSet<T> Set<T>() where T : class;
15+
16+
void SetCommandTimeout(int? seconds);
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Linq;
5+
6+
namespace NuGetGallery
7+
{
8+
public interface IReadOnlyEntityRepository<T>
9+
where T : class, new()
10+
{
11+
IQueryable<T> GetAll();
12+
}
13+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Data.Common;
6+
using System.Data.Entity;
7+
using System.Linq;
8+
using NuGet.Services.Entities;
9+
10+
namespace NuGetGallery
11+
{
12+
public class ReadOnlyEntitiesContext : IReadOnlyEntitiesContext
13+
{
14+
private readonly EntitiesContext _entitiesContext;
15+
16+
public ReadOnlyEntitiesContext(DbConnection connection)
17+
{
18+
_entitiesContext = new EntitiesContext(connection, readOnly: true);
19+
}
20+
21+
public DbSet<Package> Packages
22+
{
23+
get
24+
{
25+
return _entitiesContext.Packages;
26+
}
27+
set
28+
{
29+
_entitiesContext.Packages = value;
30+
}
31+
}
32+
33+
DbSet<T> IReadOnlyEntitiesContext.Set<T>()
34+
{
35+
return _entitiesContext.Set<T>();
36+
}
37+
38+
public void SetCommandTimeout(int? seconds)
39+
{
40+
_entitiesContext.SetCommandTimeout(seconds);
41+
}
42+
43+
public void Dispose()
44+
{
45+
_entitiesContext.Dispose();
46+
}
47+
}
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Linq;
5+
6+
namespace NuGetGallery
7+
{
8+
public class ReadOnlyEntityRepository<T> : IReadOnlyEntityRepository<T>
9+
where T : class, new()
10+
{
11+
private readonly IReadOnlyEntitiesContext _readOnlyEntitiesContext;
12+
13+
public ReadOnlyEntityRepository(IReadOnlyEntitiesContext readOnlyEntitiesContext)
14+
{
15+
_readOnlyEntitiesContext = readOnlyEntitiesContext;
16+
}
17+
18+
public IQueryable<T> GetAll()
19+
{
20+
return _readOnlyEntitiesContext.Set<T>();
21+
}
22+
}
23+
}

src/NuGetGallery.Core/IAppConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public interface IAppConfiguration : IMessageServiceConfiguration
170170
/// </summary>
171171
string SqlConnectionString { get; set; }
172172

173+
/// <summary>
174+
/// Gets the SQL Connection string used to connect to the read only replica of the database
175+
/// </summary>
176+
string SqlReadOnlyReplicaConnectionString { get; set; }
177+
173178
/// <summary>
174179
/// Gets the SQL Connection string used to connect to the database for support requests
175180
/// </summary>

src/NuGetGallery.Core/NuGetGallery.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
<Compile Include="DisposableAction.cs" />
120120
<Compile Include="Entities\DatabaseWrapper.cs" />
121121
<Compile Include="Entities\DbContextTransactionWrapper.cs" />
122+
<Compile Include="Entities\ReadOnlyEntitiesContext.cs" />
123+
<Compile Include="Entities\IReadOnlyEntitiesContext.cs" />
124+
<Compile Include="Entities\ReadOnlyEntityRepository.cs" />
122125
<Compile Include="Entities\IDatabase.cs" />
123126
<Compile Include="Entities\IDbContextTransaction.cs" />
124127
<Compile Include="Entities\EntitiesConfiguration.cs" />
@@ -130,6 +133,7 @@
130133
<Compile Include="Entities\IEntitiesContext.cs" />
131134
<Compile Include="Entities\Interception\IObjectMaterializedInterceptor.cs" />
132135
<Compile Include="Entities\IEntityRepository.cs" />
136+
<Compile Include="Entities\IReadOnlyEntityRepository.cs" />
133137
<Compile Include="Entities\ReadOnlyModeException.cs" />
134138
<Compile Include="Entities\SuspendDbExecutionStrategy.cs" />
135139
<Compile Include="Extensions\DiagnosticsSourceExtensions.cs" />

src/NuGetGallery.Services/Configuration/AppConfiguration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ public class AppConfiguration : IAppConfiguration
149149
[DefaultValue("NuGetGallery")]
150150
public string SqlConnectionString { get; set; }
151151

152+
/// <summary>
153+
/// Gets the SQL Connection string used to connect to the database read only replica.
154+
/// </summary>
155+
[DisplayName("SqlServerReadOnlyReplica")]
156+
[DefaultValue(null)]
157+
public string SqlReadOnlyReplicaConnectionString { get; set; }
158+
152159
/// <summary>
153160
/// Gets the SQL Connection string used to connect to the database for support requests
154161
/// </summary>

0 commit comments

Comments
 (0)