码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂

时间:2020-01-11 21:59:35      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:抽象   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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!