The Magic Use of Abstract Constructor in Building Environment in ASP.NET MVC

Keywords: Attribute Session

First of all, we need to understand the problem of subclass inheritance from parent constructor calls.

Creating subclass instances through subclass parametric constructors defaults to calling parent parametric constructors
Create subclass instances through subclass parametric constructors, and also call parent parametric constructors by default
In the subclass constructor, the parent constructor is specified by the base keyword. When an instance is created by the subclass constructor, the specified parent constructor is invoked.
Common attributes of parent classes can be assigned by subclasses, and common attributes of parent classes can also be obtained by subclasses.

2. Can abstract classes have constructors?

We know that abstract classes cannot be instantiated. But can abstract classes have constructors? The answer is yes. The constructor of the abstract class is used to initialize some fields of the abstract class, which happens before the derivative class of the abstract class is instantiated. Not only that, the constructor of a stripped class has another ingenious application: it implements the code that subclasses must execute within it.

Although abstract classes cannot be instantiated, they can have constructors. Since the constructor of the abstract class occurs before instantiating the derived class, it is possible to initialize the abstract class field or execute other code related to the subclass at this stage.

III. Scene Use

When ASP.NET MVC environment is built, our business layer calls the data session layer (for decoupling), but we encapsulate the code when the business layer calls (adding, deleting and modifying, we need to specify which business class in EF is specified by the subclass inheriting the abstract class, then we can use constructors to solve this problem).

1. Abstract class code

using CZBK.ItcastOA.DALFactory;
using CZBK.ItcastOA.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CZBK.ItcastOA.BLL
{
   public abstract class BaseService<T> where T:class,new()
    {
       public IDBSession CurrentDBSession
       {
           get
           {
              // return new DBSession();// temporary
               return DBSessionFactory.CreateDBSession();
           }
       }
       public IDAL.IBaseDal<T> CurrentDal { get; set; }
       public abstract void SetCurrentDal();
       public BaseService()
       {
           SetCurrentDal();//Subclasses must implement Abstract methods.
       }
       public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
       {
         
           return CurrentDal.LoadEntities(whereLambda);
       }

       public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc)
       {
           return CurrentDal.LoadPageEntities<s>(pageIndex, pageSize, out totalCount, whereLambda, orderbyLambda, isAsc);
       }
        /// <summary>
        /// delete
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public bool DeleteEntity(T entity)
       {
           CurrentDal.DeleteEntity(entity);
           return CurrentDBSession.SaveChanges();
       }
        /// <summary>
        /// update
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public bool EditEntity(T entity)
       {
           CurrentDal.EditEntity(entity);
           return CurrentDBSession.SaveChanges();
       }
        /// <summary>
        /// Add data
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public T AddEntity(T entity)
       {
           CurrentDal.AddEntity(entity);
           CurrentDBSession.SaveChanges();
           return entity;
       }
    }
}

2. Implementation of subclass inheritance code

public partial class UserInfoService : BaseService<UserInfo>,IUserInfoService
    {

        public override void SetCurrentDal()
        {
            CurrentDal = this.CurrentDBSession.UserInfoDal;
        }
}

Four, summary

The CurrentDal attribute in the abstract class needs to be specified, and then the subclass overrides the SetCurrentDal method in the parent class, when the constructor in the parent class calls, the overridden SetCurrentDal method is called. Assign a value to the CurrentDal attribute. (In the wrong place, please give more advice)

Posted by marsooka on Sat, 11 May 2019 10:05:27 -0700