IHasCreationTime (ihascreationtime)
Preface
Because the company's projects are implemented based on the ABC framework, and recently the project is being adjusted, we want to implement it by ourselves.Module-ZeroAfter allModule-ZeroYes (You know). Today, we implement the function of automatically adding the creator and creating the time (sub-account concept) similar to the audit (IHasCreationTime)
Implementation of ideas
- Override ObjectStateManager_ObjectStateManagerChanged in the XXXXXXDbContext: AbpDbContext class
1 protected override void ObjectStateManager_ObjectStateManagerChanged(object sender, CollectionChangeEventArgs e) 2 { 3 var contextAdapter = (IObjectContextAdapter)this; 4 if (e.Action != CollectionChangeAction.Add) 5 { 6 return; 7 } 8 9 var entry = contextAdapter.ObjectContext.ObjectStateManager.GetObjectStateEntry(e.Element);10 switch (entry.State)11 {12 case EntityState.Added:13 CheckAndSetId(entry.Entity);14 CheckAndSetMustHaveTenantIdProperty(entry.Entity);15 SetCreationAuditProperties(entry.Entity, GetAuditUserId());16 SetChildAcountProperties(entry.Entity);17 break;18 }19 }
View Code
- The SetChildAcountProperties method is as follows:
/// <Summary> /// set the subaccount attribute field value /// </summary> /// <param name = "entityAsObj"> Object object to Obj </ param> protected void SetChildAcountProperties (object entityAsObj) {var entityWithChildAccount = entityAsObj as IChildAccount; if (entityWithChildAccount = null) {return;} var entity = entityAsObj. as <IChildAccount> (); if (! String. IsNullOrWhiteSpace (GetClaimValue (ClaimTypes. NameIdentifier) entity. BelongUserId = Convert. ToInt64 (GetClaimValue (ClaimTypes. NameIdentifier); if (! String. IsNullOrWhiteSpace (GetClaimValue (ClaimTypeExtensions. LayerCode) entity. UserLayerCode = GetClaimValue (ClaimTypeExtensions. LayerCode );}
- The GetClaimValue method is a method that is used only when the abpsession attribute is not obtained. In fact, according to the core of the abpsession expansion described by the blog Director, it is still obtained from the user Statement (Claims, the Code is as follows:
/// <Summary> /// obtain the declarative value /// </summary> /// <returns> </returns> protected string GetClaimValue (string claimType) {var claimsPrincipal = DefaultPrincipalAccessor. instance. principal; var claim = claimsPrincipal ?. Claims. FirstOrDefault (c => c. Type = claimType); return string. IsNullOrEmpty (claim ?. Value )? Null: claim. Value ;}
- Now the code function has been completed.
Summary
The article is your own understanding. It is still a long journey for reference only. You need to continue to study hard and learn a lot of good ideas and ideas !!!
Extension
Another "posture" point is that data filtering can be performed. There are many articles on custom expansion of abpDataFilter in the blog, so please take a look forward to the next update. Haha ~
Reference
The idea of abpsession expansion in this article comes from:
Author: St. Jie
Link: http://www.jianshu.com/p/930c10287e2a
Source: Simplified book
Copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.