刚学习设计模式,还不是太了解,感觉只有多数据库的情况下才用的到,待学习

首先创建空白解决方案,依次创建类库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#简单工厂模式学习的更多相关文章

  1. 【2016-10-17】【坚持学习】【Day8】【简单工厂模式】

    今天学习简单工厂模式, 结构 一个抽象产品 多个具体产品 一个工厂类,通过传入参数,new出不同的产品 代码: abstract class Product { //所有产品类的公共业务方法 publ ...

  2. Java设计模式学习记录-简单工厂模式、工厂方法模式

    前言 之前介绍了设计模式的原则和分类等概述.今天开启设计模式的学习,首先要介绍的就是工厂模式,在介绍工厂模式前会先介绍一下简单工厂模式,这样由浅入深来介绍. 简单工厂模式 做法:创建一个工厂(方法或类 ...

  3. 再起航,我的学习笔记之JavaScript设计模式05(简单工厂模式)

    我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 前几 ...

  4. Java设计模式学习笔记(二) 简单工厂模式

    前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 正文开始... 1. 简介 简单工厂模式不属于GoF23中设计模式之一,但在软件开发中应用也较为 ...

  5. 学习设计模式第二十七 - GoF之外简单工厂模式

    示例代码来自<深入浅出设计模式>和<大话设计模式> 概述 简单工厂模式又被称为静态工厂模式,属于类的创建型模式.其实质是由一个工厂类根据传入的参量,动态决定应该创建出哪一个产品 ...

  6. C#简单工厂模式(学习Learning hard讲解笔记)

    原味地址http://www.cnblogs.com/zhili/p/SimpleFactory.html 简单工厂模式通俗的理解就是用户与工厂的关系,用户用的东西,工厂来生成,责任明确. 就像大神展 ...

  7. C#设计模式学习笔记:简单工厂模式(工厂方法模式前奏篇)

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7551373.html,记录一下学习过程以备后续查用. 一.引言 简单工厂模式并不属于GoF23里面的设计模式 ...

  8. C++简单工厂模式的学习

    我们先从最常见的C++类的一个实现开始说起, class API { public: virtual test(std::string s)=0; protected: API(){}; }; cla ...

  9. 设计模式(二)简单工厂模式(Simple Factory Pattern)

    一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理解的模式——简单工厂模式. 二.简单工厂 ...

随机推荐

  1. noip模拟31[time·game·cover]

    noip模拟31 solutions 我就觉得这些考试题是越考越难,我是也越考越完蛋,已经完完全全的接近爆零了 只有20pts,说真的这还是我第一次挂掉30pts,本来我还有50pts嘞 所以这次考试 ...

  2. CentOS时间日期类语法

    目录 一.date时间日期类 1. date显示当前时间 2. date 显示非当前时间 3. date 设置系统时间 二.cal 查看日历 一.date时间日期类 date [OPTION]... ...

  3. 在游戏中播放cg视频遇到的问题

    遇到问题 我们线上手游要给港澳台用户增加cg视频,在我之前文章中已经讲到了我们是怎么在unity中播放cg的--><使用AVPro Video在Unity中播放开场视频(CG)笔记> ...

  4. Java8新特性(三)之方法引用和构造器引用

    1.使用场景 当要传递给Lambda体的操作,已经存在实现的方法了,就可以使用方法引用.(抽象方法的参数列表  必须与方法引用方法的参数列表保持一致) 2. 语法 使用操作符[::]将方法名和对象或类 ...

  5. mapboxgl 互联网地图纠偏插件(三)

    先说结论,结论当然是:大功告成,喜大普奔.看效果图: 好了,接下来说一下过程 先回顾一下这个系列的第一篇和第二篇 第一篇是直接改的 mapboxgl 源码,在源码里面对瓦片的位置进行纠偏,遇到的问题是 ...

  6. spring中的组合模式

    org.springframework.cache.support.CompositeCacheManager /* * Copyright 2002-2016 the original author ...

  7. WooYun虚拟机的搭建及配置方法

    "当时代需要时,他们勇敢地站了出来.当潮水褪去时,他们等待新的使命.他们等待被抛弃或者被怀念,等待这个世界告诉他们善恶和对错." 记录一下复活乌云的过程. 第一步:虚拟机下载 网上 ...

  8. ☕【Java技术指南】「Guava Collections」实战使用相关Guava不一般的集合框架

    Google Guava Collections 使用介绍 简介 Google Guava Collections 是一个对 Java Collections Framework 增强和扩展的一个开源 ...

  9. BUUCTF刷题系列(1)5.25日记

    前面的题目都不太难,就直接从SQL注入开始了. 这个样子的话,明显就是注入,我们先拿出SQL语句:http://fb415201-6634-4fc3-a6bc-a67b84ea1ed2.node3.b ...

  10. Intellj IDEA 光标显示insert状态解决办法

    使用idea过程中,不知道怎么回事,鼠标的光标老是insert状态,体验效果极其差劲,于是去百度,扒拉了好一阵,过滤了垃圾博客,发现了有两种方法可以解决此问题: 第一种方法: 在File------& ...