Skip to content

Commit 1dd08d2

Browse files
committed
Add ID property name constructor argument for MongoDB repository
1 parent d26c74a commit 1dd08d2

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

DynamicRepository.MongoDB/MongoDBRepository.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,42 @@ namespace DynamicRepository.MongoDB
2323
/// <summary>
2424
/// Favoring composition on paged data.
2525
/// </summary>
26-
private DataPager<Key, Entity> _dataPager;
26+
private readonly DataPager<Key, Entity> _dataPager;
2727

2828
/// <summary>
2929
/// The mongoDB database instance to access the desired collection for this repository.
3030
/// </summary>
31-
private IMongoDatabase _mongoDatabase;
31+
private readonly IMongoDatabase _mongoDatabase;
3232

3333
/// <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.
3535
/// </summary>
36-
protected internal IMongoCollection<Entity> Collection;
36+
private readonly string _idPropertyName;
3737

3838
/// <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.
4040
/// </summary>
41-
protected string CollectionName { get; }
41+
private readonly Func<PagedDataSettings, Expression<Func<Entity, bool>>> _preConditionsToPagedDataFilterDelegate;
4242

4343
/// <summary>
44-
/// Global filter instance set by <see cref="HasGlobalFilter(Expression{Func{Entity, bool}})" />
44+
/// Delegate supplied via constructor for extra paged data filtering.
4545
/// </summary>
46-
private Expression<Func<Entity, bool>> GlobalFilter { get; set; }
46+
private readonly Func<PagedDataSettings, Expression<Func<Entity, bool>>> _extraPagedDataFilterDelegate;
4747

4848
/// <summary>
49-
/// Delegate supplied via constructor for pre-condition filtering.
49+
/// Current MongoDB collection being used by this Repository instance.
5050
/// </summary>
51-
private Func<PagedDataSettings, Expression<Func<Entity, bool>>> PreConditionsToPagedDataFilterDelegate { get; set; }
51+
protected internal IMongoCollection<Entity> Collection;
5252

5353
/// <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.
5555
/// </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; }
5762

5863
/// <summary>
5964
/// Default constructor of this Repository.
@@ -64,13 +69,15 @@ namespace DynamicRepository.MongoDB
6469
/// <param name="collectionName">The name of the MongoDB collection to be used with this repository.</param>
6570
public MongoDBRepository(IMongoDatabase mongoDatabase,
6671
string collectionName,
72+
string idPropertyName,
6773
Func<PagedDataSettings, Expression<Func<Entity, bool>>> preConditionsToPagedDataFilterDelegate,
6874
Func<PagedDataSettings, Expression<Func<Entity, bool>>> extraPagedDataFilterDelegate)
6975
{
7076
_mongoDatabase = mongoDatabase;
7177
CollectionName = collectionName;
72-
PreConditionsToPagedDataFilterDelegate = preConditionsToPagedDataFilterDelegate;
73-
ExtraPagedDataFilterDelegate = extraPagedDataFilterDelegate;
78+
_idPropertyName = idPropertyName;
79+
_preConditionsToPagedDataFilterDelegate = preConditionsToPagedDataFilterDelegate;
80+
_extraPagedDataFilterDelegate = extraPagedDataFilterDelegate;
7481

7582
Collection = GetCollection<Entity>(collectionName);
7683

@@ -104,8 +111,7 @@ public IMongoCollection<T> GetCollection<T>(string collectionName) where T : cla
104111
/// </summary>
105112
protected FilterDefinition<Entity> GetIdFilter(Entity entity)
106113
{
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));
109115
}
110116

111117
/// <summary>
@@ -118,8 +124,7 @@ protected FilterDefinition<Entity> GetIdFilter(Entity entity)
118124
/// </example>
119125
protected FilterDefinition<Entity> GetIdFilter(Key id)
120126
{
121-
// TODO: "Id" property name should be configurable
122-
return Builders<Entity>.Filter.Eq("Id", id);
127+
return Builders<Entity>.Filter.Eq(_idPropertyName, id);
123128
}
124129

125130
/// <summary>
@@ -270,7 +275,7 @@ public IEnumerable<Entity> List(Expression<Func<Entity, bool>> filter = null, Fu
270275
/// <returns>Collection of filtered items result.</returns>
271276
public IPagedDataResult<Entity> GetPagedData(PagedDataSettings settings)
272277
{
273-
return _dataPager.GetPagedData(GetQueryable(), settings, PreConditionsToPagedDataFilterDelegate(settings), ExtraPagedDataFilterDelegate(settings));
278+
return _dataPager.GetPagedData(GetQueryable(), settings, _preConditionsToPagedDataFilterDelegate(settings), _extraPagedDataFilterDelegate(settings));
274279
}
275280
}
276281
}

DynamicRepository.MongoDB/Repository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ protected IMongoCollection<Entity> Collection
3737
/// The mongo database to be interfaced and fetch the data.
3838
/// </param>
3939
/// <param name="collectionName">The collection name to be accessed by this repository.</param>
40-
public Repository(IMongoDatabase mongoDatabase, string collectionName)
40+
/// <param name="idPropertyName">Name of the Id property for each document of the collection being accessed. Default is "Id".</param>
41+
public Repository(IMongoDatabase mongoDatabase, string collectionName, string idPropertyName = "Id")
4142
{
4243
_mongoDBRepository = new MongoDBRepository<Key, Entity>(mongoDatabase,
4344
collectionName,
45+
idPropertyName,
4446
AddPreConditionsPagedDataFilter,
4547
AddExtraPagedDataFilter);
4648

0 commit comments

Comments
 (0)