This repository has been archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
EFRepository.cs
149 lines (131 loc) · 5.24 KB
/
EFRepository.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
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
140
141
142
143
144
145
146
147
148
149
#region license
//Copyright 2010 Ritesh Rao
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
#endregion
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.Practices.ServiceLocation;
using NCommon.Expressions;
using NCommon.Extensions;
namespace NCommon.Data.EntityFramework
{
/// <summary>
/// Inherits from the <see cref="RepositoryBase{TEntity}"/> class to provide an implementation of a
/// Repository that uses Entity Framework.
/// </summary>
public class EFRepository<TEntity> : RepositoryBase<TEntity> where TEntity : class
{
readonly IEFSession _privateSession;
readonly List<string> _includes = new List<string>();
/// <summary>
/// Creates a new instance of the <see cref="EFRepository{TEntity}"/> class.
/// </summary>
public EFRepository()
{
Initialize();
}
/// <summary>
/// Default Init.
/// </summary>
protected virtual void Initialize()
{
if (ServiceLocator.Current == null)
return;
var sessions = ServiceLocator.Current.GetAllInstances<IEFSession>();
if (sessions != null && sessions.Count() > 0)
_privateSession = sessions.First();
}
/// <summary>
/// Gets the <see cref="ObjectContext"/> to be used by the repository.
/// </summary>
private IEFSession Session
{
get
{
if (_privateSession != null)
return _privateSession;
var unitOfWork = UnitOfWork<EFUnitOfWork>();
return unitOfWork.GetSession<TEntity>();
}
}
/// <summary>
/// Gets the <see cref="IQueryable{TEntity}"/> used by the <see cref="RepositoryBase{TEntity}"/>
/// to execute Linq queries.
/// </summary>
/// <value>A <see cref="IQueryable{TEntity}"/> instance.</value>
protected override IQueryable<TEntity> RepositoryQuery
{
get
{
var query = Session.CreateQuery<TEntity>();
if (_includes.Count > 0)
_includes.ForEach(x => query = query.Include(x));
return query;
}
}
/// <summary>
/// Adds a transient instance of <typeparamref cref="TEntity"/> to be tracked
/// and persisted by the repository.
/// </summary>
/// <param name="entity"></param>
public override void Add(TEntity entity)
{
Session.Add(entity);
}
/// <summary>
/// Marks the entity instance to be deleted from the store.
/// </summary>
/// <param name="entity">An instance of <typeparamref name="TEntity"/> that should be deleted.</param>
public override void Delete(TEntity entity)
{
Session.Delete(entity);
}
/// <summary>
/// Detaches a instance from the repository.
/// </summary>
/// <param name="entity">The entity instance, currently being tracked via the repository, to detach.</param>
/// <exception cref="NotImplementedException">Implentors should throw the NotImplementedException if Detaching
/// entities is not supported.</exception>
public override void Detach(TEntity entity)
{
Session.Detach(entity);
}
/// <summary>
/// Attaches a detached entity, previously detached via the <see cref="IRepository{TEntity}.Detach"/> method.
/// </summary>
/// <param name="entity">The entity instance to attach back to the repository.</param>
/// <exception cref="NotImplementedException">Implentors should throw the NotImplementedException if Attaching
/// entities is not supported.</exception>
public override void Attach(TEntity entity)
{
Session.Attach(entity);
}
/// <summary>
/// Refreshes a entity instance.
/// </summary>
/// <param name="entity">The entity to refresh.</param>
/// <exception cref="NotImplementedException">Implementors should throw the NotImplementedException if Refreshing
/// entities is not supported.</exception>
public override void Refresh(TEntity entity)
{
Session.Refresh(entity);
}
internal void AddInclude(string includePath)
{
_includes.Add(includePath);
}
}
}