ASP.Net MVC OA project notes

Posted by Marchingknight11 on Mon, 16 Dec 2019 15:54:32 +0100

1.1.1 Abstract Factory encapsulates the creation of data operation class instance, and then DBSession calls abstract factory to modify DBSession

CZBK.ItcastOA.DALFactory data session layer calls data layer can not be directly new, you need to encapsulate decoupling

 

1.2.1 CZBK.ItcastOA.DALFactory add abstract factory class AbstractFactory

1.2.2 configure the configuration information (assembly and namespace of data layer) in web.config

<! -- configure assembly name and namespace name -- >
<add key="AssemblyPath" value="CZBK.ItcastOA.DAL" />
<add key="NameSpace" value="CZBK.ItcastOA.DAL" />

1.2.3 introduce System.configuration because you want to read configuration information

1.2.4 abstract factory class AbstractFactory code

  /// <summary>
    /// Create an instance of the class as a reflection
    /// </summary>
    public class AbstractFactory
    {
        //Read the configured assembly and namespace
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["AssemblyPath"];
        private static readonly string NameSpace = ConfigurationManager.AppSettings["NameSpace"];

        public static IUserInfoDal CreateUserInfoDal()
        {
            string fullClassName = NameSpace + ".UserInfoDal";
            return CreateInStance(fullClassName) as IUserInfoDal;//Encapsulate reflection in this method
        }
        private static object CreateInStance(string className)
        {
            //Load assembly
            var assembly = Assembly.Load(AssemblyPath);
            return assembly.CreateInstance(className);
        }
    }

1.2.5 encapsulates the creation of instances of a class through an abstract factory

1.2.6 in this way, it is completely decoupled. In the future, no matter what DAL is changed, just change the configuration file

1.3.1 each session layer is called through an interface, so the data session layer also needs an interface

Interface oriented programming, decoupled in this way

Add an interface IDBSession

First, IDA should also refer to EF by adding entity data model reference

 /// <summary>
    /// Business layer calls the interface of data session layer
    /// </summary>
    public interface IDBSession
    {
        DbContext Db { get; }
        IUserInfoDal UserInfoDal { get; set; }
        bool SaveChanges();

    }

1.3.2 let DBSession inherit IDBSession

 

 /// <summary>
    /// 1.Data session layer: it is a factory class that is responsible for the creation of all data operation class instances, and then the business layer obtains the instances of the data classes to be operated through the data session layer,
    /// So data session layer decouples business layer and data layer.
    /// 2.A method is provided in the data session layer to save all data.
    /// </summary>
    public class DBSession:IDBSession
    {
        //OAEntities Db = new OAEntities(); 
        public DbContext Db
        {
            get
            {
                return DBContextFactory.CreateDbContext();
            }
        }

        private IUserInfoDal _UserInfoDal;
        public IUserInfoDal UserInfoDal
        {
            get
            {
                if(_UserInfoDal==null)
                {
                    //_UserInfoDal= new UserInfoDal();
                    //It encapsulates the creation of class instance through abstract factory
                    _UserInfoDal = AbstractFactory.CreateUserInfoDal();
                }
                return _UserInfoDal;
            }
            set
            {
                _UserInfoDal = value;
            }
        }


        /// <summary>
        /// A business often involves the operation of multiple tables. We hope to connect the database once to complete the operation of the table data and improve the performance
        /// Work unit mode
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
           return Db.SaveChanges() > 0;
        }
    }

Topics: ASP.NET Session Programming Database