Removed dependency from user repository

pull/702/head
Luke Pulverenti 12 years ago
parent 9f0405b9a0
commit 6efd877017

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

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

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

@ -247,7 +247,7 @@ namespace MediaBrowser.ServerApplication
UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager); UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
RegisterSingleInstance(UserDataRepository); RegisterSingleInstance(UserDataRepository);
UserRepository = new SqliteUserRepository(ApplicationPaths, JsonSerializer, LogManager); UserRepository = await GetUserRepository().ConfigureAwait(false);
RegisterSingleInstance(UserRepository); RegisterSingleInstance(UserRepository);
DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager); DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
@ -256,7 +256,7 @@ namespace MediaBrowser.ServerApplication
ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager); ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
RegisterSingleInstance(ItemRepository); RegisterSingleInstance(ItemRepository);
UserManager = new UserManager(Logger, ServerConfigurationManager); UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository);
RegisterSingleInstance(UserManager); RegisterSingleInstance(UserManager);
LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataRepository, () => DirectoryWatchers); 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 displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false));
var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false)); var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false));
var userdataTask = Task.Run(async () => await ConfigureUserDataRepositories().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 ConfigureNotificationsRepository().ConfigureAwait(false);
await Task.WhenAll(itemsTask, userTask, displayPreferencesTask, userdataTask).ConfigureAwait(false); await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);
SetKernelProperties(); 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> /// <summary>
/// Configures the repositories. /// Configures the repositories.
/// </summary> /// </summary>
@ -358,18 +366,7 @@ namespace MediaBrowser.ServerApplication
private Task ConfigureUserDataRepositories() private Task ConfigureUserDataRepositories()
{ {
return UserDataRepository.Initialize(); 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> /// <summary>
/// Connects to db. /// Connects to db.

Loading…
Cancel
Save