标签:抽象 factory users generic 工厂 col stat view interface
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Dapper; using System.Data.SqlClient; using System.Collections; namespace WebApplication4.Controllers { public class UserInfoController : Controller { // GET: UserInfo public ActionResult Index() { return View(); } [HttpPost] public void Index(UserModel m) { Factory factory = Factory.GetFactory(); IUser user = factory.GetUsers(); int n = user.GetUsers(m); if (n>0) { } } /// <summary> /// Model /// </summary> public class UserModel { public int Id { get; set; } public string UName { get; set; } public string UPwd { get; set; } } /// <summary> /// 接口 /// </summary> public interface IUser { int GetUsers(UserModel m); } /// <summary> /// 实现方法 /// </summary> public class SQLUser : IUser { public int GetUsers(UserModel m) { using (SqlConnection conn = new SqlConnection("")) { return Convert.ToInt32(conn.ExecuteScalar($"select count(1) from User where UName=‘@UName‘ and UPwd=‘@UPwd‘",new { UName = m.UName, UPwd = m.UPwd })); } } } /// <summary> /// 获取 实体业务工厂 对象 /// </summary> /// <returns></returns> public abstract class Factory { public static Factory GetFactory() { Factory fac= new Factory_A(); return fac; } public abstract IUser GetUsers(); } /// <summary> /// 实现SQL工厂 /// </summary> public class Factory_A : Factory { public override IUser GetUsers() { return new SQLUser(); } } } }
标签:抽象 factory users generic 工厂 col stat view interface
原文地址:https://www.cnblogs.com/ly-03-04/p/12181127.html