在ASP.NET MVC中搭建环境时抽象类构造函数的妙用

一、首先了解下子类继承父类构造函数调用的问题

● 通过子类无参构造函数创建子类实例,会默认调用父类的无参构造函数
● 通过子类有参构造函数创建子类实例,也会默认调用父类的无参构造函数
● 在子类构造函数中通过base关键字指明父类构造函数,当通过子类构造函数创建实例,会调用指明的、父类的构造函数
● 父类的公共属性可以通过子类来赋值,子类也可以获取到父类的公共属性

二、抽象类能否有构造函数?

我们知道,抽象类是不能被实例化的。但抽象类是否可以有构造函数?答案是可以有。抽象类的构造函数用来初始化抽象类的一些字段,而这一切都在抽象类的派生类实例化之前发生。不仅如此,抽线类的构造函数还有一种巧妙应用:就是在其内部实现子类必须执行的代码。

虽然抽象类不能被实例化,但可以有构造函数。由于抽象类的构造函数在实例化派生类之前发生,所以,可以在这个阶段初始化抽象类字段或执行其它与子类相关的代码。

三、场景运用

在ASP.NET MVC环境搭建的时候,我们业务层调用数据会话层(为了解耦),但是我们在业务层调用的时候封装代码(增删改查,需要指定是哪个EF中的那个业务类,这个工作由继承该抽象类的子类去指定,那么这个时候就可以运用构造函数解决)

1、抽象类代码

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();//暂时
               return DBSessionFactory.CreateDBSession();
           }
       }
       public IDAL.IBaseDal<T> CurrentDal { get; set; }
       public abstract void SetCurrentDal();
       public BaseService()
       {
           SetCurrentDal();//子类一定要实现抽象方法。
       }
       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>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public bool DeleteEntity(T entity)
       {
           CurrentDal.DeleteEntity(entity);
           return CurrentDBSession.SaveChanges();
       }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public bool EditEntity(T entity)
       {
           CurrentDal.EditEntity(entity);
           return CurrentDBSession.SaveChanges();
       }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
       public T AddEntity(T entity)
       {
           CurrentDal.AddEntity(entity);
           CurrentDBSession.SaveChanges();
           return entity;
       }
    }
}

2、子类继承代码实现

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

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

四、总结

抽象类中的CurrentDal属性需要被指定,然后子类重写父类中的SetCurrentDal方法,这时候父类中的构造函数调用时,会调用重写的SetCurrentDal方法。给CurrentDal属性赋值。(错误的地方请多多指教)

posted @ 2018-06-27 09:42  HPUGIS  阅读(388)  评论(0编辑  收藏  举报