PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost.RavenDb.Embeddable/EntityContext.cs

#
C# | 59 lines | 47 code | 12 blank | 0 comment | 0 complexity | 11dc529952258b42686ce38d94fb5f39 MD5 | raw file
 1using Bifrost.Entities;
 2using System.Linq;
 3using Raven.Client.Embedded;
 4using Raven.Client;
 5
 6namespace Bifrost.RavenDb.Embeddable
 7{
 8    public class EntityContext<T> : IEntityContext<T>
 9    {
10        IEntityContextConnection _connection;
11        
12        IDocumentSession _session;
13
14        public EntityContext(EntityContextConnection connection)
15        {
16            _connection = connection;
17            _session = connection.DocumentStore.OpenSession();
18        }
19
20
21        public IQueryable<T> Entities { get { return _session.Query<T>(); } }
22
23        public void Attach(T entity)
24        {
25        }
26
27        public void Insert(T entity)
28        {
29            _session.Store(entity);
30        }
31
32        public void Update(T entity)
33        {
34            _session.Store(entity);
35        }
36
37        public void Delete(T entity)
38        {
39            _session.Delete(entity);
40        }
41
42        public void Save(T entity)
43        {
44            _session.Store(entity);
45            _session.SaveChanges();
46        }
47
48        public void Commit()
49        {
50            _session.SaveChanges();
51        }
52
53        public void Dispose()
54        {
55            _session.SaveChanges();
56            _session.Dispose();
57        }
58    }
59}