C#简单工厂模式学习
刚学习设计模式,还不是太了解,感觉只有多数据库的情况下才用的到,待学习
首先创建空白解决方案,依次创建类库Model,IDAL,SqlServerDAL,DALFactory,BLL,DBUtility,并创建一个窗体程序
首先在窗体程序的App.Config中添加以下设置
<appSettings>
//指定DAL调用类型,DALFactory中使用
<add key="DAL" value=" Nothwind.SqlServerDAL"/>
</appSettings> <connectionStrings>
<add name="con" connectionString="Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=*******;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
</connectionStrings>
在DBUtility中添加助手函数,这里只为了读取连接字符串
namespace Nothwind.DBUtility
{
public class SqlHelper
{
public static string connStr
{
get { return ConfigurationManager.ConnectionStrings["con"].ConnectionString; }
}
}
}
在Nothwind数据库中创建一张表Studen,为了学习及演示方便只创建2个字段Id Int ,Name nvarchar(50),并根据数据库结构创建模型类
namespace Nothwind.Model
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
}
创建接口类IStudent.cs,IDAL需要添加引用Model类
namespace Nothwind.IDAL
{
public interface IStudent
{
List<Model.Student> GetStudents(); //读取所有Student
Model.Student GetStudentById(int id); //根据Id返回单个Student
}
}
在SqlServerDAL中创建接口实现类Student.cs,SqlServerDAL需要添加DBUtility,IStuden,Model三个项目引用,因为需要读取数据库所以NuGet中安装Dapper
namespace Nothwind.SqlServerDAL
{
public class Student : IDAL.IStudent
{
//根据Id返回单个Student
public Model.Student GetStudentById(int id)
{
using (IDbConnection conn = new SqlConnection(DBUtility.SqlHelper.connStr))
{
string sql = "SELECT Id,Name FROM Student WHERE Id=@Id";
DynamicParameters parameters = new DynamicParameters();
parameters.Add("Id", id);
Model.Student students = conn.Query<Model.Student>(sql, parameters).FirstOrDefault();
return students;
}
}
//返回所有Student
public List<Model.Student> GetStudents()
{
using (IDbConnection conn = new SqlConnection(DBUtility.SqlHelper.connStr))
{
string sql = "SELECT Id,Name FROM Student";
IEnumerable<Model.Student> students = conn.Query<Model.Student>(sql);
return students.ToList();
}
}
}
}
在DALFactory类库中添加DataAccess.cs,为了根据配置文件选择不同的数据库,创建DALFactory。返回程序集的指定类的实例。需要引用IDAL,DBUtility
namespace Nothwind.DALFactory
{
public class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings["DAL"]; public static IDAL.IStudent CreateStudent()
{
object objectType = Assembly.Load(path).CreateInstance(path + ".Student");
return (IDAL.IStudent)objectType;
}
}
}
BLL类库中添加Student.cs,并添加引用Model,IDAL,DALFactory
namespace Nothwind.BLL
{
public class Student
{
public static Model.Student GetStudentById(int id)
{
IDAL.IStudent student = DALFactory.DataAccess.CreateStudent();
return student.GetStudentById(id);
} public static List<Model.Student> GetStudents()
{
IDAL.IStudent student = DALFactory.DataAccess.CreateStudent();
return student.GetStudents();
}
}
}
在窗体程序中添加一个button及dataGridView,添加以下代码
private void button1_Click(object sender, EventArgs e)
{
List<Model.Student> student = new List<Model.Student>();
student = BLL.Student.GetStudents();
dataGridView1.DataSource = student;
}
效果展示

