-
Notifications
You must be signed in to change notification settings - Fork 3
Model
The package BIA.Net.Model contains mainly the class:
- TGenericRepository<Entity, ProjectDBContext>
- TDBContainer< ProjectDBContext >
- TGenericTransaction< ProjectDBContext >
Those classes offer advanced functionnalities for entity framework:
- Four filters context (All, Read, Write, Delete)
- Standard manipulation function.
It requiered to have an entityframework model.
=> In your project create an entityframework model change the template who generate entity classe to make inherit this classes of ObjectRemap. Sample here.
- in following sample the name of the Entity Container is : ZZProjectNameZZDBContainer
=> The TDBContainer should be referenced by unity. Place the folowing code in unity config :
BIAUnity.RegisterType<TDBContainer<ZZProjectNameZZDBContainer>, TDBContainer<ZZProjectNameZZDBContainer>>();
=> Acces to the repository and entity elements :
/// Create a repository based on an Entity Classe (here: MyEntity)
TGenericRepository<MyEntity, ZZProjectNameZZDBContainer> Repository = new TGenericRepository<MyEntity, ZZProjectNameZZDBContainer>();;
... Initialization option ....
/// Acces to element
IQueryable<MyEntity> query = Repository.GetStandardQuery();
List<MyEntity> list = query.ToList();
=> Use Context filter :
You can use context filter to filter elements that can be read, update, delete. There is 4 filters property in the classe GenericRepository :
- Expression<Func<Entity, bool>> FilterContextAll (if not define return all) => corresponding access flag : AccessMode.All
- Expression<Func<Entity, bool>> FilterContextRead (= FilterContextAll if not define) => corresponding access flag : AccessMode.Read
- Expression<Func<Entity, bool>> FilterContextWrite (= FilterContextRead if not define) => corresponding access flag : AccessMode.Write
- Expression<Func<Entity, bool>> FilterContextDelete (= FilterContextDelete if not define) => corresponding access flag : AccessMode.Delete
After the creation of the repository you can define tose filter, by using a Linq expression (this expression should be base on relations of your Entity ) ex :
Repository.FilterContextRead = s => s.Members.Any(m => m.AspNetUser.Id == userId);
Repository.FilterContextWrite = s => s.Members.Any(m => m.AspNetUser.Id == userId && m.MemberRole.Any(mr => mr.Id == Constants.MemberRoleSiteAdmin));
Now when you get the query you will have a different result, depending on the flag you passe in parameter:
IQueryable<MyEntity> query = Repository.GetStandardQuery(AccessMode.Read);
List<MyEntity> list = query.ToList(); => This return the list of all element filter by FilterContextRead
-----------------------------------------------
IQueryable<MyEntity> query = Repository.GetStandardQuery(AccessMode.Write);
List<MyEntity> list = query.ToList(); => This return the list of all element filter by FilterContextWrite
...
=> Standard manipulation function
- Entity Insert(Entity entity, GenericRepositoryParmeter param = null) => insert an element ex:
MyEntity entity = new MyEntity();
entity.Name = "MyName";
MyEntity entityInsered = Repository.Insert(entity);
- Entity UpdateValues(Entity entity, List valuesToUpdate); => update a list of values in the entity, ONLY if the entity match with "FilterContextWrite" :
MyEntity entity = new MyEntity ();
entity.Name = "MyNewName";
MyEntity entityUpdated = Repository.UpdateValues(entity, new List<string>() { nameof(MyEntity.Name) });
System.Diagnostics.Debug.Assert(entityUpdated != null, "Cannot update value");
• int DeleteById(object primaryKey, GenericRepositoryParmeter param = null); => delete an entity find by its primary keys, ONLY if the entity match with "FilterContextDelete" :
int id = 1;
int ret = Repository.DeleteById(id);
System.Diagnostics.Debug.Assert(ret == 0, "Cannot delete value");
=> Begin insert id at a specific value
If the key of your entity is not in automatic increment, the id will be generated based on the min value in the liste +1. You can use the option MinInsertKeysValue to begin at a specific number:
MyEntity entity = new MyEntity();
entity.Name = "MyName";
Repository.MinInsertKeysValue = 10000;
MyEntity entityInsered = Repository.Insert(entity);
-
ListInclude
-
GenericRepositoryParmeter param