抽象工厂

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


    }
}
posted @ 2020-01-11 20:59  噜啦啦0001  阅读(110)  评论(0编辑  收藏  举报