From 57e78e31feec5ff68d4ca1e6ba02e7e94c7f057b Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 11 May 2013 13:06:57 -0700 Subject: [PATCH] cleanup --- NzbDrone.Common/Composition/Container.cs | 5 +++- .../Composition/ContainerBuilderBase.cs | 14 +++++---- .../Messaging/MessageAggregator.cs | 27 ++++++++++++----- NzbDrone.Core/Datastore/BasicRepository.cs | 30 +++++++++++-------- NzbDrone.Core/Datastore/Database.cs | 13 ++++++-- NzbDrone.Core/Datastore/DbFactory.cs | 17 +++++++---- 6 files changed, 71 insertions(+), 35 deletions(-) diff --git a/NzbDrone.Common/Composition/Container.cs b/NzbDrone.Common/Composition/Container.cs index cb460298f..9de414480 100644 --- a/NzbDrone.Common/Composition/Container.cs +++ b/NzbDrone.Common/Composition/Container.cs @@ -51,7 +51,10 @@ namespace NzbDrone.Common.Composition public void Register(Func factory) where TService : class { - _container.Register((c, n) => factory(this)); + _container.Register((c, n) => + { + return factory(this); + }); } public void RegisterSingleton() diff --git a/NzbDrone.Common/Composition/ContainerBuilderBase.cs b/NzbDrone.Common/Composition/ContainerBuilderBase.cs index c1e5814a7..f48cc677a 100644 --- a/NzbDrone.Common/Composition/ContainerBuilderBase.cs +++ b/NzbDrone.Common/Composition/ContainerBuilderBase.cs @@ -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 @@ namespace NzbDrone.Common.Composition 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 { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName); foreach (var contract in contracts) @@ -50,11 +52,6 @@ namespace NzbDrone.Common.Composition 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 @@ namespace NzbDrone.Common.Composition { var impl = implementations.Single(); + Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, impl.Name)); + + if (impl.HasAttribute()) { Container.RegisterSingleton(contractType, impl); @@ -78,6 +78,8 @@ namespace NzbDrone.Common.Composition } else { + Trace.WriteLine(string.Format("Registering {0} -> {1}", contractType.FullName, implementations.Count)); + Container.RegisterAll(contractType, implementations); } } diff --git a/NzbDrone.Common/Messaging/MessageAggregator.cs b/NzbDrone.Common/Messaging/MessageAggregator.cs index 3d1a17ab7..2fd60c672 100644 --- a/NzbDrone.Common/Messaging/MessageAggregator.cs +++ b/NzbDrone.Common/Messaging/MessageAggregator.cs @@ -20,20 +20,22 @@ namespace NzbDrone.Common.Messaging public void PublishEvent(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>()) { 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 @@ namespace NzbDrone.Common.Messaging 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 command) where TCommand : ICommand { var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType()); @@ -59,7 +72,7 @@ namespace NzbDrone.Common.Messaging var handler = _serviceFactory.Build(handlerContract); _logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name); - + try { handlerContract.GetMethod("Execute").Invoke(handler, new object[] { command }); diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 762903d64..a983e8f03 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -34,42 +34,46 @@ namespace NzbDrone.Core.Datastore public class BasicRepository : IBasicRepository 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 Query { - get { return _dataMapper.Query(); } + get { return DataMapper.Query(); } } protected void Delete(Expression> filter) { - _dataMapper.Delete(filter); + DataMapper.Delete(filter); } public IEnumerable All() { - return _dataMapper.Query().ToList(); + return DataMapper.Query().ToList(); } public int Count() { - return _dataMapper.Query().GetRowCount(); + return DataMapper.Query().GetRowCount(); } public TModel Get(int id) { - return _dataMapper.Query().Single(c => c.Id == id); + return DataMapper.Query().Single(c => c.Id == id); } public IEnumerable Get(IEnumerable ids) @@ -101,7 +105,7 @@ namespace NzbDrone.Core.Datastore throw new InvalidOperationException("Can't insert model with existing ID"); } - _dataMapper.Insert(model); + DataMapper.Insert(model); _messageAggregator.PublishEvent(new ModelEvent(model, ModelEvent.RepositoryAction.Created)); return model; @@ -114,13 +118,13 @@ namespace NzbDrone.Core.Datastore 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(c => c.Id == model.Id); + DataMapper.Delete(c => c.Id == model.Id); } public void InsertMany(IList models) @@ -157,7 +161,7 @@ namespace NzbDrone.Core.Datastore public void Delete(int id) { - _dataMapper.Delete(c => c.Id == id); + DataMapper.Delete(c => c.Id == id); } public void DeleteMany(IEnumerable ids) @@ -167,7 +171,7 @@ namespace NzbDrone.Core.Datastore public void Purge() { - _dataMapper.Delete(c => c.Id > -1); + DataMapper.Delete(c => c.Id > -1); } public bool HasItems() @@ -182,7 +186,7 @@ namespace NzbDrone.Core.Datastore throw new InvalidOperationException("Attempted to updated model without ID"); } - _dataMapper.Update() + DataMapper.Update() .Where(c => c.Id == model.Id) .ColumnsIncluding(properties) .Entity(model) diff --git a/NzbDrone.Core/Datastore/Database.cs b/NzbDrone.Core/Datastore/Database.cs index 2786600f1..963761bbb 100644 --- a/NzbDrone.Core/Datastore/Database.cs +++ b/NzbDrone.Core/Datastore/Database.cs @@ -10,12 +10,19 @@ namespace NzbDrone.Core.Datastore public class Database : IDatabase { + private readonly Func _dataMapperFactory; - public Database(IDataMapper dataMapper) + public Database(Func dataMapperFactory) { - DataMapper = dataMapper; + _dataMapperFactory = dataMapperFactory; } - public IDataMapper DataMapper { get; private set; } + public IDataMapper DataMapper + { + get + { + return _dataMapperFactory(); + } + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/DbFactory.cs b/NzbDrone.Core/Datastore/DbFactory.cs index f36e3c7ab..f66eaab83 100644 --- a/NzbDrone.Core/Datastore/DbFactory.cs +++ b/NzbDrone.Core/Datastore/DbFactory.cs @@ -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 @@ namespace NzbDrone.Core.Datastore IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main); } + [Singleton] public class DbFactory : IDbFactory { private readonly IMigrationController _migrationController; @@ -31,14 +33,19 @@ namespace NzbDrone.Core.Datastore 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)