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;
}

 效果展示

项目文件

posted @ 2020-05-14 10:52  liessay  阅读(417)  评论(0编辑  收藏  举报