-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLegacyRepositoryBase.cs
67 lines (53 loc) · 2.22 KB
/
LegacyRepositoryBase.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
namespace KalikoCMS.Legacy.Data.Repositories {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using KalikoCMS.Data.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
public abstract class LegacyRepositoryBase<TSource, TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class where TSource : class {
private readonly LegacyDataContext _context;
protected LegacyRepositoryBase(LegacyDataContext context) {
_context = context;
}
public virtual IQueryable<TEntity> GetAll() {
return _context.Set<TSource>().Select(Map()).AsNoTracking();
}
public abstract TEntity GetById(TKey id);
public virtual void Create(TEntity entity) {
throw new NotImplementedException();
}
public virtual void Update(TEntity entity) {
throw new NotImplementedException();
}
public virtual void Delete(TKey id) {
throw new NotImplementedException();
}
public virtual TEntity FirstOrDefault(Func<TEntity, bool> predicate) {
return _context.Set<TSource>().Select(Map()).FirstOrDefault(predicate);
}
public virtual IEnumerable<TEntity> Select(Func<TEntity, bool> predicate) {
return _context.Set<TSource>().Select(Map()).AsEnumerable().Where(predicate);
}
public void Delete(Func<TEntity, bool> predicate) {
throw new NotImplementedException();
}
public virtual TEntity SingleOrDefault(Func<TEntity, bool> predicate) {
return _context.Set<TSource>().Select(Map()).SingleOrDefault(predicate);
}
public abstract Expression<Func<TSource, TEntity>> Map();
public Guid ToGuid(int value) {
var bytes = new byte[16];
BitConverter.GetBytes(value).CopyTo(bytes, 0);
var guid = new Guid(bytes);
return guid;
}
public int ToInt(Guid value) {
var bytes = new byte[16];
value.ToByteArray().CopyTo(bytes, 0);
return BitConverter.ToInt32(bytes, 0);
}
}
}