Mad.Repositories

Mad repositories contains different interfaces and implementations for data access and persisting.


IRepository<T>

This is an Id based storage repository. It also implements IPersistor and IQCollection.

Interface

namespace Mad.Repositories
{
    public interface IRepository<T> : IRepository<T, object>
    {
    }

    public interface IRepository<T, TId> : IQCollection<T>, IPersistor<T>
    {
        T GetById(TId id);
        IEnumerable<T> GetByIds(IEnumerable<TId> ids);
        Result<T> Upsert(T item);
        Result<T> Update(T item);
        Result UpdatePropertyById(string propertyName, object value, TId id);
        Result UpdatePropertiesById(ValueSet properties, TId id);
        bool ExistsById(TId id);
        Result DeleteById(TId id);
    }
}

RepFac

Use the RepFac to create instances of the Respository class.


BaseRepository

An abstract class where you can inject an IRepository and then extended the repository with custom helper methods.


IPersistor<T>

Used for CRUD operations with simple querying.

Interface

namespace Mad.Repositories.Persisting
{
    public interface IPersistor
    {
        IEnumerable<ValueSet> GetByMatch(MatchSet where);
        Result<ValueSet> Add(ValueSet item);
        Result UpdatePropertyByMatch(string name, object value, MatchSet where);
        Result UpdatePropertiesByMatch(ValueSet properties, MatchSet where);
        Result DeleteByMatch(MatchSet where);
        long CountByMatch(MatchSet where);
        bool ExistsByMatch(MatchSet where);
    }

    public interface IPersistor<T> : IPersistor
    {
        new IEnumerable<T> GetByMatch(MatchSet where);
        Result<T> Add(T item);
    }
}

Implementations


IQCollection<T>

Used for querying data collections.

Interface

namespace Mad.Repositories.Querying
{
    public interface IQCollection : IQGetter, IQCounter
    {
    }

    public interface IQCollection<T> : IQCollection, IQGetter<T>
    {
    }

    public interface IQGetter
    {
        IEnumerable<ValueSet> Get(QQuery q);
    }

    public interface IQGetter<T> : IQGetter
    {
        new IEnumerable<T> Get(QQuery q);
    }

    public interface IQCounter
    {
        long Count(QExpression expression);
    }
}

Implementations

Query Builder

Use the query builder to build queries.

By Property
  Q.Where(prop, value)
And
  Q.Where(prop, value).And(prop, value)
Or
  Q.Where(prop, value).Or(prop, value)
Sub Group
  Q.Where(prop, value).And(Q.Where(prop, value).Or(prop, value))
Operator
  Q.Where(prop, QFilterType.GreaterThan, value)
Order By
  Q.Where(prop, value).OrderBy(prop)
Order By Desc
  Q.Where(prop, value).OrderByDesc(prop)
Paging
  Q.Where(prop, value).OrderBy(prop, value).Page(page, pageSize)
Get All
  Q.All()

Code Example


    public class AccountEntity
    {
        public string Id { get; set; }
        public string Email { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public DateTime Date { get; set; }
    }

    public class AccountService
    {
        private AccountRepository _repository;

        public AccountService(IRepository<AccountEntity> repository)
        {
            _repository = new AccountRepository(repository);
        }

        public AccountEntity GetByEmail(string email)
        {
            return _repository.GetFirstOrDefault(Q.Where("Email", email))
        }

        public Result<AccountEntity> Add(AccountEntity account)
        {
            return _repository.Add(r);
        }

        public Result<AccountEntity> Update(AccountEntity account)
        {
            return _repository.Update(r);
        }

        public IEnumerable<AccountEntity> GetAll()
        {
            return _repository.Get(Q.All());
        }
    }