C#简单工厂模式学习的更多相关文章
- 【2016-10-17】【坚持学习】【Day8】【简单工厂模式】
今天学习简单工厂模式, 结构 一个抽象产品 多个具体产品 一个工厂类,通过传入参数,new出不同的产品 代码: abstract class Product { //所有产品类的公共业务方法 publ ...
- Java设计模式学习记录-简单工厂模式、工厂方法模式
前言 之前介绍了设计模式的原则和分类等概述.今天开启设计模式的学习,首先要介绍的就是工厂模式,在介绍工厂模式前会先介绍一下简单工厂模式,这样由浅入深来介绍. 简单工厂模式 做法:创建一个工厂(方法或类 ...
- 再起航,我的学习笔记之JavaScript设计模式05(简单工厂模式)
我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 前几 ...
- Java设计模式学习笔记(二) 简单工厂模式
前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 正文开始... 1. 简介 简单工厂模式不属于GoF23中设计模式之一,但在软件开发中应用也较为 ...
- 学习设计模式第二十七 - GoF之外简单工厂模式
示例代码来自<深入浅出设计模式>和<大话设计模式> 概述 简单工厂模式又被称为静态工厂模式,属于类的创建型模式.其实质是由一个工厂类根据传入的参量,动态决定应该创建出哪一个产品 ...
- C#简单工厂模式(学习Learning hard讲解笔记)
原味地址http://www.cnblogs.com/zhili/p/SimpleFactory.html 简单工厂模式通俗的理解就是用户与工厂的关系,用户用的东西,工厂来生成,责任明确. 就像大神展 ...
- C#设计模式学习笔记:简单工厂模式(工厂方法模式前奏篇)
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7551373.html,记录一下学习过程以备后续查用. 一.引言 简单工厂模式并不属于GoF23里面的设计模式 ...
- C++简单工厂模式的学习
我们先从最常见的C++类的一个实现开始说起, class API { public: virtual test(std::string s)=0; protected: API(){}; }; cla ...
- 设计模式(二)简单工厂模式(Simple Factory Pattern)
一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理解的模式——简单工厂模式. 二.简单工厂 ...
随机推荐
- ORACLE ORA-00933: SQL 命令未正确结束,
这个错误害我花了一天时间排查,最后原来是因为结束符,这种语句不能是分号,将分号即可执行成功. MERGE INTO MO_TRADE_COUNT_DAY A USING ( SELECT MAX(fl ...
- Azure安装完postgresql遇到:psql: error: could not connect to server: FATAL: no pg_hba.conf entry for host
进入创建好的Azure Database for PostgreSQL server 点击connection security 在Firewall rules中 Add 0.0.0.0-255.2 ...
- Android开发没有一技之长就废了吗?
写在前面的话 不知你发现没有,人到中年之后,时间流逝的速度仿佛越来越快? 还记得毕业那会儿,我们怀着新鲜和好奇踏进了职场,那个时候每天都是满满的挑战和需要学习的东西,感觉时间过得真慢啊: 不知道从什么 ...
- SpringCloud升级之路2020.0.x版-12.UnderTow 简介与内部原理
本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 在我们的项目中,我 ...
- Git-09-常用命令
git常用命令 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 ...
- pikachu CSRF
CSRF简介 CSRF 是 Cross Site Request Forgery 的 简称,中文名为跨域请求伪造在CSRF的攻击场景中,攻击者会伪造一个请求(一般是一个链接)然后欺骗目标用户进行点击, ...
- NOIP 模拟 $28\; \rm 割海成路之日$
题解 \(by\;zj\varphi\) 用两个集合分别表示 \(1\) 边联通块,\(1,2\) 边联通块 . \(\rm son_x\) 表示当前节点通过 \(3\) 类边能到的 \(2\) 联通 ...
- Charles 抓包 Client SSL handshake failed - Remote host closed connection during handshake
Charles 抓包 https 报错: Client SSL handshake failed - Remote host closed connection during handshake # ...
- MySQL 数据库、数据表、数据的基本操作
1.数据库(database)管理 1.1 create 创建数据库 create database firstDB; 1.2 show 查看所有数据库 mysql> show database ...
- .net core signalR 全局异常处理
Hub的异常拦截 { } { } *:first-child { } *:last-child { } { } { } { } { } { } { } { } { } { } h6:first-chi ...