blob: 4fd5905305d8d10b88bf9c9d3943ed54925cfbae (
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
48
49
50
51
52
53
54
55
56
|
using System;
using System.Collections.Generic;
using System.Linq;
using Gorilla.Commons.Infrastructure.Transactions;
using Gorilla.Commons.Utility.Core;
using Gorilla.Commons.Utility.Extensions;
namespace MoMoney.DataAccess.Transactions
{
public class ChangeTracker<T> : IChangeTracker<T> where T : IIdentifiable<Guid>
{
readonly ITrackerEntryMapper<T> mapper;
readonly IStatementRegistry registry;
readonly IList<ITrackerEntry<T>> items;
readonly IList<T> to_be_deleted;
public ChangeTracker(ITrackerEntryMapper<T> mapper, IStatementRegistry registry)
{
this.mapper = mapper;
this.registry = registry;
items = new List<ITrackerEntry<T>>();
to_be_deleted = new List<T>();
}
public void register(T entity)
{
items.Add(mapper.map_from(entity));
}
public void delete(T entity)
{
to_be_deleted.Add(entity);
}
public void commit_to(IDatabase database)
{
items.each(x => commit(x, database));
to_be_deleted.each(x => database.apply(registry.prepare_command_for(x)));
}
public bool is_dirty()
{
return items.Count(x => x.has_changes()) > 0 || to_be_deleted.Count > 0;
}
public void Dispose()
{
items.Clear();
}
void commit(ITrackerEntry<T> entry, IDatabase database)
{
if (entry.has_changes()) database.apply(registry.prepare_command_for(entry.current));
}
}
}
|