zoukankan      html  css  js  c++  java
  • 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;
    }

     效果展示

    项目文件

  • 相关阅读:
    Java:day11
    Java:day10
    Java:day9
    Java:day8
    纯虚函数和抽象类
    C++的虚拟继承
    派生类构造函数、析构函数的定义和调用次序
    c++的继承方式——公有、保护、私有
    操作系统中系统调用的执行过程
    C++的类
  • 原文地址:https://www.cnblogs.com/liessay/p/12887104.html
Copyright © 2011-2022 走看看