diff --git a/src/Umbraco.Infrastructure/Persistence/DbProviderFactoryCreator.cs b/src/Umbraco.Infrastructure/Persistence/DbProviderFactoryCreator.cs index 0bae3494ed..8814c01761 100644 --- a/src/Umbraco.Infrastructure/Persistence/DbProviderFactoryCreator.cs +++ b/src/Umbraco.Infrastructure/Persistence/DbProviderFactoryCreator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data.Common; using System.Linq; +using NPoco; using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax; namespace Umbraco.Cms.Infrastructure.Persistence @@ -12,17 +13,20 @@ namespace Umbraco.Cms.Infrastructure.Persistence private readonly IDictionary _embeddedDatabaseCreators; private readonly IDictionary _syntaxProviders; private readonly IDictionary _bulkSqlInsertProviders; + private readonly IDictionary _providerSpecificMapperFactories; public DbProviderFactoryCreator( Func getFactory, IEnumerable syntaxProviders, IEnumerable bulkSqlInsertProviders, - IEnumerable embeddedDatabaseCreators) + IEnumerable embeddedDatabaseCreators, + IEnumerable providerSpecificMapperFactories) { _getFactory = getFactory; _embeddedDatabaseCreators = embeddedDatabaseCreators.ToDictionary(x=>x.ProviderName); _syntaxProviders = syntaxProviders.ToDictionary(x=>x.ProviderName); _bulkSqlInsertProviders = bulkSqlInsertProviders.ToDictionary(x=>x.ProviderName); + _providerSpecificMapperFactories = providerSpecificMapperFactories.ToDictionary(x=>x.ProviderName); } public DbProviderFactory CreateFactory(string providerName) @@ -61,5 +65,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence creator.Create(); } } + + public NPocoMapperCollection ProviderSpecificMappers(string providerName) + { + if(_providerSpecificMapperFactories.TryGetValue(providerName, out var mapperFactory)) + { + return mapperFactory.Mappers; + } + + return new NPocoMapperCollection(Array.Empty()); + } } } diff --git a/src/Umbraco.Infrastructure/Persistence/IDbProviderFactoryCreator.cs b/src/Umbraco.Infrastructure/Persistence/IDbProviderFactoryCreator.cs index 47cede9b26..99b7469085 100644 --- a/src/Umbraco.Infrastructure/Persistence/IDbProviderFactoryCreator.cs +++ b/src/Umbraco.Infrastructure/Persistence/IDbProviderFactoryCreator.cs @@ -10,5 +10,6 @@ namespace Umbraco.Cms.Infrastructure.Persistence ISqlSyntaxProvider GetSqlSyntaxProvider(string providerName); IBulkSqlInsertProvider CreateBulkSqlInsertProvider(string providerName); void CreateDatabase(string providerName); + NPocoMapperCollection ProviderSpecificMappers(string providerName); } } diff --git a/src/Umbraco.Infrastructure/Persistence/IProviderSpecificMapperFactory.cs b/src/Umbraco.Infrastructure/Persistence/IProviderSpecificMapperFactory.cs new file mode 100644 index 0000000000..3a73d647e8 --- /dev/null +++ b/src/Umbraco.Infrastructure/Persistence/IProviderSpecificMapperFactory.cs @@ -0,0 +1,8 @@ +namespace Umbraco.Cms.Infrastructure.Persistence +{ + public interface IProviderSpecificMapperFactory + { + string ProviderName { get; } + NPocoMapperCollection Mappers { get; } + } +} diff --git a/src/Umbraco.Infrastructure/Persistence/SqlServerDbProviderFactoryCreator.cs b/src/Umbraco.Infrastructure/Persistence/SqlServerDbProviderFactoryCreator.cs index 789331177e..c6c0c6b0bb 100644 --- a/src/Umbraco.Infrastructure/Persistence/SqlServerDbProviderFactoryCreator.cs +++ b/src/Umbraco.Infrastructure/Persistence/SqlServerDbProviderFactoryCreator.cs @@ -1,6 +1,7 @@ using System; using System.Data.Common; using Microsoft.Extensions.Options; +using NPoco; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax; @@ -51,5 +52,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence { throw new NotSupportedException("Embedded databases are not supported"); } + + public NPocoMapperCollection ProviderSpecificMappers(string providerName) => new NPocoMapperCollection(Array.Empty()); } } diff --git a/src/Umbraco.Infrastructure/Persistence/UmbracoDatabase.cs b/src/Umbraco.Infrastructure/Persistence/UmbracoDatabase.cs index d95b5feaf7..977be3ac5e 100644 --- a/src/Umbraco.Infrastructure/Persistence/UmbracoDatabase.cs +++ b/src/Umbraco.Infrastructure/Persistence/UmbracoDatabase.cs @@ -29,7 +29,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence private readonly DatabaseSchemaCreatorFactory _databaseSchemaCreatorFactory; private readonly RetryPolicy _connectionRetryPolicy; private readonly RetryPolicy _commandRetryPolicy; - //private readonly IEnumerable _mapperCollection; + private readonly IEnumerable _mapperCollection; private readonly Guid _instanceGuid = Guid.NewGuid(); private List _commands; @@ -50,8 +50,8 @@ namespace Umbraco.Cms.Infrastructure.Persistence IBulkSqlInsertProvider bulkSqlInsertProvider, DatabaseSchemaCreatorFactory databaseSchemaCreatorFactory, RetryPolicy connectionRetryPolicy = null, - RetryPolicy commandRetryPolicy = null - /*IEnumerable mapperCollection = null*/) + RetryPolicy commandRetryPolicy = null, + IEnumerable mapperCollection = null) : base(connectionString, sqlContext.DatabaseType, provider, sqlContext.SqlSyntax.DefaultIsolationLevel) { SqlContext = sqlContext; @@ -60,7 +60,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence _databaseSchemaCreatorFactory = databaseSchemaCreatorFactory; _connectionRetryPolicy = connectionRetryPolicy; _commandRetryPolicy = commandRetryPolicy; - //_mapperCollection = mapperCollection; + _mapperCollection = mapperCollection; Init(); } @@ -85,10 +85,10 @@ namespace Umbraco.Cms.Infrastructure.Persistence { EnableSqlTrace = EnableSqlTraceDefault; NPocoDatabaseExtensions.ConfigureNPocoBulkExtensions(); - //if (_mapperCollection != null) - //{ - // Mappers.AddRange(_mapperCollection); - //} + if (_mapperCollection != null) + { + Mappers.AddRange(_mapperCollection); + } } #endregion diff --git a/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs b/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs index 72bde97e95..d018abf8e4 100644 --- a/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs +++ b/src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs @@ -280,6 +280,8 @@ namespace Umbraco.Cms.Infrastructure.Persistence // add all registered mappers for NPoco _pocoMappers.AddRange(_npocoMappers); + _pocoMappers.AddRange(_npocoMappers); + var factory = new FluentPocoDataFactory(GetPocoDataFactoryResolver); _pocoDataFactory = factory; var config = new FluentConfig(xmappers => factory); @@ -323,7 +325,9 @@ namespace Umbraco.Cms.Infrastructure.Persistence _bulkSqlInsertProvider, _databaseSchemaCreatorFactory, _connectionRetryPolicy, - _commandRetryPolicy); + _commandRetryPolicy, + _pocoMappers + ); protected override void DisposeResources() { diff --git a/src/Umbraco.Persistence.SqlCe/SqlCeSpecificMapperFactory.cs b/src/Umbraco.Persistence.SqlCe/SqlCeSpecificMapperFactory.cs new file mode 100644 index 0000000000..0e53561929 --- /dev/null +++ b/src/Umbraco.Persistence.SqlCe/SqlCeSpecificMapperFactory.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core; +using Umbraco.Cms.Infrastructure.Persistence; + +namespace Umbraco.Cms.Persistence.SqlCe +{ + public class SqlCeSpecificMapperFactory : IProviderSpecificMapperFactory + { + public string ProviderName => Constants.DatabaseProviders.SqlCe; + public NPocoMapperCollection Mappers => new NPocoMapperCollection(new[] {new SqlCeImageMapper()}); + } +} diff --git a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs index 764ba49cdc..e329d9fb67 100644 --- a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs @@ -145,7 +145,8 @@ namespace Umbraco.Extensions DbProviderFactories.GetFactory, factory.GetServices(), factory.GetServices(), - factory.GetServices() + factory.GetServices(), + factory.GetServices() )); builder.AddCoreInitialServices(); @@ -351,14 +352,17 @@ namespace Umbraco.Extensions Type sqlCeSyntaxProviderType = umbSqlCeAssembly.GetType("Umbraco.Cms.Persistence.SqlCe.SqlCeSyntaxProvider"); Type sqlCeBulkSqlInsertProviderType = umbSqlCeAssembly.GetType("Umbraco.Cms.Persistence.SqlCe.SqlCeBulkSqlInsertProvider"); Type sqlCeEmbeddedDatabaseCreatorType = umbSqlCeAssembly.GetType("Umbraco.Cms.Persistence.SqlCe.SqlCeEmbeddedDatabaseCreator"); - Type sqlCeImageMapperType = umbSqlCeAssembly.GetType("Umbraco.Cms.Persistence.SqlCe.SqlCeImageMapper"); + Type sqlCeSpecificMapperFactory = umbSqlCeAssembly.GetType("Umbraco.Cms.Persistence.SqlCe.SqlCeSpecificMapperFactory"); - if (!(sqlCeSyntaxProviderType is null || sqlCeBulkSqlInsertProviderType is null || sqlCeEmbeddedDatabaseCreatorType is null)) + if (!(sqlCeSyntaxProviderType is null + || sqlCeBulkSqlInsertProviderType is null + || sqlCeEmbeddedDatabaseCreatorType is null + || sqlCeSpecificMapperFactory is null)) { builder.Services.AddSingleton(typeof(ISqlSyntaxProvider), sqlCeSyntaxProviderType); builder.Services.AddSingleton(typeof(IBulkSqlInsertProvider), sqlCeBulkSqlInsertProviderType); builder.Services.AddSingleton(typeof(IEmbeddedDatabaseCreator), sqlCeEmbeddedDatabaseCreatorType); - builder.NPocoMappers().Add(sqlCeImageMapperType); + builder.Services.AddSingleton(typeof(IProviderSpecificMapperFactory), sqlCeSpecificMapperFactory); } var sqlCeAssembly = Assembly.LoadFrom(Path.Combine(binFolder, "System.Data.SqlServerCe.dll")); diff --git a/src/Umbraco.Web/UmbracoDbProviderFactoryCreator.cs b/src/Umbraco.Web/UmbracoDbProviderFactoryCreator.cs index 24847d3a81..34fcc74d27 100644 --- a/src/Umbraco.Web/UmbracoDbProviderFactoryCreator.cs +++ b/src/Umbraco.Web/UmbracoDbProviderFactoryCreator.cs @@ -2,6 +2,7 @@ using System; using System.Data.Common; using System.Data.SqlServerCe; using Microsoft.Extensions.Options; +using NPoco; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Infrastructure.Migrations.Install; using Umbraco.Cms.Infrastructure.Persistence; @@ -57,5 +58,7 @@ namespace Umbraco.Web var engine = new SqlCeEngine(DatabaseBuilder.EmbeddedDatabaseConnectionString); engine.CreateDatabase(); } + + public NPocoMapperCollection ProviderSpecificMappers(string providerName) => new NPocoMapperCollection(Array.Empty()); } }