zoukankan      html  css  js  c++  java
  • 简单三层备忘

    1.实例层

    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace OA.Entity
    {
        public class user
        {
            private int _id;
    
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
            private string _name;
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }

    2.数据访问接口(引用实体)

    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace OA.IDAL
    {
       
            public interface user
            {
                OA.Entity.user LoadById(int id);
            }
    
       
    }

    3.数据访问实现(引用实体和接口)参照此实现可以根据接口写其它数据库实现

    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace OA.SQLProvider
        
    {
        public class user:OA.IDAL.user
        {
            public OA.Entity.user LoadById(int id)
            {
                if (id==1)
                {
                    OA.Entity.user list = new Entity.user
                    {
                        Id = 1,
                        Name = "TEST NAME"
                    };
                   return list;
                }
                else
                {
    
                    OA.Entity.user list = new Entity.user
                    {
                        Id = 0,
                        Name = "null"
                    };
                    return list;
                }
                 
            }
        }
    }

    4.BLL业务逻缉层(引用实体。接口。工厂)

    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace OA.BLL
    {
        public class user
        {
            public OA.Entity.user  ById(int id)
             {
                OA.IDAL .user ProviderClass= DALFactory .DataAccess.CreateObject <OA.IDAL .user >();
    
            return ProviderClass .LoadById(id);
            }
        }
    }

    5.数据工厂

    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    
    namespace OA.DALFactory
    {
        /// <summary>
        /// 抽象工厂模式创建DAL-(利用工厂模式+泛型机制+反射机制+缓存机制,实现动态创建不同的数据层对象接口) 。
        /// 可以在这个DataAccess类里创建所有DAL类
        /// </summary>
        public sealed class DataAccess
        {
            /// <summary>
            /// 数据访问的具体实现
            /// </summary>
            private static readonly string path = System.Web.HttpContext.Current.Server.MapPath("~/DataProviders/" + System.Web.Configuration.WebConfigurationManager.AppSettings["dllname"]);
    
            /// <summary>
            /// 采用泛型创建对象或从缓存获取
            /// </summary>
            /// <typeparam name="T">数据类型</typeparam>
            /// <returns>确定类型的对象</returns>
            public static T CreateObject<T>()
            {
                // 此处可采用XML或者配置文件,把对应关系做的更加灵活             
                //string typeName = "OA.DALSQLProvider." + typeof(T).Name;//OA.DALSQLProvider .User 
                string provider = System.Web.Configuration.WebConfigurationManager.AppSettings["Provider"];
                string typeName = provider + "." + typeof(T).Name;
               
                object obj;
    
                // 从缓存中读取数据
                obj = System.Web.HttpContext.Current.Cache.Get(typeName);
    
                // 创建新的实例
                if (obj == null)
                {   // 反射创建
                    obj = System.Reflection.Assembly.LoadFile(path).CreateInstance(typeName);
                    System.Web.HttpContext.Current.Cache.Add(typeName, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);    // 放入缓存中
                }
                return (T)obj;
                
            }
        }
    }

    6.UI(引用实体,业务)

    View Code
     protected void Button1_Click(object sender, EventArgs e)
            {
                OA.BLL.user user = new OA.BLL.user();
                OA.Entity.user list = new Entity.user();
                list = user.ById(1);
                this.Label1.Text = list.Name;
            }

    WEB方式的WEBCONFIG(改变此处即可以实现调用不同数据库)

    View Code
     <appSettings>
    
        <add  key ="dllname" value ="OA.SQLProvider.dll"/>
        <add key="Provider" value="OA.SQLProvider"/>
      </appSettings>

     复制OA.SQLProvider.dll到"~/DataProviders/“下面,此处与WEBCONFIG配合实现调用不同的数据库)

  • 相关阅读:
    9、spring五种scope
    2、数据库四种事务隔离级别
    4、jquery获取servlet值
    3、$.post不执行
    A brief Arch installation in VMware
    Git经验记录
    Windows上virtualenv搭建python开发环境
    no such file django-admin.py
    复制拷贝函数+重载operator=
    Reconfigure CentOS+freeradius+daloradius again
  • 原文地址:https://www.cnblogs.com/51yi/p/2480834.html
Copyright © 2011-2022 走看看