2014-01-28 10:46:59 +11:00
|
|
|
using System;
|
2015-12-21 13:36:58 +01:00
|
|
|
using LightInject;
|
2012-12-09 09:01:00 +05:00
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
|
|
|
|
using Umbraco.Core.Persistence.UnitOfWork;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Persistence
|
|
|
|
|
{
|
2012-12-10 08:44:17 -01:00
|
|
|
/// <summary>
|
2016-04-12 19:55:50 +02:00
|
|
|
/// Instanciates repositories.
|
2012-12-10 08:44:17 -01:00
|
|
|
/// </summary>
|
2016-04-12 19:55:50 +02:00
|
|
|
public class RepositoryFactory
|
2012-12-10 08:44:17 -01:00
|
|
|
{
|
2015-12-21 13:36:58 +01:00
|
|
|
private readonly IServiceContainer _container;
|
2016-04-12 19:55:50 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RepositoryFactory"/> class with a container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="container">A container.</param>
|
|
|
|
|
public RepositoryFactory(IServiceContainer container)
|
2015-01-09 10:51:15 +11:00
|
|
|
{
|
2016-04-12 19:55:50 +02:00
|
|
|
if (container == null) throw new ArgumentNullException(nameof(container));
|
2015-12-21 13:36:58 +01:00
|
|
|
_container = container;
|
2015-01-09 10:51:15 +11:00
|
|
|
}
|
|
|
|
|
|
2016-04-28 08:48:59 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a repository.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TRepository">The type of the repository.</typeparam>
|
|
|
|
|
/// <param name="uow">A unit of work.</param>
|
|
|
|
|
/// <param name="name">The optional name of the repository.</param>
|
|
|
|
|
/// <returns>The created repository for the unit of work.</returns>
|
|
|
|
|
public virtual TRepository CreateRepository<TRepository>(IDatabaseUnitOfWork uow, string name = null)
|
|
|
|
|
where TRepository : IRepository
|
2015-01-22 19:19:21 +11:00
|
|
|
{
|
2016-04-28 08:48:59 +02:00
|
|
|
return string.IsNullOrWhiteSpace(name)
|
|
|
|
|
? _container.GetInstance<IDatabaseUnitOfWork, TRepository>(uow)
|
|
|
|
|
: _container.GetInstance<IDatabaseUnitOfWork, TRepository>(uow, name);
|
2015-01-22 19:19:21 +11:00
|
|
|
}
|
2015-11-20 13:39:42 +01:00
|
|
|
|
2016-04-28 08:48:59 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a repository.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TRepository">The type of the repository.</typeparam>
|
|
|
|
|
/// <param name="uow">A unit of work.</param>
|
|
|
|
|
/// <param name="name">The optional name of the repository.</param>
|
|
|
|
|
/// <returns>The created repository for the unit of work.</returns>
|
|
|
|
|
internal virtual TRepository CreateRepository<TRepository>(FileUnitOfWork uow, string name = null)
|
|
|
|
|
where TRepository : IRepository
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrWhiteSpace(name)
|
|
|
|
|
? _container.GetInstance<FileUnitOfWork, TRepository>(uow)
|
|
|
|
|
: _container.GetInstance<FileUnitOfWork, TRepository>(uow, name);
|
2015-11-20 13:39:42 +01:00
|
|
|
}
|
2012-12-10 08:44:17 -01:00
|
|
|
}
|
2012-12-09 09:01:00 +05:00
|
|
|
}
|