mirror of
https://github.com/Radarr/Radarr.git
synced 2024-09-11 12:02:35 +02:00
cleanup
This commit is contained in:
parent
3592cf530d
commit
57e78e31fe
@ -51,7 +51,10 @@ public void Register(Type serviceType, Type implementationType)
|
||||
|
||||
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>()
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using NzbDrone.Common.Messaging;
|
||||
@ -32,9 +33,10 @@ protected ContainerBuilderBase(params string[] assemblies)
|
||||
private void AutoRegisterInterfaces()
|
||||
{
|
||||
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))
|
||||
.Where(c => !c.FullName.StartsWith("System"))
|
||||
.Except(new List<Type> { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName);
|
||||
|
||||
foreach (var contract in contracts)
|
||||
@ -50,11 +52,6 @@ protected void AutoRegisterImplementations<TContract>()
|
||||
|
||||
private void AutoRegisterImplementations(Type contractType)
|
||||
{
|
||||
if (contractType.Name.Contains("oots"))
|
||||
{
|
||||
int adawd = 12;
|
||||
}
|
||||
|
||||
var implementations = GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
|
||||
|
||||
|
||||
@ -67,6 +64,9 @@ private void AutoRegisterImplementations(Type contractType)
|
||||
{
|
||||
var impl = implementations.Single();
|
||||
|
||||
Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, impl.Name));
|
||||
|
||||
|
||||
if (impl.HasAttribute<SingletonAttribute>())
|
||||
{
|
||||
Container.RegisterSingleton(contractType, impl);
|
||||
@ -78,6 +78,8 @@ private void AutoRegisterImplementations(Type contractType)
|
||||
}
|
||||
else
|
||||
{
|
||||
Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, implementations.Count));
|
||||
|
||||
Container.RegisterAll(contractType, implementations);
|
||||
}
|
||||
}
|
||||
|
@ -20,20 +20,22 @@ public MessageAggregator(Logger logger, IServiceFactory serviceFactory)
|
||||
|
||||
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.
|
||||
foreach (var handler in _serviceFactory.BuildAll<IHandle<TEvent>>())
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.Debug("{0} -> {1}", @event.GetType().Name, handler.GetType().Name);
|
||||
_logger.Debug("{0} -> {1}", eventName, handler.GetType().Name);
|
||||
handler.Handle(@event);
|
||||
_logger.Debug("{0} <- {1}", @event.GetType().Name, handler.GetType().Name);
|
||||
_logger.Debug("{0} <- {1}", eventName, handler.GetType().Name);
|
||||
}
|
||||
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 @@ public void PublishEvent<TEvent>(TEvent @event) where TEvent : IEvent
|
||||
var handlerLocal = handler;
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
_logger.Debug("{0} ~> {1}", @event.GetType().Name, handlerLocal.GetType().Name);
|
||||
_logger.Debug("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
|
||||
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
|
||||
{
|
||||
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
||||
@ -59,7 +72,7 @@ public void PublishCommand<TCommand>(TCommand command) where TCommand : ICommand
|
||||
var handler = _serviceFactory.Build(handlerContract);
|
||||
|
||||
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
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()
|
||||
{
|
||||
private readonly IDatabase _database;
|
||||
private readonly IMessageAggregator _messageAggregator;
|
||||
|
||||
//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)
|
||||
{
|
||||
_database = database;
|
||||
_messageAggregator = messageAggregator;
|
||||
_dataMapper = database.DataMapper;
|
||||
}
|
||||
|
||||
protected QueryBuilder<TModel> Query
|
||||
{
|
||||
get { return _dataMapper.Query<TModel>(); }
|
||||
get { return DataMapper.Query<TModel>(); }
|
||||
}
|
||||
|
||||
protected void Delete(Expression<Func<TModel, bool>> filter)
|
||||
{
|
||||
_dataMapper.Delete(filter);
|
||||
DataMapper.Delete(filter);
|
||||
}
|
||||
|
||||
public IEnumerable<TModel> All()
|
||||
{
|
||||
return _dataMapper.Query<TModel>().ToList();
|
||||
return DataMapper.Query<TModel>().ToList();
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return _dataMapper.Query<TModel>().GetRowCount();
|
||||
return DataMapper.Query<TModel>().GetRowCount();
|
||||
}
|
||||
|
||||
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)
|
||||
@ -101,7 +105,7 @@ public TModel Insert(TModel model)
|
||||
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));
|
||||
|
||||
return model;
|
||||
@ -114,13 +118,13 @@ public TModel Update(TModel model)
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
@ -157,7 +161,7 @@ public TModel Upsert(TModel model)
|
||||
|
||||
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)
|
||||
@ -167,7 +171,7 @@ public void DeleteMany(IEnumerable<int> ids)
|
||||
|
||||
public void Purge()
|
||||
{
|
||||
_dataMapper.Delete<TModel>(c => c.Id > -1);
|
||||
DataMapper.Delete<TModel>(c => c.Id > -1);
|
||||
}
|
||||
|
||||
public bool HasItems()
|
||||
@ -182,7 +186,7 @@ public void SetFields(TModel model, params Expression<Func<TModel, object>>[] pr
|
||||
throw new InvalidOperationException("Attempted to updated model without ID");
|
||||
}
|
||||
|
||||
_dataMapper.Update<TModel>()
|
||||
DataMapper.Update<TModel>()
|
||||
.Where(c => c.Id == model.Id)
|
||||
.ColumnsIncluding(properties)
|
||||
.Entity(model)
|
||||
|
@ -10,12 +10,19 @@ public interface 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 Marr.Data;
|
||||
using Marr.Data.Reflection;
|
||||
using NzbDrone.Common.Composition;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
|
||||
@ -12,6 +13,7 @@ public interface IDbFactory
|
||||
IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main);
|
||||
}
|
||||
|
||||
[Singleton]
|
||||
public class DbFactory : IDbFactory
|
||||
{
|
||||
private readonly IMigrationController _migrationController;
|
||||
@ -31,14 +33,19 @@ public IDatabase Create(string dbPath, MigrationType migrationType = MigrationTy
|
||||
var connectionString = GetConnectionString(dbPath);
|
||||
|
||||
_migrationController.MigrateToLatest(connectionString, migrationType);
|
||||
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
||||
{
|
||||
SqlMode = SqlModes.Text,
|
||||
};
|
||||
|
||||
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user