关闭

简单工厂模式

933人阅读 评论(0) 收藏 举报
分类:


设计模式:简单工厂模式


演变:

  1>简单三层:2>简单工厂 3>抽象工厂模式

1简单三层


DAL层

BaseDbContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using System.Data.Entity; //还需要在DAL中引入EntityFramework文件
    public class BaseDbContext:DbContext
    {
        public BaseDbContext()
            : base("name=salesEntities")
        { 
        
        }
    }
}

baseDal.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace DAL
{
    /// <summary>
    /// 操作增,删,查,改的通用方法
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseDal<TEntity> where TEntity:class
    {
        //实例化EF上下文对象
        BaseDbContext db = new BaseDbContext();

        protected DbSet<TEntity> _dbSet;

        public BaseDal()
        {
            _dbSet=db.Set<TEntity>();
        }

        /// <summary>
        /// 获取所有实体集合
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where) 
        {
            return _dbSet.Where(where).ToList();
        }
    }
}


T_UserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using Model;
    public class T_UserInfoDal : BaseDal<T_UserInfo>
    {
        //属于T_UserInfo表特有的方法(连表操作)TableName参数是T_UserInfo表要连接表的表名
        public List<T_UserInfo> QueryJoin(string TableName)
        {
            //返回连表查询后的集合(如果有需要我们我们也做一个条件筛选)
            return base._dbSet.Include(TableName).ToList();
        }
    }
}

T_UserLoginDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using Model;
    public class T_UserLoginDal:BaseDal<T_UserLoginDal>
    {
    }
}

BLL层

BaseBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace BLL
{
    using DAL;

    public class BaseBll<TEntity> where TEntity : class
    {
        protected BaseDal<TEntity> basebll; //这里声明一个basedal变量。在子类中实例化它

        public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where)
        {
            return basebll.QueryWhere(where);
        }
    }
}

T_UserInfoBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.BLL
{
    using Model;

    using DAL;
    public class T_UserInfoBll : BaseBll<T_UserInfo>
    {
        T_UserInfoDal dal; //在BLL层定义一个DAL的变量,并在BLL层的构造函数中实例化它,这样在UI层中new BLL层的对象的时候顺便就为BLL层创建了DAL的对象,这就可以在BLL中来调用DAL层中的方法了。

        public T_UserInfoBll()
        {
            dal = new T_UserInfoDal(); //这里为什么要这么写?那是因为在UI层中T_UserInfoBll bll = new T_UserInfoBll();的时候 就会执行T_UserInfoBll类的构造函数,这样就会也就同时创建了T_UserInfoDal的对象,并同时将T_UserInfoBll类的父类对象初始化了。【注意点:当我们在UI层中T_UserInfoBll bll = new T_UserInfoBll()的时候,这个bll对象就可以调用父类的QueryWhere()方法和它自己的JoinQuery()方法】
            

            base.basedal = dal; //实例化父类变量

            //base.basedal = new T_UserInfoDal(); 如果这样简化的话是不行的。因为basedal是父类的对象,它里面并没有QueryJoin方法。所以在下面的QueryJoin方法中用basedal就调不出DAL层中的JoinQuery方法来。所以只有用dal才能调用出QueryJoin方法来
        }

        public List<T_UserInfo> QueryJoin(string tableName)
        {
            List<T_UserInfo> list = dal.JoinQuery("T_UserLogin");
            return list;
        }
    }
}

T_UserLoginBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    using Model;
    public class T_UserLoginBll:BaseBll<T_UserLogin>
    {

    }
}

UI层 (MvcApp)

HomeController.cs

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApp.Controllers
{
    using BLL;
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {
            T_UserInfoBll bll = new T_UserInfoBll();
            
            //----测试1(条件查询)
            T_UserInfo user = bll.QueryWhere(r => r.Name == "凡斌").FirstOrDefault();


            //----测试2 (连表查询)
            var a = bll.QueryJoin("Model.T_UserLogin").FirstOrDefault();

            var b = a.T_UserLogin.UserName;
           ;
            return View();
        }

    }
}

简单工厂模式 (由简单三层演变)

 

DAL文件夹


DAL层


BaseDal.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using IDAL;

