Skip to content

How to create Repository

marektihkan edited this page Sep 13, 2010 · 2 revisions

Workflow

  1. Create repository interface to Core.(BoundedContext).Services.Repositories namespace
  2. Create repository class to Services.Repositories namespace
  3. Inherit this class from Arc.Infrastructure.Data.BaseRepository<TEntity>
  4. Create default constructor for this class with IRepository<TEntity>
    public PersonRepository(IRepository<Person> repository) : base(repository) {}
  5. Use InnerRepository property to create queries, save, delete etc
  6. Register repository to Service Locator if needed (use conventions for less configuration)

Remarks

  • Repository should be registered to Service Locator

Example


namespace ExampleSolution.Core.Parties.Services.Repositories
{
    public interface IExampleEntityRepository
    {
        Person GetPersonById(int id);

        IList<Person> GetAllPersons();

        long GetPersonsCount();

        void Save(Person entity);
	
        void Delete(Person entity);
    }
}

namespace ExampleSolution.Services.Repositories
{
    public class PersonRepository : BaseRepository<Person>, IPersonRepository
    {
        public PersonRepository(IRepository<Person> repository) : base(repository) {}


        public Person GetPerson(int id)
        {
            return InnerRepository.GetEntityById(id);
        }

        public IList<Person> GetAllPersons()
        {
            return InnerRepository.GetAllEntities<Person>();
        }

        public long GetPersonsCount()
        {
            return InnerRepository.Count(InnerRepository.CreateCriteria<Person>());
        }

        public void Save(Person entity)
        {
            InnerRepository.Save(entity);
        }

        public void Delete(Person entity)
        {
            InnerRepository.Delete(entity);
        }
    }
}