Removed dependency from user repository

This commit is contained in:
Luke Pulverenti 2013-08-09 20:46:34 -04:00
parent 9f0405b9a0
commit 6efd877017
4 changed files with 31 additions and 38 deletions

View File

@ -14,7 +14,7 @@ namespace MediaBrowser.Controller.Persistence
/// Opens the connection to the repository
/// </summary>
/// <returns>Task.</returns>
Task Initialize();
void Initialize();
/// <summary>
/// Deletes the user.

View File

@ -97,16 +97,17 @@ namespace MediaBrowser.Server.Implementations.Library
/// Gets the active user repository
/// </summary>
/// <value>The user repository.</value>
public IUserRepository UserRepository { get; set; }
private IUserRepository UserRepository { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="UserManager" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="configurationManager">The configuration manager.</param>
public UserManager(ILogger logger, IServerConfigurationManager configurationManager)
public UserManager(ILogger logger, IServerConfigurationManager configurationManager, IUserRepository userRepository)
{
_logger = logger;
UserRepository = userRepository;
ConfigurationManager = configurationManager;
}

View File

@ -6,8 +6,6 @@ using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
@ -19,11 +17,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
public class SqliteUserRepository : IUserRepository
{
private readonly ILogger _logger;
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
private SQLiteConnection _connection;
private IDbConnection _connection;
/// <summary>
/// Gets the name of the repository
/// </summary>
@ -50,11 +48,12 @@ namespace MediaBrowser.Server.Implementations.Persistence
/// <summary>
/// Initializes a new instance of the <see cref="SqliteUserRepository" /> class.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="appPaths">The app paths.</param>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logManager">The log manager.</param>
/// <exception cref="System.ArgumentNullException">appPaths</exception>
public SqliteUserRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
public SqliteUserRepository(IDbConnection connection, IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
{
if (appPaths == null)
{
@ -65,6 +64,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
throw new ArgumentNullException("jsonSerializer");
}
_connection = connection;
_appPaths = appPaths;
_jsonSerializer = jsonSerializer;
@ -75,12 +75,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public async Task Initialize()
public void Initialize()
{
var dbFile = Path.Combine(_appPaths.DataPath, "users.db");
_connection = await SqliteExtensions.ConnectToDb(dbFile).ConfigureAwait(false);
string[] queries = {
"create table if not exists users (guid GUID primary key, data BLOB)",
@ -120,7 +116,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
SQLiteTransaction transaction = null;
IDbTransaction transaction = null;
try
{
@ -129,12 +125,12 @@ namespace MediaBrowser.Server.Implementations.Persistence
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = "replace into users (guid, data) values (@1, @2)";
cmd.AddParam("@1", user.Id);
cmd.AddParam("@2", serialized);
cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = user.Id;
cmd.Parameters.Add(cmd, "@2", DbType.Binary).Value = serialized;
cmd.Transaction = transaction;
await cmd.ExecuteNonQueryAsync(cancellationToken);
cmd.ExecuteNonQuery();
}
transaction.Commit();
@ -217,7 +213,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
SQLiteTransaction transaction = null;
IDbTransaction transaction = null;
try
{
@ -227,12 +223,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
cmd.CommandText = "delete from users where guid=@guid";
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
guidParam.Value = user.Id;
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id;
cmd.Transaction = transaction;
await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
cmd.ExecuteNonQuery();
}
transaction.Commit();

View File

@ -247,7 +247,7 @@ namespace MediaBrowser.ServerApplication
UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
RegisterSingleInstance(UserDataRepository);
UserRepository = new SqliteUserRepository(ApplicationPaths, JsonSerializer, LogManager);
UserRepository = await GetUserRepository().ConfigureAwait(false);
RegisterSingleInstance(UserRepository);
DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
@ -256,7 +256,7 @@ namespace MediaBrowser.ServerApplication
ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
RegisterSingleInstance(ItemRepository);
UserManager = new UserManager(Logger, ServerConfigurationManager);
UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository);
RegisterSingleInstance(UserManager);
LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataRepository, () => DirectoryWatchers);
@ -288,11 +288,10 @@ namespace MediaBrowser.ServerApplication
var displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false));
var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false));
var userdataTask = Task.Run(async () => await ConfigureUserDataRepositories().ConfigureAwait(false));
var userTask = Task.Run(async () => await ConfigureUserRepositories().ConfigureAwait(false));
await ConfigureNotificationsRepository().ConfigureAwait(false);
await Task.WhenAll(itemsTask, userTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);
await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);
SetKernelProperties();
}
@ -312,6 +311,15 @@ namespace MediaBrowser.ServerApplication
);
}
private async Task<IUserRepository> GetUserRepository()
{
var dbFile = Path.Combine(ApplicationPaths.DataPath, "users.db");
var connection = await ConnectToDb(dbFile).ConfigureAwait(false);
return new SqliteUserRepository(connection, ApplicationPaths, JsonSerializer, LogManager);
}
/// <summary>
/// Configures the repositories.
/// </summary>
@ -358,18 +366,7 @@ namespace MediaBrowser.ServerApplication
private Task ConfigureUserDataRepositories()
{
return UserDataRepository.Initialize();
}
/// <summary>
/// Configures the user repositories.
/// </summary>
/// <returns>Task.</returns>
private async Task ConfigureUserRepositories()
{
await UserRepository.Initialize().ConfigureAwait(false);
((UserManager)UserManager).UserRepository = UserRepository;
}
}
/// <summary>
/// Connects to db.