-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql project global usings and file scoped namespaces #249
- Loading branch information
Showing
41 changed files
with
2,628 additions
and
2,928 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using PopForums.Configuration; | ||
using PopForums.Services; | ||
namespace PopForums.Sql; | ||
|
||
namespace PopForums.Sql | ||
public class CacheHelper : ICacheHelper | ||
{ | ||
public class CacheHelper : ICacheHelper | ||
public CacheHelper(IConfig config, ITenantService tenantService) | ||
{ | ||
public CacheHelper(IConfig config, ITenantService tenantService) | ||
_config = config; | ||
_tenantService = tenantService; | ||
if (_cache == null) | ||
{ | ||
_config = config; | ||
_tenantService = tenantService; | ||
if (_cache == null) | ||
{ | ||
var options = new MemoryCacheOptions(); | ||
_cache = new MemoryCache(options); | ||
} | ||
var options = new MemoryCacheOptions(); | ||
_cache = new MemoryCache(options); | ||
} | ||
} | ||
|
||
private readonly IConfig _config; | ||
private readonly ITenantService _tenantService; | ||
private static IMemoryCache _cache; | ||
private readonly IConfig _config; | ||
private readonly ITenantService _tenantService; | ||
private static IMemoryCache _cache; | ||
|
||
private string PrefixTenantOnKey(string key) | ||
{ | ||
var tenantID = _tenantService.GetTenant(); | ||
return $"{tenantID}:{key}"; | ||
} | ||
private string PrefixTenantOnKey(string key) | ||
{ | ||
var tenantID = _tenantService.GetTenant(); | ||
return $"{tenantID}:{key}"; | ||
} | ||
|
||
public void SetCacheObject(string key, object value) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_config.CacheSeconds) }; | ||
_cache.Set(key, value, options); | ||
} | ||
public void SetCacheObject(string key, object value) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_config.CacheSeconds) }; | ||
_cache.Set(key, value, options); | ||
} | ||
|
||
public void SetCacheObject(string key, object value, double seconds) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(seconds) }; | ||
_cache.Set(key, value, options); | ||
} | ||
public void SetCacheObject(string key, object value, double seconds) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(seconds) }; | ||
_cache.Set(key, value, options); | ||
} | ||
|
||
public void SetLongTermCacheObject(string key, object value) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var options = new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(60) }; | ||
_cache.Set(key, value, options); | ||
} | ||
public void SetLongTermCacheObject(string key, object value) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var options = new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(60) }; | ||
_cache.Set(key, value, options); | ||
} | ||
|
||
public void SetPagedListCacheObject<T>(string rootKey, int page, List<T> value) | ||
{ | ||
rootKey = PrefixTenantOnKey(rootKey); | ||
_cache.TryGetValue(rootKey, out Dictionary<int, List<T>> rootPages); | ||
if (rootPages == null) | ||
rootPages = new Dictionary<int, List<T>>(); | ||
else if (rootPages.ContainsKey(page)) | ||
rootPages.Remove(page); | ||
rootPages.Add(page, value); | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_config.CacheSeconds) }; | ||
_cache.Set(rootKey, rootPages, options); | ||
} | ||
public void SetPagedListCacheObject<T>(string rootKey, int page, List<T> value) | ||
{ | ||
rootKey = PrefixTenantOnKey(rootKey); | ||
_cache.TryGetValue(rootKey, out Dictionary<int, List<T>> rootPages); | ||
if (rootPages == null) | ||
rootPages = new Dictionary<int, List<T>>(); | ||
else if (rootPages.ContainsKey(page)) | ||
rootPages.Remove(page); | ||
rootPages.Add(page, value); | ||
var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_config.CacheSeconds) }; | ||
_cache.Set(rootKey, rootPages, options); | ||
} | ||
|
||
public void RemoveCacheObject(string key) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
_cache.Remove(key); | ||
} | ||
public void RemoveCacheObject(string key) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
_cache.Remove(key); | ||
} | ||
|
||
public T GetCacheObject<T>(string key) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var cacheObject = _cache.Get(key); | ||
return cacheObject != null ? (T)cacheObject : default; | ||
} | ||
public T GetCacheObject<T>(string key) | ||
{ | ||
key = PrefixTenantOnKey(key); | ||
var cacheObject = _cache.Get(key); | ||
return cacheObject != null ? (T)cacheObject : default; | ||
} | ||
|
||
public List<T> GetPagedListCacheObject<T>(string rootKey, int page) | ||
{ | ||
rootKey = PrefixTenantOnKey(rootKey); | ||
_cache.TryGetValue(rootKey, out Dictionary<int, List<T>> rootPages); | ||
if (rootPages == null) | ||
return null; | ||
if (rootPages.ContainsKey(page)) | ||
return rootPages[page]; | ||
public List<T> GetPagedListCacheObject<T>(string rootKey, int page) | ||
{ | ||
rootKey = PrefixTenantOnKey(rootKey); | ||
_cache.TryGetValue(rootKey, out Dictionary<int, List<T>> rootPages); | ||
if (rootPages == null) | ||
return null; | ||
} | ||
if (rootPages.ContainsKey(page)) | ||
return rootPages[page]; | ||
return null; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,132 +1,122 @@ | ||
using System; | ||
using System.Data; | ||
using System.Data.Common; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using PopForums.Configuration; | ||
using PopForums.Sql.Repositories; | ||
using PopForums.Repositories; | ||
namespace PopForums.Sql; | ||
|
||
namespace PopForums.Sql | ||
public static class Extensions | ||
{ | ||
public static class Extensions | ||
public static DbCommand Command(this DbConnection connection, ISqlObjectFactory sqlObjectFactory, string sql) | ||
{ | ||
public static DbCommand Command(this DbConnection connection, ISqlObjectFactory sqlObjectFactory, string sql) | ||
{ | ||
var command = sqlObjectFactory.GetCommand(sql, connection); | ||
return command; | ||
} | ||
var command = sqlObjectFactory.GetCommand(sql, connection); | ||
return command; | ||
} | ||
|
||
public static DbCommand AddParameter(this DbCommand command, ISqlObjectFactory sqlObjectFactory, string parameterName, object value) | ||
{ | ||
var parameter = sqlObjectFactory.GetParameter(parameterName, value); | ||
command.Parameters.Add(parameter); | ||
return command; | ||
} | ||
public static DbCommand AddParameter(this DbCommand command, ISqlObjectFactory sqlObjectFactory, string parameterName, object value) | ||
{ | ||
var parameter = sqlObjectFactory.GetParameter(parameterName, value); | ||
command.Parameters.Add(parameter); | ||
return command; | ||
} | ||
|
||
public static void Using(this DbConnection connection, Action<DbConnection> action) | ||
public static void Using(this DbConnection connection, Action<DbConnection> action) | ||
{ | ||
using (connection) | ||
{ | ||
using (connection) | ||
try | ||
{ | ||
connection.Open(); | ||
action(connection); | ||
} | ||
finally | ||
{ | ||
try | ||
{ | ||
connection.Open(); | ||
action(connection); | ||
} | ||
finally | ||
{ | ||
connection.Close(); | ||
connection.Dispose(); | ||
} | ||
connection.Close(); | ||
connection.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
public static async Task UsingAsync(this DbConnection connection, Func<DbConnection, Task> action) | ||
public static async Task UsingAsync(this DbConnection connection, Func<DbConnection, Task> action) | ||
{ | ||
using (connection) | ||
{ | ||
using (connection) | ||
try | ||
{ | ||
try | ||
{ | ||
await connection.OpenAsync(); | ||
await action(connection); | ||
} | ||
finally | ||
{ | ||
connection.Close(); | ||
connection.Dispose(); | ||
} | ||
await connection.OpenAsync(); | ||
await action(connection); | ||
} | ||
finally | ||
{ | ||
connection.Close(); | ||
connection.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
public static void AddPopForumsSql(this IServiceCollection services) | ||
{ | ||
services.AddTransient<ICacheHelper, CacheHelper>(); | ||
services.AddTransient<ISqlObjectFactory, SqlObjectFactory>(); | ||
services.AddTransient<IAwardCalculationQueueRepository, AwardCalculationQueueRepository>(); | ||
services.AddTransient<IAwardConditionRepository, AwardConditionRepository>(); | ||
services.AddTransient<IAwardDefinitionRepository, AwardDefinitionRepository>(); | ||
services.AddTransient<IBanRepository, BanRepository>(); | ||
services.AddTransient<ICategoryRepository, CategoryRepository>(); | ||
services.AddTransient<IEmailQueueRepository, EmailQueueRepository>(); | ||
services.AddTransient<IErrorLogRepository, ErrorLogRepository>(); | ||
services.AddTransient<IEventDefinitionRepository, EventDefinitionRepository>(); | ||
services.AddTransient<IExternalUserAssociationRepository, ExternalUserAssociationRepository>(); | ||
services.AddTransient<IFavoriteTopicsRepository, FavoriteTopicsRepository>(); | ||
services.AddTransient<IFeedRepository, FeedRepository>(); | ||
services.AddTransient<IForumRepository, ForumRepository>(); | ||
services.AddTransient<IFriendRepository, FriendRepository>(); | ||
services.AddTransient<ILastReadRepository, LastReadRepository>(); | ||
services.AddTransient<IModerationLogRepository, ModerationLogRepository>(); | ||
services.AddTransient<IPointLedgerRepository, PointLedgerRepository>(); | ||
services.AddTransient<IPostRepository, PostRepository>(); | ||
services.AddTransient<IPrivateMessageRepository, PrivateMessageRepository>(); | ||
services.AddTransient<IProfileRepository, ProfileRepository>(); | ||
services.AddTransient<IQueuedEmailMessageRepository, QueuedEmailMessageRepository>(); | ||
services.AddTransient<IRoleRepository, RoleRepository>(); | ||
services.AddTransient<ISearchIndexQueueRepository, SearchIndexQueueRepository>(); | ||
services.AddTransient<ISearchRepository, SearchRepository>(); | ||
services.AddTransient<ISecurityLogRepository, SecurityLogRepository>(); | ||
services.AddTransient<ISettingsRepository, SettingsRepository>(); | ||
services.AddTransient<ISetupRepository, SetupRepository>(); | ||
services.AddTransient<ISubscribedTopicsRepository, SubscribedTopicsRepository>(); | ||
services.AddTransient<ITopicRepository, TopicRepository>(); | ||
services.AddTransient<ITopicViewLogRepository, TopicViewLogRepository>(); | ||
services.AddTransient<IUserAvatarRepository, UserAvatarRepository>(); | ||
services.AddTransient<IUserAwardRepository, UserAwardRepository>(); | ||
services.AddTransient<IUserImageRepository, UserImageRepository>(); | ||
services.AddTransient<IUserRepository, UserRepository>(); | ||
services.AddTransient<IUserSessionRepository, UserSessionRepository>(); | ||
services.AddTransient<IServiceHeartbeatRepository, ServiceHeartbeatRepository>(); | ||
} | ||
public static void AddPopForumsSql(this IServiceCollection services) | ||
{ | ||
services.AddTransient<ICacheHelper, CacheHelper>(); | ||
services.AddTransient<ISqlObjectFactory, SqlObjectFactory>(); | ||
services.AddTransient<IAwardCalculationQueueRepository, AwardCalculationQueueRepository>(); | ||
services.AddTransient<IAwardConditionRepository, AwardConditionRepository>(); | ||
services.AddTransient<IAwardDefinitionRepository, AwardDefinitionRepository>(); | ||
services.AddTransient<IBanRepository, BanRepository>(); | ||
services.AddTransient<ICategoryRepository, CategoryRepository>(); | ||
services.AddTransient<IEmailQueueRepository, EmailQueueRepository>(); | ||
services.AddTransient<IErrorLogRepository, ErrorLogRepository>(); | ||
services.AddTransient<IEventDefinitionRepository, EventDefinitionRepository>(); | ||
services.AddTransient<IExternalUserAssociationRepository, ExternalUserAssociationRepository>(); | ||
services.AddTransient<IFavoriteTopicsRepository, FavoriteTopicsRepository>(); | ||
services.AddTransient<IFeedRepository, FeedRepository>(); | ||
services.AddTransient<IForumRepository, ForumRepository>(); | ||
services.AddTransient<IFriendRepository, FriendRepository>(); | ||
services.AddTransient<ILastReadRepository, LastReadRepository>(); | ||
services.AddTransient<IModerationLogRepository, ModerationLogRepository>(); | ||
services.AddTransient<IPointLedgerRepository, PointLedgerRepository>(); | ||
services.AddTransient<IPostRepository, PostRepository>(); | ||
services.AddTransient<IPrivateMessageRepository, PrivateMessageRepository>(); | ||
services.AddTransient<IProfileRepository, ProfileRepository>(); | ||
services.AddTransient<IQueuedEmailMessageRepository, QueuedEmailMessageRepository>(); | ||
services.AddTransient<IRoleRepository, RoleRepository>(); | ||
services.AddTransient<ISearchIndexQueueRepository, SearchIndexQueueRepository>(); | ||
services.AddTransient<ISearchRepository, SearchRepository>(); | ||
services.AddTransient<ISecurityLogRepository, SecurityLogRepository>(); | ||
services.AddTransient<ISettingsRepository, SettingsRepository>(); | ||
services.AddTransient<ISetupRepository, SetupRepository>(); | ||
services.AddTransient<ISubscribedTopicsRepository, SubscribedTopicsRepository>(); | ||
services.AddTransient<ITopicRepository, TopicRepository>(); | ||
services.AddTransient<ITopicViewLogRepository, TopicViewLogRepository>(); | ||
services.AddTransient<IUserAvatarRepository, UserAvatarRepository>(); | ||
services.AddTransient<IUserAwardRepository, UserAwardRepository>(); | ||
services.AddTransient<IUserImageRepository, UserImageRepository>(); | ||
services.AddTransient<IUserRepository, UserRepository>(); | ||
services.AddTransient<IUserSessionRepository, UserSessionRepository>(); | ||
services.AddTransient<IServiceHeartbeatRepository, ServiceHeartbeatRepository>(); | ||
} | ||
|
||
public static object GetObjectOrDbNull(this object value) | ||
{ | ||
return value ?? DBNull.Value; | ||
} | ||
public static object GetObjectOrDbNull(this object value) | ||
{ | ||
return value ?? DBNull.Value; | ||
} | ||
|
||
public static int? NullIntDbHelper(this DbDataReader reader, int index) | ||
{ | ||
if (reader.IsDBNull(index)) return null; | ||
return reader.GetInt32(index); | ||
} | ||
public static int? NullIntDbHelper(this DbDataReader reader, int index) | ||
{ | ||
if (reader.IsDBNull(index)) return null; | ||
return reader.GetInt32(index); | ||
} | ||
|
||
public static string NullStringDbHelper(this DbDataReader reader, int index) | ||
{ | ||
if (reader.IsDBNull(index)) return null; | ||
return reader.GetString(index); | ||
} | ||
public static string NullStringDbHelper(this DbDataReader reader, int index) | ||
{ | ||
if (reader.IsDBNull(index)) return null; | ||
return reader.GetString(index); | ||
} | ||
|
||
public static Guid? NullGuidDbHelper(this IDataReader reader, int index) | ||
{ | ||
if (reader.IsDBNull(index)) return null; | ||
return reader.GetGuid(index); | ||
} | ||
public static Guid? NullGuidDbHelper(this IDataReader reader, int index) | ||
{ | ||
if (reader.IsDBNull(index)) return null; | ||
return reader.GetGuid(index); | ||
} | ||
|
||
public static string NullToEmpty(this string text) | ||
{ | ||
if (String.IsNullOrEmpty(text)) | ||
return String.Empty; | ||
return text; | ||
} | ||
public static string NullToEmpty(this string text) | ||
{ | ||
if (String.IsNullOrEmpty(text)) | ||
return String.Empty; | ||
return text; | ||
} | ||
} | ||
} |
Oops, something went wrong.