namespace DAL
{
    /// <summary>
    /// 操作增,删,查,改的通用方法
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseDal<TEntity> :IBaseDal<TEntity> where TEntity:class
    {
        //实例化EF上下文对象
        BaseDbContext db = new BaseDbContext();

        protected DbSet<TEntity> _dbSet;

        public BaseDal()
        {
            _dbSet=db.Set<TEntity>();
        }

        /// <summary>
        /// 获取所有实体集合
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where) 
        {
            return _dbSet.Where(where).ToList();
        }
    }
}

BaseDbContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using System.Data.Entity;
    public class BaseDbContext:DbContext
    {
        public BaseDbContext()
            : base("name=salesEntities")
        { 
        
        }
    }
}

T_UserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using Model;
    using IDAL;
    public class T_UserInfoDal : BaseDal<T_UserInfo>,IT_UserInfoDal
    {
        //属于T_UserInfo表特有的方法(连表操作)TableName参数是T_UserInfo表要连接表的表名
        public List<T_UserInfo> QueryJoin(string TableName)
        {
            //返回连表查询后的集合(如果有需要我们我们也做一个条件筛选)
            return base._dbSet.Include(TableName).ToList();
        }
    }
}

T_UserLoginDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using Model;
    using IDAL;
    public class T_UserLoginDal:BaseDal<T_UserLogin>,IT_UserLoginDal
    {
    }
}

IDAL


IBaseDal.cs

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace IDAL
{
    public interface IBaseDal<TEntity> where TEntity : class //指定这个TEntity是一个类
    {
        List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where);
    }
}

IT_UserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDAL
{
    using Model;
    public interface IT_UserInfoDal:IBaseDal<T_UserInfo>
    {
        List<T_UserInfo> QueryJoin(string TableName);
    }
}

IT_UserLoginDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDAL
{
    using Model;
    public interface IT_UserLoginDal : IBaseDal<T_UserLogin>
    {
    }
}

DalFactory(Dal层的简单工厂类库)


FactoryDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DalFactory
{
    using IDAL;
    using DAL;
    using System.Reflection;
    /// <summary>
    /// 简单工厂(负责创建Dal层的所有类的对象实例,以其接口的方式放回)
    /// </summary>
    public class FactoryDal
    {
        /// <summary>
        /// 创建T_UserInfoDal类对象,并以接口的形式返回
        /// </summary>
        /// <returns></returns>
        public static IT_UserInfoDal CreateT_UserInfoDalInstance()
        {
            return CreateObject("T_UserInfoDal") as IT_UserInfoDal;
        }
        /// <summary>
        /// 创建一个T_UserLoginDal对象,并以接口的形式返回
        /// </summary>
        /// <returns></returns>
        public static IT_UserLoginDal CreateT_UserLoginDalInstance()
        {
            return CreateObject("T_UserLoginDal") as IT_UserLoginDal;
        }


        /// <summary>
        /// (封装)根据类名创建对象实例
        /// </summary>
        /// <param name="className">需要创建对象的类名</param>
        /// <returns></returns>
        static object CreateObject(string className)
        {
            //获取程序集名称(程序集名称是自己约定的,具体叫什么就在web.config文件中配置)
            string assName = System.Configuration.ConfigurationManager.AppSettings["dal"];
            if (string.IsNullOrEmpty(assName))
            {
                throw new Exception("dal配置节点不存在,请配置");
            }

            //将指定的程序集加载到内存中           
            Assembly ass = Assembly.Load(assName);

            //创建T_UserInfoDal类的实例对象(CreateInstance返回的对象类型是object类型,具体用它的时候需要转换类型)
            object obj = ass.CreateInstance(assName + "." + className);
            return obj;
        }
    }
}

BLL文件夹

Bll层


BaseBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace BLL
{
    using IBLL;
    using IDAL;

    public class BaseBll<TEntity>:IBaseBll<TEntity> where TEntity : class
    {
        protected IBaseDal<TEntity> basedal;
        public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where)
        {
            return basedal.QueryWhere(where);
        }
    }
}

T_UserInfoBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    using IBLL;
    using IDAL;
    using Model;
    using DalFactory;
    public class T_UserInfoBll : BaseBll<T_UserInfo>, IT_UserInfoBll
    {

        IT_UserInfoDal idal;

        /// <summary>
        /// 构造函数:实例化接口成员
        /// </summary>
        public T_UserInfoBll()
        {
            idal = FactoryDal.CreateT_UserInfoDalInstance();
            base.basedal = idal;
        }

        //属于T_UserInfo这个表特有的方法
        public List<T_UserInfo> QueryJoin(string TableName)
        {
            List<T_UserInfo> list = idal.QueryJoin("T_UserLogin");
            return list;
        }
    }
}

