using System; using LightInject; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Persistence { /// /// Instanciates repositories. /// public class RepositoryFactory { private readonly IServiceContainer _container; /// /// Initializes a new instance of the class with a container. /// /// A container. public RepositoryFactory(IServiceContainer container) { if (container == null) throw new ArgumentNullException(nameof(container)); _container = container; } /// /// Creates a repository. /// /// The type of the repository. /// A unit of work. /// The optional name of the repository. /// The created repository for the unit of work. public virtual TRepository CreateRepository(IDatabaseUnitOfWork uow, string name = null) where TRepository : IRepository { return string.IsNullOrWhiteSpace(name) ? _container.GetInstance(uow) : _container.GetInstance(uow, name); } /// /// Creates a repository. /// /// The type of the repository. /// A unit of work. /// The optional name of the repository. /// The created repository for the unit of work. internal virtual TRepository CreateRepository(FileUnitOfWork uow, string name = null) where TRepository : IRepository { return string.IsNullOrWhiteSpace(name) ? _container.GetInstance(uow) : _container.GetInstance(uow, name); } } }