aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.BL/ReadOnlyEntityRepository.cs
blob: f275483091dd1f8b7e57b7e981f3db910a54a067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Tango.BL;

namespace Tango.FSE.BL
{
    public class ReadOnlyEntityRepository<TEntity, TCachedEntity> : EntityRepositoryBase<TEntity, TCachedEntity> where TEntity : ObservableEntity<TEntity> where TCachedEntity : ObservableEntityDTO<TCachedEntity, TEntity>
    {
        private Func<TCachedEntity, TEntity> _convertToEntity;
        private Func<TEntity, TCachedEntity> _convertToCached;
        private DataResolverNode[] _nodes;

        public ReadOnlyEntityRepository(Func<TCachedEntity, TEntity> convertToEntity, Func<TEntity, TCachedEntity> convertToCached, params DataResolverNode[] nodes)
        {
            _convertToEntity = convertToEntity;
            _convertToCached = convertToCached;
            _nodes = nodes;
        }

        protected override TCachedEntity ConvertToCached(TEntity entity)
        {
            return _convertToCached.Invoke(entity);
        }

        protected override TEntity ConvertToEntity(TCachedEntity cachedEntity)
        {
            return _convertToEntity(cachedEntity);
        }

        public Task<List<TEntity>> FindAll(Expression<Func<TEntity, bool>> expression)
        {
            return FindAll(expression, _nodes);
        }

        public Task<List<TEntity>> FindAll()
        {
            return FindAll(x => true, _nodes);
        }

        public Task<TEntity> FindOne(Expression<Func<TEntity, bool>> expression)
        {
            return FindOne(expression, _nodes);
        }
    }
}