• 简单抽象工厂-继简单工厂改善


    昨天简单阐述了简单工厂的编程模式,他实现了简单三层中业务逻辑层与数据层的解耦和业务逻辑层与表现层的解耦,使数据访问层的变化不会影响到业务逻辑层,业务逻辑层的变化不回影响到表现层,使程序更加的灵活。但简单工厂类中创建的对象是写死的,也具有局限性,所以引入抽象工厂,只需要通过修改配置文件的信息和反射,从而在业务逻辑层中创建不同的DAL对象,实现低耦合,高内聚的编程思想。

    以下是相关代码实现:

    在配置文件<configuration></configuration>节点中添加

    1   <appSettings>
    2     <!--程序集-->
    3     <add key="DalAssemblyPath" value="Test.SqlServerDAL"/>
    4     <!--命名空间-->
    5     <add key="NameSpace" value="Test.SqlServerDAL"/>
    6   </appSettings>

    之后,新建一个抽象工厂类,将配置文件中的程序集和命名空间获取到,利用反射Assembly.Load()方法加载程序集,再使用CreateIntance()方法创建实例。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Configuration;
     4 using System.Linq;
     5 using System.Text;
     6 using IDAL;
     7 using System.Reflection;
     8 namespace StudentFactoryDAL
     9 {
    10     //抽象工厂类
    11     public class AbstractFactory
    12     {
    13         private readonly static string dalAssemblyPath = ConfigurationManager.AppSettings["DalAssemblyPath"];//根据配置文件读取程序集
    14         private readonly static string nameSpace = ConfigurationManager.AppSettings["NameSpace"];//根据配置文件读取命名空间
    15         /// <summary>
    16         /// 用反射创建实例对象
    17         /// </summary>
    18         /// <returns></returns>
    19         public static IStudentDAL CreateStudentDal()
    20         {
    21             string fullclass = nameSpace + ".StudentDAL";
    22             return CreateIntance(fullclass, nameSpace) as IStudentDAL;
    23         }
    24         /// <summary>
    25         /// 根据类创建实例
    26         /// </summary>
    27         /// <param name="classname">命名空间+类名</param>
    28         /// <param name="assemblypath">程序集</param>
    29         /// <returns></returns>
    30         public static object CreateIntance(string classname, string assemblypath)
    31         {
    32             var assembly = Assembly.Load(assemblypath);//加载程序集
    33             object Intance = assembly.CreateInstance(classname);//根据命名空间+类名创建类的实例
    34             return Intance;
    35         }
    36 
    37     }
    38 }
    View Code

    最后,在相关数据访问层使用抽象工厂类创建DAL对象即可。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using Test.Model;
     6 
     7 namespace Test.BLL
     8 {
     9     public class StudentBLL:IBLL.IStudentBLL
    10     {
    11 
    12         //IDAL.IStudentDAL dal = StudentFactoryDAL.FactoryDAL.CreateStudentDAL();
    13         //利用抽象工厂创建DAL对象,进行操作        
    14         IDAL.IStudentDAL dal = StudentFactoryDAL.AbstractFactory.CreateStudentDal();
    15         public List<Student> GetAllStudent()
    16         {
    17             return dal.GetAllStudent();
    18         }
    19     }
    20 }
    View Code

    这样,修改数据库中的数据访问层(比如:换个数据库访问层),只需要将修改的数据访问层的程序集和命名空间在配置文件中修改即可。

  • 相关阅读:
    js Touch事件(向左滑动,后退)
    浏览器滚动条到底部自动加载更多
    如何终止terminal后台进程
    DFT介绍
    GDS area estimation
    Web Scraping with Python第二章
    Web Scraping with Python第一章
    python技巧汇总
    centos6.4安装zsh与dircolors
    linux解压命令汇总
  • 原文地址:https://www.cnblogs.com/JokerYang/p/3424034.html
Copyright © 2020-2023  润新知