Fixes ServiceContext to ensure the repo's are lazy instantiated because their ctor's rely on the

RepositoryResolver.Current being initialized which normally doesn't occur until after the ServiceContext
is constructed. Adds instance level caching for the GetRecursiveValue method in case this is called more than
one time for a property in one view. Reverts PetaPocoUnitOfWork to allow more than one call to Commit().. this
isn't 'best practices' per se but it is more for performance reasons because otherwise we'd have to create a new
repo object + uow for any bulk saving operations... The end result is the same, bulk operations in the Services
cannot be processed in one transaction. Fixing up the ContentServiceTests by ensuring that the shared and always open sqlce
connection with the legacy SqlCeContextGuardian is closed on TearDown.
This commit is contained in:
Shannon Deminick
2012-12-15 10:33:29 +05:00
parent ad7aa66b0b
commit b73efd16dc
6 changed files with 98 additions and 71 deletions

View File

@@ -1,3 +1,4 @@
using System;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
@@ -11,14 +12,14 @@ namespace Umbraco.Core.Services
/// </summary>
public class ServiceContext
{
private ContentService _contentService;
private UserService _userService;
private MediaService _mediaService;
private MacroService _macroService;
private ContentTypeService _contentTypeService;
private DataTypeService _dataTypeService;
private FileService _fileService;
private LocalizationService _localizationService;
private Lazy<ContentService> _contentService;
private Lazy<UserService> _userService;
private Lazy<MediaService> _mediaService;
private Lazy<MacroService> _macroService;
private Lazy<ContentTypeService> _contentTypeService;
private Lazy<DataTypeService> _dataTypeService;
private Lazy<FileService> _fileService;
private Lazy<LocalizationService> _localizationService;
/// <summary>
/// Constructor
@@ -28,7 +29,10 @@ namespace Umbraco.Core.Services
/// <param name="publishingStrategy"></param>
internal ServiceContext(IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, IUnitOfWorkProvider fileUnitOfWorkProvider, IPublishingStrategy publishingStrategy)
{
BuildServiceCache(dbUnitOfWorkProvider, fileUnitOfWorkProvider, publishingStrategy, RepositoryResolver.Current.Factory);
BuildServiceCache(dbUnitOfWorkProvider, fileUnitOfWorkProvider, publishingStrategy,
//this needs to be lazy because when we create the service context it's generally before the
//resolvers have been initialized!
new Lazy<RepositoryFactory>(() => RepositoryResolver.Current.Factory));
}
/// <summary>
@@ -38,34 +42,34 @@ namespace Umbraco.Core.Services
IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider,
IUnitOfWorkProvider fileUnitOfWorkProvider,
IPublishingStrategy publishingStrategy,
RepositoryFactory repositoryFactory)
Lazy<RepositoryFactory> repositoryFactory)
{
var provider = dbUnitOfWorkProvider;
var fileProvider = fileUnitOfWorkProvider;
var fileProvider = fileUnitOfWorkProvider;
if(_userService == null)
_userService = new UserService(provider, repositoryFactory);
if (_userService == null)
_userService = new Lazy<UserService>(() => new UserService(provider, repositoryFactory.Value));
if (_contentService == null)
_contentService = new ContentService(provider, repositoryFactory, publishingStrategy, _userService);
_contentService = new Lazy<ContentService>(() => new ContentService(provider, repositoryFactory.Value, publishingStrategy, _userService.Value));
if(_mediaService == null)
_mediaService = new MediaService(provider, repositoryFactory);
_mediaService = new Lazy<MediaService>(() => new MediaService(provider, repositoryFactory.Value));
if(_macroService == null)
_macroService = new MacroService(fileProvider, repositoryFactory);
_macroService = new Lazy<MacroService>(() => new MacroService(fileProvider, repositoryFactory.Value));
if(_contentTypeService == null)
_contentTypeService = new ContentTypeService(provider, repositoryFactory, _contentService, _mediaService);
_contentTypeService = new Lazy<ContentTypeService>(() => new ContentTypeService(provider, repositoryFactory.Value, _contentService.Value, _mediaService.Value));
if(_dataTypeService == null)
_dataTypeService = new DataTypeService(provider, repositoryFactory);
_dataTypeService = new Lazy<DataTypeService>(() => new DataTypeService(provider, repositoryFactory.Value));
if(_fileService == null)
_fileService = new FileService(fileProvider, provider, repositoryFactory);
_fileService = new Lazy<FileService>(() => new FileService(fileProvider, provider, repositoryFactory.Value));
if(_localizationService == null)
_localizationService = new LocalizationService(provider, repositoryFactory);
_localizationService = new Lazy<LocalizationService>(() => new LocalizationService(provider, repositoryFactory.Value));
}
/// <summary>
@@ -73,7 +77,7 @@ namespace Umbraco.Core.Services
/// </summary>
public IContentService ContentService
{
get { return _contentService; }
get { return _contentService.Value; }
}
/// <summary>
@@ -81,7 +85,7 @@ namespace Umbraco.Core.Services
/// </summary>
public IContentTypeService ContentTypeService
{
get { return _contentTypeService; }
get { return _contentTypeService.Value; }
}
/// <summary>
@@ -89,7 +93,7 @@ namespace Umbraco.Core.Services
/// </summary>
public IDataTypeService DataTypeService
{
get { return _dataTypeService; }
get { return _dataTypeService.Value; }
}
/// <summary>
@@ -97,7 +101,7 @@ namespace Umbraco.Core.Services
/// </summary>
public IFileService FileService
{
get { return _fileService; }
get { return _fileService.Value; }
}
/// <summary>
@@ -105,7 +109,7 @@ namespace Umbraco.Core.Services
/// </summary>
public ILocalizationService LocalizationService
{
get { return _localizationService; }
get { return _localizationService.Value; }
}
/// <summary>
@@ -113,7 +117,7 @@ namespace Umbraco.Core.Services
/// </summary>
public IMediaService MediaService
{
get { return _mediaService; }
get { return _mediaService.Value; }
}
/// <summary>
@@ -121,7 +125,7 @@ namespace Umbraco.Core.Services
/// </summary>
internal IMacroService MacroService
{
get { return _macroService; }
get { return _macroService.Value; }
}
/// <summary>
@@ -129,7 +133,7 @@ namespace Umbraco.Core.Services
/// </summary>
internal IUserService UserService
{
get { return _userService; }
get { return _userService.Value; }
}
}
}