• 业务逻辑层-Active Record


    Active Record(活动记录模式),当系统中的业务和数据库中的表存在一一对应关系的时候,可用采用。
    Active Record模式的特点:每个业务对象代表数据表中的一行数据,并且业务对象还包括了数据的增删改查的方法。

    ORM

    一般这种模式采用一种ORM框架,即对象关系映射。这里用的的映射是:Dapper(开源轻量级,高效率,白自动化),Dapper需要我们写sql语法,所有是半自动化。

    案例

    内容管理系统中一博客系统

    1. 可以发布博客
    2. 对博客进行评论

    code

    代码下载

    对象实体

    	public class Post
    	{
    		public int Id { get; set; }
    		public string Subject { get; set; }
    		public string Text { get; set; }
    		public DateTime DateAdded { get; set; }
    	}
    
        public class Comment
    	{
    		public int? Id { get; set; }
    		public string Text { get; set; }
    		public string Author { get; set; }
    		public DateTime DateAdded { get; set; }
    		public int PostId { get; set; }
    	}
    

    实体对象

    	public class PostWithComment
    	{
    		public int Id { get; set; }
    		public string Subject { get; set; }
    		public string Text { get; set; }
    		public DateTime DateAdded { get; set; }
    		public List<Comment> Comments { get; set; }
    	}
    

    数据连接

    因Dapper是对DbConnection的扩展方法,这里定义一个父类ConnectionBase,操作数据的类只需继承父类

    	public abstract class ConnectionBase
    	{
    		public static string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    
    		protected SqlConnection _connection;
    
    		protected SqlConnection connection => _connection ?? (_connection = GetOpenConnection());
    
    		public static SqlConnection GetOpenConnection(bool mars = false)
    		{
    			var cs = ConnectionString;
    			if (mars)
    			{
    				var scsb = new SqlConnectionStringBuilder(cs)
    				{
    					MultipleActiveResultSets = true
    				};
    				cs = scsb.ConnectionString;
    			}
    			var connection = new SqlConnection(cs);
    			connection.Open();
    			return connection;
    		}
    
    		public void Dispose()
    		{
    			_connection?.Dispose();
    		}
    	}
    

    服务

    public class BlogService: ConnectionBase
    	{
    		public IEnumerable<Post> GetAllPosts()
    		{
    			string sql = "select * from Posts";
    			return connection.Query<Post>(sql);
    		}
    
    		public PostWithComment GetPostWithComments(int postId)
    		{
    			string sql = @"select * from Posts p
    							left join Comments c on p.Id = c.PostId
    							where p.Id = @Id";
    			PostWithComment result = null;
    			var relust = connection.Query<PostWithComment, Comment, PostWithComment>(sql,(post,comment)=> 
    			{
    				if(result == null)
    				{
    					result = post;
    					result.Comments = new List<Comment>();
    				}
    				if(comment != null)
    				{
    					result.Comments.Add(comment);
    				}
    				return result;
    			},
    			new { Id=postId}).FirstOrDefault();
    
    			return relust;
    		}
    	}
    
  • 相关阅读:
    nginx通过配置防止DDoS攻击
    BZOJ 2120 数颜色(单点修改莫队)
    数论模板
    BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(分块)
    HDU 4609 3-idiots(FFT)
    BZOJ 3527 [Zjoi2014]力(FFT)
    快速对拍
    HDU 1402 A * B Problem Plus(FFT)
    FFT
    BZOJ 5319: [Jsoi2018]军训列队(可持久化线段树)
  • 原文地址:https://www.cnblogs.com/LoveTomato/p/9397564.html
Copyright © 2020-2023  润新知