T_UserLoginBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    using Model;
    using IBLL;
    public class T_UserLoginBll:BaseBll<T_UserLogin>,IT_UserLoginBll
    {

    }
}

IBLL 接口层

IBaseBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    using Model;
    using System.Linq.Expressions;
    public interface IBaseBll<TEntity> where TEntity : class
    {
        List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where);
    }
}

IT_UserInfoBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    using Model;
    public interface IT_UserInfoBll:IBaseBll<T_UserInfo>  
    {
        List<T_UserInfo> QueryJoin(string TableName);
    }
}

IT_UserLoginBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    using Model;
    public interface IT_UserLoginBll : IBaseBll<T_UserLogin>
    {
    }
}

BllFactory(BLL层的简单工厂类库)


FactroyBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BllFactory
{
    using IBLL;
    using System.Reflection;
    public class FactroyBll
    {
        static object CreateObject(string className)
        {
            //获取程序集名称(程序集名称是自己约定的,具体叫什么就在web.config文件中配置)
            string assName = System.Configuration.ConfigurationManager.AppSettings["bll"];
            if (string.IsNullOrEmpty(assName))
            {
                throw new Exception("bll配置节点不存在,请配置");
            }
            //将指定的程序集加载到内存中           
            Assembly ass = Assembly.Load(assName);
           
            //创建指定类名的对象实例(根据类型的完全限定名来创建的)
            return ass.CreateInstance(assName + "." + className);
        }
        public static IT_UserInfoBll CreateT_UserInfoBllInstance()
        {
            return CreateObject("T_UserInfoBll") as IT_UserInfoBll;
        }

        public static IT_UserLoginBll CarateT_UserLoginInstance()
        {
            return CreateObject("T_UserLogin") as IT_UserLoginBll;
        }


    }
}


UI层(MvcApp)

Home 控制器

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApp.Controllers
{
    using IBLL;
    using Model;
    using BllFactory;
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            //通过工厂创建T_UserInfoBll类的对象实例(工厂以接口的形式返回的)
            IT_UserInfoBll bll = FactroyBll.CreateT_UserInfoBllInstance();

            //----测试1(条件查询)
            T_UserInfo user= bll.QueryWhere(r => r.Name == "周晶").FirstOrDefault();


            //----测试2 (连表查询)
            var a = bll.QueryJoin("Model.T_UserLogin").LastOrDefault();

            var b = a.T_UserLogin.UserName;
            ;
            return View();
        }

    }
}

web.config配置文件

 <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <!--工厂模式:约定:通过反射获取类的完全限定名的名称空间在这里配置-->
    <add key="bll" value="BLL"/>
    <add key="dal" value="DAL"/>
  </appSettings>

特别注意:

//将指定的程序集加载到内存中           
Assembly ass = Assembly.Load(assName);  ----执行这里的时候会报下面那条错误(这里是工厂模式根从配置文件中获取的名称空间加载程序集到内存中)

BLL层生成解决方案的时候,需要将bll类库生成生成到UI层下面,具体生成到UI层bin目录下,还是bin目录下的Debug目录下,这要看报错的详细信息,看日志: 尝试下载新的 URL具体从哪里下载 (否则在通过BLL层的工厂模式创建bll层类对象的时候就会报:未能加载文件或程序集“BLL或它的某一个依赖项,系统找不到指定的文件”)


比如:我们这报错的详细信息是这样的:





抽象工厂模式 (由简单工厂演变)

DAL文件夹

DAL类库

BaseDal.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using IDAL;

namespace DAL
{
    /// <summary>
    /// 操作增,删,查,改的通用方法
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseDal<TEntity> :IBaseDal<TEntity> where TEntity:class
    {
        //实例化EF上下文对象
        BaseDbContext db = new BaseDbContext();

        protected DbSet<TEntity> _dbSet;

        public BaseDal()
        {
            _dbSet=db.Set<TEntity>();
        }

        /// <summary>
        /// 获取所有实体集合
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where) 
        {
            return _dbSet.Where(where).ToList();
        }
    }
}

BaseDbContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using System.Data.Entity;
    public class BaseDbContext:DbContext
    {
        public BaseDbContext()
            : base("name=salesEntities")
        { 
        
        }
    }
}


