cleanup
This commit is contained in:
parent
3592cf530d
commit
57e78e31fe
|
@ -51,7 +51,10 @@ namespace NzbDrone.Common.Composition
|
||||||
|
|
||||||
public void Register<TService>(Func<IContainer, TService> factory) where TService : class
|
public void Register<TService>(Func<IContainer, TService> factory) where TService : class
|
||||||
{
|
{
|
||||||
_container.Register((c, n) => factory(this));
|
_container.Register((c, n) =>
|
||||||
|
{
|
||||||
|
return factory(this);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterSingleton<TService, TImplementation>()
|
public void RegisterSingleton<TService, TImplementation>()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using NzbDrone.Common.Messaging;
|
using NzbDrone.Common.Messaging;
|
||||||
|
@ -32,9 +33,10 @@ namespace NzbDrone.Common.Composition
|
||||||
private void AutoRegisterInterfaces()
|
private void AutoRegisterInterfaces()
|
||||||
{
|
{
|
||||||
var loadedInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
|
var loadedInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
|
||||||
var implementedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces()).Where(i => !i.Assembly.FullName.StartsWith("System")).ToList();
|
var implementedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces());
|
||||||
|
|
||||||
var contracts = loadedInterfaces.Union(implementedInterfaces).Where(c => !c.IsGenericTypeDefinition && !string.IsNullOrWhiteSpace(c.FullName))
|
var contracts = loadedInterfaces.Union(implementedInterfaces).Where(c => !c.IsGenericTypeDefinition && !string.IsNullOrWhiteSpace(c.FullName))
|
||||||
|
.Where(c => !c.FullName.StartsWith("System"))
|
||||||
.Except(new List<Type> { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName);
|
.Except(new List<Type> { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName);
|
||||||
|
|
||||||
foreach (var contract in contracts)
|
foreach (var contract in contracts)
|
||||||
|
@ -50,11 +52,6 @@ namespace NzbDrone.Common.Composition
|
||||||
|
|
||||||
private void AutoRegisterImplementations(Type contractType)
|
private void AutoRegisterImplementations(Type contractType)
|
||||||
{
|
{
|
||||||
if (contractType.Name.Contains("oots"))
|
|
||||||
{
|
|
||||||
int adawd = 12;
|
|
||||||
}
|
|
||||||
|
|
||||||
var implementations = GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
|
var implementations = GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
|
||||||
|
|
||||||
|
|
||||||
|
@ -67,6 +64,9 @@ namespace NzbDrone.Common.Composition
|
||||||
{
|
{
|
||||||
var impl = implementations.Single();
|
var impl = implementations.Single();
|
||||||
|
|
||||||
|
Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, impl.Name));
|
||||||
|
|
||||||
|
|
||||||
if (impl.HasAttribute<SingletonAttribute>())
|
if (impl.HasAttribute<SingletonAttribute>())
|
||||||
{
|
{
|
||||||
Container.RegisterSingleton(contractType, impl);
|
Container.RegisterSingleton(contractType, impl);
|
||||||
|
@ -78,6 +78,8 @@ namespace NzbDrone.Common.Composition
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, implementations.Count));
|
||||||
|
|
||||||
Container.RegisterAll(contractType, implementations);
|
Container.RegisterAll(contractType, implementations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,20 +20,22 @@ namespace NzbDrone.Common.Messaging
|
||||||
|
|
||||||
public void PublishEvent<TEvent>(TEvent @event) where TEvent : IEvent
|
public void PublishEvent<TEvent>(TEvent @event) where TEvent : IEvent
|
||||||
{
|
{
|
||||||
_logger.Trace("Publishing {0}", @event.GetType().Name);
|
var eventName = GetEventName(@event.GetType());
|
||||||
|
|
||||||
|
_logger.Trace("Publishing {0}", eventName);
|
||||||
|
|
||||||
//call synchronous handlers first.
|
//call synchronous handlers first.
|
||||||
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.Debug("{0} -> {1}", @event.GetType().Name, handler.GetType().Name);
|
_logger.Debug("{0} -> {1}", eventName, handler.GetType().Name);
|
||||||
handler.Handle(@event);
|
handler.Handle(@event);
|
||||||
_logger.Debug("{0} <- {1}", @event.GetType().Name, handler.GetType().Name);
|
_logger.Debug("{0} <- {1}", eventName, handler.GetType().Name);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, @event.GetType().Name), e);
|
_logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,14 +44,25 @@ namespace NzbDrone.Common.Messaging
|
||||||
var handlerLocal = handler;
|
var handlerLocal = handler;
|
||||||
Task.Factory.StartNew(() =>
|
Task.Factory.StartNew(() =>
|
||||||
{
|
{
|
||||||
_logger.Debug("{0} ~> {1}", @event.GetType().Name, handlerLocal.GetType().Name);
|
_logger.Debug("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
||||||
handlerLocal.HandleAsync(@event);
|
handlerLocal.HandleAsync(@event);
|
||||||
_logger.Debug("{0} <~ {1}", @event.GetType().Name, handlerLocal.GetType().Name);
|
_logger.Debug("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static string GetEventName(Type eventType)
|
||||||
|
{
|
||||||
|
if (!eventType.IsGenericType)
|
||||||
|
{
|
||||||
|
return eventType.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Format("{0}<{1}>", eventType.Name.Remove(eventType.Name.IndexOf('`')), eventType.GetGenericArguments()[0].Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void PublishCommand<TCommand>(TCommand command) where TCommand : ICommand
|
public void PublishCommand<TCommand>(TCommand command) where TCommand : ICommand
|
||||||
{
|
{
|
||||||
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
||||||
|
@ -59,7 +72,7 @@ namespace NzbDrone.Common.Messaging
|
||||||
var handler = _serviceFactory.Build(handlerContract);
|
var handler = _serviceFactory.Build(handlerContract);
|
||||||
|
|
||||||
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
handlerContract.GetMethod("Execute").Invoke(handler, new object[] { command });
|
handlerContract.GetMethod("Execute").Invoke(handler, new object[] { command });
|
||||||
|
|
|
@ -34,42 +34,46 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
|
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
|
||||||
{
|
{
|
||||||
|
private readonly IDatabase _database;
|
||||||
private readonly IMessageAggregator _messageAggregator;
|
private readonly IMessageAggregator _messageAggregator;
|
||||||
|
|
||||||
//TODO: add assertion to make sure model properly mapped
|
//TODO: add assertion to make sure model properly mapped
|
||||||
|
|
||||||
|
|
||||||
private readonly IDataMapper _dataMapper;
|
private IDataMapper DataMapper
|
||||||
|
{
|
||||||
|
get { return _database.DataMapper; }
|
||||||
|
}
|
||||||
|
|
||||||
public BasicRepository(IDatabase database, IMessageAggregator messageAggregator)
|
public BasicRepository(IDatabase database, IMessageAggregator messageAggregator)
|
||||||
{
|
{
|
||||||
|
_database = database;
|
||||||
_messageAggregator = messageAggregator;
|
_messageAggregator = messageAggregator;
|
||||||
_dataMapper = database.DataMapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected QueryBuilder<TModel> Query
|
protected QueryBuilder<TModel> Query
|
||||||
{
|
{
|
||||||
get { return _dataMapper.Query<TModel>(); }
|
get { return DataMapper.Query<TModel>(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Delete(Expression<Func<TModel, bool>> filter)
|
protected void Delete(Expression<Func<TModel, bool>> filter)
|
||||||
{
|
{
|
||||||
_dataMapper.Delete(filter);
|
DataMapper.Delete(filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TModel> All()
|
public IEnumerable<TModel> All()
|
||||||
{
|
{
|
||||||
return _dataMapper.Query<TModel>().ToList();
|
return DataMapper.Query<TModel>().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Count()
|
public int Count()
|
||||||
{
|
{
|
||||||
return _dataMapper.Query<TModel>().GetRowCount();
|
return DataMapper.Query<TModel>().GetRowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TModel Get(int id)
|
public TModel Get(int id)
|
||||||
{
|
{
|
||||||
return _dataMapper.Query<TModel>().Single(c => c.Id == id);
|
return DataMapper.Query<TModel>().Single(c => c.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TModel> Get(IEnumerable<int> ids)
|
public IEnumerable<TModel> Get(IEnumerable<int> ids)
|
||||||
|
@ -101,7 +105,7 @@ namespace NzbDrone.Core.Datastore
|
||||||
throw new InvalidOperationException("Can't insert model with existing ID");
|
throw new InvalidOperationException("Can't insert model with existing ID");
|
||||||
}
|
}
|
||||||
|
|
||||||
_dataMapper.Insert(model);
|
DataMapper.Insert(model);
|
||||||
_messageAggregator.PublishEvent(new ModelEvent<TModel>(model, ModelEvent<TModel>.RepositoryAction.Created));
|
_messageAggregator.PublishEvent(new ModelEvent<TModel>(model, ModelEvent<TModel>.RepositoryAction.Created));
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
|
@ -114,13 +118,13 @@ namespace NzbDrone.Core.Datastore
|
||||||
throw new InvalidOperationException("Can't update model with ID 0");
|
throw new InvalidOperationException("Can't update model with ID 0");
|
||||||
}
|
}
|
||||||
|
|
||||||
_dataMapper.Update(model, c => c.Id == model.Id);
|
DataMapper.Update(model, c => c.Id == model.Id);
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Delete(TModel model)
|
public void Delete(TModel model)
|
||||||
{
|
{
|
||||||
_dataMapper.Delete<TModel>(c => c.Id == model.Id);
|
DataMapper.Delete<TModel>(c => c.Id == model.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InsertMany(IList<TModel> models)
|
public void InsertMany(IList<TModel> models)
|
||||||
|
@ -157,7 +161,7 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
public void Delete(int id)
|
public void Delete(int id)
|
||||||
{
|
{
|
||||||
_dataMapper.Delete<TModel>(c => c.Id == id);
|
DataMapper.Delete<TModel>(c => c.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteMany(IEnumerable<int> ids)
|
public void DeleteMany(IEnumerable<int> ids)
|
||||||
|
@ -167,7 +171,7 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
public void Purge()
|
public void Purge()
|
||||||
{
|
{
|
||||||
_dataMapper.Delete<TModel>(c => c.Id > -1);
|
DataMapper.Delete<TModel>(c => c.Id > -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasItems()
|
public bool HasItems()
|
||||||
|
@ -182,7 +186,7 @@ namespace NzbDrone.Core.Datastore
|
||||||
throw new InvalidOperationException("Attempted to updated model without ID");
|
throw new InvalidOperationException("Attempted to updated model without ID");
|
||||||
}
|
}
|
||||||
|
|
||||||
_dataMapper.Update<TModel>()
|
DataMapper.Update<TModel>()
|
||||||
.Where(c => c.Id == model.Id)
|
.Where(c => c.Id == model.Id)
|
||||||
.ColumnsIncluding(properties)
|
.ColumnsIncluding(properties)
|
||||||
.Entity(model)
|
.Entity(model)
|
||||||
|
|
|
@ -10,12 +10,19 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
public class Database : IDatabase
|
public class Database : IDatabase
|
||||||
{
|
{
|
||||||
|
private readonly Func<IDataMapper> _dataMapperFactory;
|
||||||
|
|
||||||
public Database(IDataMapper dataMapper)
|
public Database(Func<IDataMapper> dataMapperFactory)
|
||||||
{
|
{
|
||||||
DataMapper = dataMapper;
|
_dataMapperFactory = dataMapperFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDataMapper DataMapper { get; private set; }
|
public IDataMapper DataMapper
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _dataMapperFactory();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
using Marr.Data;
|
using Marr.Data;
|
||||||
using Marr.Data.Reflection;
|
using Marr.Data.Reflection;
|
||||||
|
using NzbDrone.Common.Composition;
|
||||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ namespace NzbDrone.Core.Datastore
|
||||||
IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main);
|
IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Singleton]
|
||||||
public class DbFactory : IDbFactory
|
public class DbFactory : IDbFactory
|
||||||
{
|
{
|
||||||
private readonly IMigrationController _migrationController;
|
private readonly IMigrationController _migrationController;
|
||||||
|
@ -31,14 +33,19 @@ namespace NzbDrone.Core.Datastore
|
||||||
var connectionString = GetConnectionString(dbPath);
|
var connectionString = GetConnectionString(dbPath);
|
||||||
|
|
||||||
_migrationController.MigrateToLatest(connectionString, migrationType);
|
_migrationController.MigrateToLatest(connectionString, migrationType);
|
||||||
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
|
||||||
{
|
|
||||||
SqlMode = SqlModes.Text,
|
|
||||||
};
|
|
||||||
|
|
||||||
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
|
MapRepository.Instance.ReflectionStrategy = new SimpleReflectionStrategy();
|
||||||
|
|
||||||
return new Database(dataMapper);
|
return new Database(() =>
|
||||||
|
{
|
||||||
|
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
||||||
|
{
|
||||||
|
SqlMode = SqlModes.Text,
|
||||||
|
};
|
||||||
|
|
||||||
|
return dataMapper;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetConnectionString(string dbPath)
|
private string GetConnectionString(string dbPath)
|
||||||
|
|
Loading…
Reference in New Issue