summaryrefslogtreecommitdiff
path: root/product/DataAccess/Transactions/Session.cs
blob: d355a2e443611822b6ecd9d625677c4f8cc6dfd8 (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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Gorilla.Commons.Infrastructure.Logging;
using Gorilla.Commons.Utility.Core;
using Gorilla.Commons.Utility.Extensions;

namespace MoMoney.DataAccess.Transactions
{
    public interface ISession : IDisposable
    {
        T find<T>(Guid guid) where T : IIdentifiable<Guid>;
        IEnumerable<T> all<T>() where T : IIdentifiable<Guid>;
        void save<T>(T entity) where T : IIdentifiable<Guid>;
        void delete<T>(T entity) where T : IIdentifiable<Guid>;
        void flush();
        bool is_dirty();
    }

    public class Session : ISession
    {
        ITransaction transaction;
        readonly IDatabase database;
        readonly IDictionary<Type, object> identity_maps;
        long id;

        public Session(ITransaction transaction, IDatabase database)
        {
            this.database = database;
            this.transaction = transaction;
            identity_maps = new Dictionary<Type, object>();
            id = DateTime.Now.Ticks;
        }

        public T find<T>(Guid id) where T : IIdentifiable<Guid>
        {
            if (get_identity_map_for<T>().contains_an_item_for(id))
            {
                return get_identity_map_for<T>().item_that_belongs_to(id);
            }

            var entity = database.fetch_all<T>().Single(x => x.id.Equals(id));
            get_identity_map_for<T>().add(id, entity);
            return entity;
        }

        public IEnumerable<T> all<T>() where T : IIdentifiable<Guid>
        {
            database
                .fetch_all<T>()
                .where(x => !get_identity_map_for<T>().contains_an_item_for(x.id))
                .each(x => get_identity_map_for<T>().add(x.id, x));
            return get_identity_map_for<T>().all();
        }

        public void save<T>(T entity) where T : IIdentifiable<Guid>
        {
            this.log().debug("saving {0}: {1}", id, entity);
            get_identity_map_for<T>().add(entity.id, entity);
        }

        public void delete<T>(T entity) where T : IIdentifiable<Guid>
        {
            get_identity_map_for<T>().evict(entity.id);
        }

        public void flush()
        {
            this.log().debug("flushing session {0}", id);
            transaction.commit_changes();
            transaction = null;
        }

        public bool is_dirty()
        {
            this.log().debug("is dirty? {0}", id);
            return null != transaction && transaction.is_dirty();
        }

        public void Dispose()
        {
            if (null != transaction) transaction.rollback_changes();
        }

        IIdentityMap<Guid, T> get_identity_map_for<T>() where T : IIdentifiable<Guid>
        {
            return identity_maps.ContainsKey(typeof (T))
                       ? identity_maps[typeof (T)].downcast_to<IIdentityMap<Guid, T>>()
                       : create_map_for<T>();
        }

        IIdentityMap<Guid, T> create_map_for<T>() where T : IIdentifiable<Guid>
        {
            var identity_map = transaction.create_for<T>();
            identity_maps.Add(typeof (T), identity_map);
            return identity_map;
        }

        public override string ToString()
        {
            return "session: {0}".formatted_using(id);
        }
    }
}