@@ -23,37 +23,42 @@ namespace DynamicRepository.MongoDB
23
23
/// <summary>
24
24
/// Favoring composition on paged data.
25
25
/// </summary>
26
- private DataPager < Key , Entity > _dataPager ;
26
+ private readonly DataPager < Key , Entity > _dataPager ;
27
27
28
28
/// <summary>
29
29
/// The mongoDB database instance to access the desired collection for this repository.
30
30
/// </summary>
31
- private IMongoDatabase _mongoDatabase ;
31
+ private readonly IMongoDatabase _mongoDatabase ;
32
32
33
33
/// <summary>
34
- /// Current MongoDB collection being used by this Repository instance .
34
+ /// Name of the Id property for each document of the collection being accessed .
35
35
/// </summary>
36
- protected internal IMongoCollection < Entity > Collection ;
36
+ private readonly string _idPropertyName ;
37
37
38
38
/// <summary>
39
- /// The name of the MongoDB collection where the data for this entity is stored .
39
+ /// Delegate supplied via constructor for pre-condition filtering .
40
40
/// </summary>
41
- protected string CollectionName { get ; }
41
+ private readonly Func < PagedDataSettings , Expression < Func < Entity , bool > > > _preConditionsToPagedDataFilterDelegate ;
42
42
43
43
/// <summary>
44
- /// Global filter instance set by <see cref="HasGlobalFilter(Expression{Func{Entity, bool}})" />
44
+ /// Delegate supplied via constructor for extra paged data filtering.
45
45
/// </summary>
46
- private Expression < Func < Entity , bool > > GlobalFilter { get ; set ; }
46
+ private readonly Func < PagedDataSettings , Expression < Func < Entity , bool > > > _extraPagedDataFilterDelegate ;
47
47
48
48
/// <summary>
49
- /// Delegate supplied via constructor for pre-condition filtering .
49
+ /// Current MongoDB collection being used by this Repository instance .
50
50
/// </summary>
51
- private Func < PagedDataSettings , Expression < Func < Entity , bool > > > PreConditionsToPagedDataFilterDelegate { get ; set ; }
51
+ protected internal IMongoCollection < Entity > Collection ;
52
52
53
53
/// <summary>
54
- /// Delegate supplied via constructor for extra paged data filtering .
54
+ /// The name of the MongoDB collection where the data for this entity is stored .
55
55
/// </summary>
56
- private Func < PagedDataSettings , Expression < Func < Entity , bool > > > ExtraPagedDataFilterDelegate { get ; set ; }
56
+ protected string CollectionName { get ; }
57
+
58
+ /// <summary>
59
+ /// Global filter instance set by <see cref="HasGlobalFilter(Expression{Func{Entity, bool}})" />
60
+ /// </summary>
61
+ private Expression < Func < Entity , bool > > GlobalFilter { get ; set ; }
57
62
58
63
/// <summary>
59
64
/// Default constructor of this Repository.
@@ -64,13 +69,15 @@ namespace DynamicRepository.MongoDB
64
69
/// <param name="collectionName">The name of the MongoDB collection to be used with this repository.</param>
65
70
public MongoDBRepository ( IMongoDatabase mongoDatabase ,
66
71
string collectionName ,
72
+ string idPropertyName ,
67
73
Func < PagedDataSettings , Expression < Func < Entity , bool > > > preConditionsToPagedDataFilterDelegate ,
68
74
Func < PagedDataSettings , Expression < Func < Entity , bool > > > extraPagedDataFilterDelegate )
69
75
{
70
76
_mongoDatabase = mongoDatabase ;
71
77
CollectionName = collectionName ;
72
- PreConditionsToPagedDataFilterDelegate = preConditionsToPagedDataFilterDelegate ;
73
- ExtraPagedDataFilterDelegate = extraPagedDataFilterDelegate ;
78
+ _idPropertyName = idPropertyName ;
79
+ _preConditionsToPagedDataFilterDelegate = preConditionsToPagedDataFilterDelegate ;
80
+ _extraPagedDataFilterDelegate = extraPagedDataFilterDelegate ;
74
81
75
82
Collection = GetCollection < Entity > ( collectionName ) ;
76
83
@@ -104,8 +111,7 @@ public IMongoCollection<T> GetCollection<T>(string collectionName) where T : cla
104
111
/// </summary>
105
112
protected FilterDefinition < Entity > GetIdFilter ( Entity entity )
106
113
{
107
- // TODO: "Id" property name should be configurable
108
- return Builders < Entity > . Filter . Eq ( "Id" , entity . GetType ( ) . GetProperty ( "Id" ) . GetValue ( entity , null ) ) ;
114
+ return Builders < Entity > . Filter . Eq ( _idPropertyName , entity . GetType ( ) . GetProperty ( "Id" ) . GetValue ( entity , null ) ) ;
109
115
}
110
116
111
117
/// <summary>
@@ -118,8 +124,7 @@ protected FilterDefinition<Entity> GetIdFilter(Entity entity)
118
124
/// </example>
119
125
protected FilterDefinition < Entity > GetIdFilter ( Key id )
120
126
{
121
- // TODO: "Id" property name should be configurable
122
- return Builders < Entity > . Filter . Eq ( "Id" , id ) ;
127
+ return Builders < Entity > . Filter . Eq ( _idPropertyName , id ) ;
123
128
}
124
129
125
130
/// <summary>
@@ -270,7 +275,7 @@ public IEnumerable<Entity> List(Expression<Func<Entity, bool>> filter = null, Fu
270
275
/// <returns>Collection of filtered items result.</returns>
271
276
public IPagedDataResult < Entity > GetPagedData ( PagedDataSettings settings )
272
277
{
273
- return _dataPager . GetPagedData ( GetQueryable ( ) , settings , PreConditionsToPagedDataFilterDelegate ( settings ) , ExtraPagedDataFilterDelegate ( settings ) ) ;
278
+ return _dataPager . GetPagedData ( GetQueryable ( ) , settings , _preConditionsToPagedDataFilterDelegate ( settings ) , _extraPagedDataFilterDelegate ( settings ) ) ;
274
279
}
275
280
}
276
281
}
0 commit comments