Umbraco UI Builder
CMSCloudHeartcoreDXP
10.latest (LTS)
10.latest (LTS)
  • Umbraco UI Builder Documentation
  • Known Issues
  • Release Notes
  • Installation
    • Installing Umbraco UI Builder
    • Licensing
  • Upgrading
    • Upgrading Umbraco UI Builder
    • Version Specific Upgrade Notes
    • Migrate from Konstrukt to Umbraco UI Builder
  • Getting Started
    • Overview
    • Configuration
    • User Interface
  • How-to Guides
    • Creating your first integration
  • Areas
    • Overview
    • Sections
      • Summary Dashboards
    • Trees
      • Folders
    • Dashboards
    • Context Apps
  • Collections
    • Overview
    • The Basics
    • List Views
      • Field Views
    • Editors
    • Child Collections
      • Child Collection Groups
  • Searching
    • Overview
    • Searchable Properties
  • Filtering
    • Overview
    • Global Filters
    • Data Views
      • Data Views Builders
    • Filterable Properties
  • Actions
    • Overview
    • The Basics
    • Action Visibility
    • Inbuilt Actions
  • Cards
    • Overview
    • Count Cards
    • Custom Cards
  • Property Editors
    • Overview
    • Entity Picker
  • Advanced
    • Virtual Sub Trees
    • Encrypted Properties
    • Value Mappers
    • Repositories
    • Events
  • Miscellaneous
    • Conventions
    • Umbraco Aliases
Powered by GitBook
On this page
  • Defining a repository
  • Changing the repository implementation of a collection
  • SetRepositoryType<TRepositoryType>() : CollectionConfigBuilder<TEntityType>
  • SetRepositoryType(Type repositoryType) : CollectionConfigBuilder<TEntityType>
  • Accessing a repository in code
  • IRepositoryFactory.GetRepository<TEntity, TId>() : Repository<TEntity, TId>
  • IRepositoryFactory.GetRepository<TEntity, TId>(string collectionAlias) : Repository<TEntity, TId>
Edit on GitHub
Export as PDF
  1. Advanced

Repositories

Configuring repositories in Umbraco UI Builder, the backoffice UI builder for Umbraco.

Repositories are used by Umbraco UI Builder to access the entity data stores. By default, collections will use a generic built-in NPoco repository. However, you can define your own repository implementation should you wish to store your entities via an alternative strategy.

Defining a repository

To define a repository create a class that inherits from the base class Repository<TEntity, TId> and implements all of its abstract methods.

// Example
public class PersonRepository : Repository<Person, int> {

    public PersonRepository(RepositoryContext context)
        : base(context)
    { }

    protected override int GetIdImpl(Person entity) {
        return entity.Id;
    }

    protected override Person GetImpl(int id) {
        ...
    }

    protected override Person SaveImpl(Person entity) {
        ...
    }

    protected override void DeleteImpl(int id) {
        ...
    }

    protected override IEnumerable<Person> GetAllImpl(Expression<Func<Person, bool>> whereClause, Expression<Func<Person, object>> orderBy, SortDirection orderByDirection) {
        ...
    }

    protected override PagedResult<Person> GetPagedImpl(int pageNumber, int pageSize, Expression<Func<Person, bool>> whereClause, Expression<Func<Person, object>> orderBy, SortDirection orderByDirection) {
        ...
    }

    protected override long GetCountImpl(Expression<Func<Person, bool>> whereClause) {
        ...
    }
}

Note: For all Impl methods there are public alternatives without the Impl suffix. However, there are separate implementation methods in order to ensure all repositories fire the relevant Umbraco UI Builder events. This is whether triggered via the Umbraco UI Builder's UI or not.

Changing the repository implementation of a collection

SetRepositoryType<TRepositoryType>() : CollectionConfigBuilder<TEntityType>

Sets the repository type to the given type for the current collection.

// Example
collectionConfig.SetRepositoryType<PersonRepositoryType>();

SetRepositoryType(Type repositoryType) : CollectionConfigBuilder<TEntityType>

Sets the repository type to the given type for the current collection.

// Example
collectionConfig.SetRepositoryType(typeof(PersonRepositoryType));

Accessing a repository in code

To help with accessing a repository (default or custom) Umbraco UI Builder has an IRepositoryFactory you can inject into your code base. This includes a couple of factory methods to create the repository instances for you. Repositories should only be created via the repository factory as there are some injected dependencies that can only be resolved by Umbraco UI Builder.

IRepositoryFactory.GetRepository<TEntity, TId>() : Repository<TEntity, TId>

Creates a repository for the given entity type. Umbraco UI Builder will search the configuration for the first section/collection with a configuration for the given entity type. Then it will use that as a repository configuration.

// Example
public class MyController : Controller
{
    private readonly Repository<Person, int> _repo;

    public MyController(IRepositoryFactory repoFactory) 
    {
        _repo = repoFactory.GetRepository<Person, int>();
    }
}

IRepositoryFactory.GetRepository<TEntity, TId>(string collectionAlias) : Repository<TEntity, TId>

Creates a repository for the given entity type from the collection with the given alias.

// Example
public class MyController : Controller
{
    private readonly Repository<Person, int> _repo;

    public MyController(IRepositoryFactory repoFactory) 
    {
        _repo = repoFactory.GetRepository<Person, int>("person");
    }
}
PreviousValue MappersNextEvents

Last updated 1 year ago