T_UserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using Model;
    using IDAL;
    public class T_UserInfoDal : BaseDal<T_UserInfo>,IT_UserInfoDal
    {
        //属于T_UserInfo表特有的方法(连表操作)TableName参数是T_UserInfo表要连接表的表名
        public List<T_UserInfo> QueryJoin(string TableName)
        {
            //返回连表查询后的集合(如果有需要我们我们也做一个条件筛选)
            return base._dbSet.Include(TableName).ToList();
        }
    }
}


T_UserLoginDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    using Model;
    using IDAL;
    public class T_UserLoginDal:BaseDal<T_UserLogin>,IT_UserLoginDal
    {
    }
}

DAL类库的引用

IDAL类库

IBaseDal.cs

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace IDAL
{
    public interface IBaseDal<TEntity> where TEntity : class //指定这个TEntity是一个类
    {
        List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where);
    }
}


IT_UserInfoDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDAL
{
    using Model;
    public interface IT_UserInfoDal:IBaseDal<T_UserInfo>
    {
        List<T_UserInfo> QueryJoin(string TableName);
    }
}


IT_UserLoginDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDAL
{
    using Model;
    public interface IT_UserLoginDal : IBaseDal<T_UserLogin>
    {
    }
}

IDAL类库的引用


DalFactory DAL层业务工厂类库

FactoryDal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DalFactory
{
    using IDAL;
    using DAL;
    using System.Reflection;
    /// <summary>
    /// 简单工厂(负责创建Dal层的所有类的对象实例,以其接口的方式放回)
    /// </summary>
    public class FactoryDal
    {
        /// <summary>
        /// 创建T_UserInfoDal类对象,并以接口的形式返回
        /// </summary>
        /// <returns></returns>
        public static IT_UserInfoDal CreateT_UserInfoDalInstance()
        {
            return CreateObject("T_UserInfoDal") as IT_UserInfoDal;
        }
        /// <summary>
        /// 创建一个T_UserLoginDal对象,并以接口的形式返回
        /// </summary>
        /// <returns></returns>
        public static IT_UserLoginDal CreateT_UserLoginDalInstance()
        {
            return CreateObject("T_UserLoginDal") as IT_UserLoginDal;
        }


        /// <summary>
        /// (封装)根据类名创建对象实例
        /// </summary>
        /// <param name="className">需要创建对象的类名</param>
        /// <returns></returns>
        static object CreateObject(string className)
        {
            //获取程序集名称(程序集名称是自己约定的,具体叫什么就在web.config文件中配置)
            string assName = System.Configuration.ConfigurationManager.AppSettings["dal"];
            if (string.IsNullOrEmpty(assName))
            {
                throw new Exception("dal配置节点不存在,请配置");
            }

            //将指定的程序集加载到内存中           
            Assembly ass = Assembly.Load(assName);

            //创建T_UserInfoDal类的实例对象(CreateInstance返回的对象类型是object类型,具体用它的时候需要转换类型)
            object obj = ass.CreateInstance(assName + "." + className);
            return obj;
        }
    }
}

DalFactory DAL层业务工厂类库的引用


BLL文件夹

BLL类库

BaseBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace BLL
{
    using IBLL;
    using IDAL;
    using AbstractFactory;

    public class BaseBll<TEntity>:IBaseBll<TEntity> where TEntity : class
    {
        protected IBaseDal<TEntity> basedal;
        //protected AbstractFactory.AbsFactory abdal;
        public List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where)
        {
            return basedal.QueryWhere(where);
        }
    }
}


T_UserInfoBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    using IBLL;
    using IDAL;
    using Model;
    using DalFactory;
    public class T_UserInfoBll : BaseBll<T_UserInfo>, IT_UserInfoBll
    {

        IT_UserInfoDal dal;

        /// <summary>
        /// 构造函数:实例化接口成员
        /// </summary>
        public T_UserInfoBll()
        {
            dal = FactoryDal.CreateT_UserInfoDalInstance();
            base.basedal = dal;
        }

        //属于T_UserInfo这个表特有的方法
        public List<T_UserInfo> QueryJoin(string TableName)
        {
            List<T_UserInfo> list = dal.QueryJoin("T_UserLogin");
            return list;
        }
    }
}


T_UserLoginBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    using Model;
    using IBLL;
    public class T_UserLoginBll:BaseBll<T_UserLogin>,IT_UserLoginBll
    {

    }
}

