【ASP.NET MVC】使用动软(三)(11)

一、问题

上文中提到,动软提供了数据库的基本操作功能,但是往往需要添加新的功能来解决实际问题,比如GetModel,通过id去查对象:

这个功能就需要进行改进:往往程序中获取的是实体的其他属性,比如用户登录的时提供账号名和密码,需要根据账户名(唯一)去获取数据库中的实体信息,而不是 id 。

二、解决

通过id查对象和通过其他属性查对象应该是非常类似的,可以 "照葫芦画瓢 "!

1、先在BLL中添加函数,这里利用的是函数重载(参数类型不同)

对比上下两个函数,参数不同而已。

2、在DAL中先找到GetModel函数

很显然,拷贝后,简单修改一下就可以使用利用账号名查找 的GetModel函数了

3、添加代码:

复制代码
 public Maticsoft.Model.user GetModel(string username)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,userID,password,userName from user ");
            strSql.Append(" where userID=@username");
			MySqlParameter[] parameters = {
					new MySqlParameter("@userName", MySqlDbType.VarChar)
			};
            parameters[0].Value = username;

			Maticsoft.Model.user model=new Maticsoft.Model.user();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

DAL里两个函数的关系:

由此,对动软生成的功能进行了扩展。比如登录时候的验证代码:

复制代码
       [HttpPost]
        public ActionResult Login(string msg)
        {
            JObject jobject = JObject.Parse(msg);
            string username = (string)jobject["username"];
            string password = (string)jobject["password"];
            Maticsoft.BLL.user bll = new Maticsoft.BLL.user();
            string pwd1 = UserCookie.Encrypt(password);
            Maticsoft.Model.user mod = bll.GetModel(username);
            if (mod == null)
            {
                return Content("error");
            }

            if (password != null)
            {
                string pwd = UserCookie.Encrypt(password);
                if (pwd == mod.password)
                {
                    UserCookie.WriteCookie(mod);
                    return Content("OK");
                }
                else
                    return Content("error");
            }
            return Content("error");
        }

其中: Maticsoft.Model.user mod = bll.GetModel(username);

则是利用提交的用户名(唯一)去获取数据库中对应记录的信息。(拓展功能)

以上有对密码加密、写入Cookie等功能,后文再介绍。

相关推荐
techdashen16 小时前
Arborium:把 tree-sitter 语法高亮打包成 Rust 文档生态的基础设施
开发语言·后端·rust
Profile排查笔记16 小时前
指纹浏览器环境异常排查:Fingerprint、Profile、Proxy、Session 和 Task Log 怎么看
前端·人工智能·后端·自动化
小强库计算机毕业设计16 小时前
源码分享Spring Boot + Vue3 的校园社团管理系统
java·spring boot·后端·计算机毕业设计
阿新聊ai17 小时前
从 Prompt 到 Loop:AI 编程 Agent 四代循环的演进全景
人工智能·后端
im_lanny17 小时前
从 Function Calling 到 MCP:Agent 工具调用的三层境界与生产级安全护栏
后端
agent89717 小时前
Spring Boot 接口超时治理:从连接池、线程池到熔断限流的完整排查思路
java·spring boot·后端
雨师@18 小时前
go语言项目--实例化(图书管理)--005
开发语言·后端·golang
Aspiresky18 小时前
探索Rust语言之引用
开发语言·后端·rust
geovindu18 小时前
go: Functional Options Pattern
开发语言·后端·设计模式·golang·函数式选项模式’·惯用法模式
冰暮流星19 小时前
flask之app.py讲解
后端·python·flask