forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryRepository.cs
More file actions
139 lines (118 loc) · 2.94 KB
/
Copy pathMemoryRepository.cs
File metadata and controls
139 lines (118 loc) · 2.94 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SmartStore.Core;
using SmartStore.Core.Data;
using Rhino.Mocks;
using System.Data;
using System.Data.Entity;
namespace SmartStore.Tests
{
public class MemoryRepository<T> : IRepository<T> where T : BaseEntity, new()
{
private readonly TestDbSet<T> _dbSet = new TestDbSet<T>();
private IDbContext _dbContext;
public IQueryable<T> Table
{
get { return _dbSet; }
}
public IQueryable<T> TableUntracked
{
get { return Table; }
}
public ICollection<T> Local
{
get { return _dbSet.Local; }
}
public T Create()
{
return _dbSet.Create<T>();
}
public T GetById(object id)
{
return _dbSet.Find(id);
}
public T Attach(T entity)
{
return _dbSet.Attach(entity);
}
public void Insert(T entity)
{
if (entity.Id <= 0)
{
entity.Id = Local.Count == 0 ? 1 : Local.Max(x => x.Id) + 1;
}
_dbSet.Add(entity);
}
public void InsertRange(IEnumerable<T> entities, int batchSize = 100)
{
entities.Each(x => Insert(x));
}
public void Update(T entity)
{
// Noop
}
public void UpdateRange(IEnumerable<T> entities)
{
entities.Each(x => Update(x));
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
}
public void DeleteRange(IEnumerable<T> entities)
{
_dbSet.RemoveRange(entities);
}
public IQueryable<T> Expand(IQueryable<T> query, string path)
{
return Table;
}
public IQueryable<T> Expand<TProperty>(IQueryable<T> query, Expression<Func<T, TProperty>> path)
{
return Table;
}
public IDbContext Context
{
get
{
if (_dbContext == null)
{
var ctx = MockRepository.GenerateMock<IDbContext>();
ctx.Stub(x => x.Set<T>()).Return(_dbSet);
ctx.Stub(x => x.SaveChangesAsync()).Return(Task.FromResult<int>(0));
ctx.Stub(x => x.ExecuteStoredProcedureList<T>(Arg<string>.Is.Anything, Arg<object[]>.Is.Anything)).Return(new List<T>());
ctx.Stub(x => x.GetModifiedProperties(Arg<BaseEntity>.Is.Anything)).Return(new Dictionary<string, object>());
ctx.Stub(x => x.BeginTransaction(Arg<IsolationLevel>.Is.Anything)).Return(MockRepository.GenerateMock<ITransaction>());
ctx.Stub(x => x.ChangeState(Arg<BaseEntity>.Is.TypeOf, Arg<System.Data.Entity.EntityState>.Is.Anything))
.WhenCalled(ChangeState);
_dbContext = ctx;
}
return _dbContext;
}
}
private void ChangeState(MethodInvocation invocation)
{
var entity = (T)invocation.Arguments[0];
var state = (System.Data.Entity.EntityState)invocation.Arguments[1];
if (state == System.Data.Entity.EntityState.Deleted)
{
if (_dbSet.Contains(entity))
{
_dbSet.Remove(entity);
}
}
else if (state == System.Data.Entity.EntityState.Added)
{
if (!_dbSet.Contains(entity))
{
_dbSet.Add(entity);
}
}
}
public bool? AutoCommitEnabled { get; set; }
}
}