BLL类库的引用


IBLL类库

IBaseBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    using Model;
    using System.Linq.Expressions;
    public interface IBaseBll<TEntity> where TEntity : class
    {
        List<TEntity> QueryWhere(Expression<Func<TEntity, bool>> where);
    }
}


IT_UserInfoBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    using Model;
    public interface IT_UserInfoBll:IBaseBll<T_UserInfo>  
    {
        List<T_UserInfo> QueryJoin(string TableName);
    }
}


IT_UserLoginBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    using Model;
    public interface IT_UserLoginBll : IBaseBll<T_UserLogin>
    {
    }
}

IBLL类库的引用


AbstractFactory抽象工厂类库

AbsFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactory
{
    using IBLL;
    using System.Reflection;
    /// <summary>
    /// 抽象工厂
    /// </summary>
    public abstract class AbsFactory
    {       
        public abstract IT_UserInfoBll CreateT_UserInfoBllInstance();
        public abstract IT_UserLoginBll CarateT_UserLoginInstance();
    }
}

OptAbsFactroy.cs (最后UI层就是使用它来调用抽象工厂的抽象类(实际是调用的类的实例对象))

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactory
{
    public class OptAbsFactroy
    {
        public static AbsFactory CreateObject()
        {
            //1.0 扫描当前运行目录下的bin目录下指定程序集
            Assembly ass = Assembly.Load("BllFactory");

            //2.0 创建类的对象实例
            object obj = ass.CreateInstance("BllFactory.FactroyBll");

            //以抽象类的形式返回
            return obj as AbsFactory;
        }
    }
}

AbstractFactory抽象工厂类库的引用

BllFactory BLL层的业务工厂类库

FactroyBll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BllFactory
{
    using IBLL;
    using AbstractFactory;
    using System.Reflection;
    public class FactroyBll : AbsFactory //这个业务工厂继承抽象工厂(UI层只跟抽象工厂打交道。抽象工厂负责调用业务工厂)
    {
        private static object CreateObject(string className)
        {
            //获取程序集名称(程序集名称是自己约定的,具体叫什么就在web.config文件中配置)
            string assName = System.Configuration.ConfigurationManager.AppSettings["bll"];
            if (string.IsNullOrEmpty(assName))
            {
                throw new Exception("bll配置节点不存在,请配置");
            }
            //将指定的程序集加载到内存中           
            Assembly ass = Assembly.Load(assName);

            //创建指定类名的对象实例(根据类型的完全限定名来创建的)
            return ass.CreateInstance(assName + "." + className);
        }
        public override IT_UserInfoBll CreateT_UserInfoBllInstance()
        {
            return CreateObject("T_UserInfoBll") as IT_UserInfoBll;
        }

        public override IT_UserLoginBll CarateT_UserLoginInstance()
        {
            return CreateObject("T_UserLogin") as IT_UserLoginBll;
        }


    }
}


BllFactory BLL层的业务工厂类库的引用

特别注意

BLL类库  DAL类库 BllFactory类库生成解决方案的时候需要将dll文件生成到UI层的bin目录下  BllFactory工厂类库 的生成文件需要生成到UI层中去


UI层 (MvcApp)

Home控制器

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApp.Controllers
{
    using IBLL;
    using Model;
    //using BllFactory;
    using AbstractFactory;
    public class HomeController : Controller
    {

        public ActionResult Index()
        {


            //通过抽象工厂来创建业务工厂的对象,业务工厂又创建类的对象,然后以抽象类型返回。
            IT_UserInfoBll bll = OptAbsFactroy.CreateObject().CreateT_UserInfoBllInstance();

            //----测试1(条件查询)
            T_UserInfo user = bll.QueryWhere(r => r.Name == "周晶").FirstOrDefault();


            //----测试2 (连表查询)
            var a = bll.QueryJoin("Model.T_UserLogin").LastOrDefault();

            var b = a.T_UserLogin.UserName;
            return View();
        }

    }
}


Web.config

  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <!--工厂模式:约定:通过反射获取类的完全限定名的名称空间在这里配置-->
    <add key="bll" value="BLL"/>
    <add key="dal" value="DAL"/>
  </appSettings>




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:802959次
    • 积分:12568
    • 等级:
    • 排名:第1267名
    • 原创:469篇
    • 转载:26篇
    • 译文:2篇
    • 评论:74条
    最新评论