Extending IHasCreation Time based on ABP

Preface

Because the company's projects are based on the framework of abp to achieve functions, recently the project is being adjusted, so we want to implement the corresponding functions of Module-Zero. After all, Module-Zero is charged (you know), today is to achieve similar audit (IHasCreation Time, etc.) automatically add creators, create time function (sub-account concept).

thinking

  1. First of all, I think about the new method of rewriting ABP about warehousing. There are a lot of codes, and I haven't done much research on the source code of abp, so I give up.

  2. Manual assignment of Id to account number each time an entity is instantiated. Trouble, give up

  3. Referring to abp's idea of automatically adding user Id to the IHasCreation Time audit function, the method of ObjectStateManager_ObjectStateManagerChanged defined by AbpDbContext is overridden in the class inherited from AbpDbContext (data context). Then a protected SetChildAcountProperties method user is declared to assign values to the corresponding fields of his account.

problem

  1. I wanted to declare an Abpsession attribute based on IAbpsession extension directly in the class XXXXDbContext: AbpDbContext, but I found that it could not be written like this. For an article on expanding abpsession, see Connect Blog Garden [St. Jay] ABP Introduction Series (10) - Extending AbpSession Later, other methods are used to obtain the corresponding values, which will be explained in the following code. Ask you why the gods in XXXXXXDbContext can not obtain the AbpSession value after the expansion as follows

    public new IAbpSessionExtensions AbpSession { get; set; } 

     

  2. Our sub-account is to get the UserId of abpsession. I wonder if we can use the Users.Find(AbpSession.UserId) method to find the other fields I need in the data context after acquiring the UserId of abpsession. I find that it can but cause a problem code elsewhere as follows, prompting "the error of not setting the object to the reference"
    _xxxxxxRepository.Insert(model);

Realization

  • Rewrite XXXXXXDbContext: ObjectStateManager_ObjectStateManagerChanged in the 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         }
  • The SetChildAcountProperties method is as follows:
      /// <summary>
        ///   Set the value of the subaccount property field
        /// </summary>
        /// <param name="entityAsObj">Entity objects are set 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 not a compromise in expanding the properties of abpsession. In fact, the core of the abpsession expansion according to the blog God is obtained from the Claims. The code is as follows:
        /// <summary>
        ///   Get the declaration 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;
        }

 

  • So far, the function of the code has been completed, there are pictures and facts.

summary

The article is my own understanding, only for reference abp road is still long, need to continue to study hard, many good ideas, ideas need to learn!!!

 

extend

There is another "posture" point that can be used for data filtering. There are many articles about custom expansion of abpDataFilter in blog park that can be followed. Look forward to the next update, haha.~

Reference resources

The idea of abpsession expansion in this article comes from:

Author: St.

Link: http://www.jianshu.com/p/930c10287e2a

Source: Brief Book
Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Keywords: ASP.NET Attribute

Added by guitarlass on Wed, 12 Jun 2019 02:56:38 +0300