当前位置: 移动技术网 > IT编程>开发语言>.net > C#中使用MVC架构(一)

C#中使用MVC架构(一)

2020年07月17日  | 移动技术网IT编程  | 我要评论

C#中使用MVC架构(一)

  • 系统架构

系统分层:

1.视图层NewsManager:主要设计与用户交互的窗体,响应用户点击事件,调用BLL层业务.

2.业务逻辑层BLL:实现具体的业务逻辑,向视图层提供服务,通过调用DAL层,实现数据访问。

3.数据访问层DAL:链接数据源,实现数据访问,在读取时,把数据记录转换成模型对象,在更新时,把模型对象转成数据记录存储到数据库。

4.模型层MODEL:主要是向其它层提供数据模型,把对应数据表,按照orm模型封装成对应的C#类,实现数据表到C#类的映射,数据表中每条记录到类对象的映射。

 

  • 搭建框架

右击解决方案,添加新项目,选择C#类库,分别完成BLL、DAL、Model、视图层创建.

 

添加引用,BLL层需要引用DAL和model层,DAL层需要引用model层,视图层需要引用BLL和model层

 

 

  1. 数据准备

创建数据库及建表语句如下:

create database newsdbms;

use  newsdbms;

-- 删除用户表

drop table users;

-- 创建用户表

create table users(

id int primary key identity(1,10),

name varchar(20),

pwd  varchar(20)

);

-- 删除栏目表

drop table topics;

-- 创建栏目表

create table topics(

 id int primary key identity(1,1),

 name varchar(50),

 info varchar(100)

);

--删除新闻表

drop table news;

-- 创建新闻贴表

create table news(

id int primary key identity(1,1),

tid int,

title varchar(100),

author varchar(20),

create_date date,

pic_path varchar(250),

content  varchar(1000),

modify_date date,

summary  varchar(100)

);

--添加数据

insert into users(name,pwd) values('admin','123'),('sys','123'),('lili','123');

insert into topics(name,info) values('国内新闻','展示国内新闻'),('科技知识','展示科技知识');

insert into news(tid ,title,author,create_date ,pic_path ,content ,modify_date ,summary )

values(1,'元旦节放假通知','李平',GETDATE(),'','元旦放假5天,具体安排如下',GetDate(),'元旦放假5天'),

(1,'新中国成立70周年庆典','杨中华',GETDATE(),'','新中国成立70周年庆典,全国人民祝福中国',GetDate(),'70周年庆典'),

(1,'开学报到第一天','李平',GETDATE(),'','开学了,你准备好了吗?开学第一天,彩旗歌声飘过',GetDate(),'开学第一天');

  • 实现Model层

1.对应数据表users,新建User类

public class User

    {

        int uid;

        String uname;

        String upass;

 

        public User( string uname, string upass)

        {

            this.Uname = uname;

            this.Upass = upass;

        }

 

        public User(int uid, string uname, string upass)

        {

            this.uid = uid;

            this.uname = uname;

            this.upass = upass;

        }

 

        public int Uid { get => uid; set => uid = value; }

        public string Uname { get => uname; set => uname = value; }

        public string Upass { get => upass; set => upass = value; }

}

2.对应数据表topics,新建Topic类

public class Topic

    {

        int tid;

        String tname;

        public int Tid { get =>tid; set => tid = value; }

        public String Tname { get => tname; set => tname = value; }

        public Topic(int tid, string tname)

        {

            this.tid = tid;

            this.tname = tname;

        }

}

3.对应数据表news,新建News类

public class News

    {

        int nid;

        int ntid;

        String ntitle;

        String nauthor;

        DateTime ncreatedate;

        String npicpath;

        String ncontent;

        DateTime nmodifydate;

        String summary;

//属性设置略

}

未完,待续

本文地址:https://blog.csdn.net/yytrobot/article/details/107340463

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网