惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - YbSoftwareFactory

YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法 YbSoftwareFactory 代码生成插件【二十四】:MVC中实现动态自定义路由 YbSoftwareFactory 代码生成插件【二十三】:集成强大的公文流转系统 YbSoftwareFactory 代码生成插件【二十二】:CMS基础功能的实现 YbSoftwareFactory 代码生成插件【二十一】:Web Api及MVC性能提升的几个小技巧 YbSoftwareFactory 代码生成插件【二十】:DynamicObject的序列化 YbSoftwareFactory 代码生成插件【十九】:实体类配合数据库表字段进行属性扩展的小技巧 YbSoftwareFactory 代码生成插件【十八】:树形结构下的查询排序的数据库设计 YbSoftwareFactory 代码生成插件【十七】:先进的权限模型体系设计 YbSoftwareFactory 代码生成插件【十六】:Web 下灵活、强大的审批流程实现(含流程控制组件、流程设计器和表单设计器) YbSoftwareFactory 代码生成插件【十五】:Show 一下最新的动态属性扩展功能与键值生成器功能 YbSoftwareFactory 代码生成插件【十四】:通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页 YbSoftwareFactory 代码生成插件【十三】:Web API 的安全性 YbSoftwareFactory 代码生成插件【十二】:超级灵活方便的应用程序设置管理API YbSoftwareFactory 代码生成插件【十一】:ASP.NET WebApi MVC下组织机构管理和菜单权限管理的实现 YbSoftwareFactory 代码生成插件【九】:基于JQuery、WebApi的ASP.NET MVC插件的代码生成项目主要技术解析 YbSoftwareFactory 代码生成插件【八】:基于JQuery EasyUI、Web Api的 ASP.NET MVC 代码生成插件 YbSoftwareFactory 代码生成插件【七】:YbRapidSolution for WinForm 插件生成项目总体架构介绍 YbSoftwareFactory 代码生成插件【六】:WinForm 完美解决方案的代码生成插件
YbSoftwareFactory 代码生成插件【十】:ASP.NET WebApi MVC下审计、缓存和导出功能的实现
YbSoftwareFactory · 2013-03-11 · via 博客园 - YbSoftwareFactory

    YbSoftwareFactory 的 ASP.NET MVC 插件所生成的项目目前支持缓存、审计日志和导出功能。

1、缓存功能

    缓存的目的是提高性能,缓存的设计也有一定的规范性可言,主要需要注意的是缓存不是完全可靠的,可能会被系统自动移除,同时易变的数据也不适合缓存。因此考虑到具体的场景,仅对审计日志、数据字典等不经常变化的数据进行了缓存,同时,即使这些数据被修改或删除也要及时把对应的缓存内容清除,以确保数据的准确。

    缓存的实现借鉴了NopCommerce的实现方式,采用扩展方法加回调函数的做法,这样在任何需要缓存的地方都能进行缓存的处理,扩展方法的核心代码如下:

/// <summary>
    
/// Extensions
    
/// </summary>
    public static class CacheExtensions
    {
        public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)
        {
            return Get(cacheManager, key, 60, acquire);
        }

        public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire) 
        {
            if (cacheManager.IsSet(key))
            {
                return cacheManager.Get<T>(key);
            }
            else
            {
                var result = acquire();
                //if (result != null)
                    cacheManager.Set(key, result, cacheTime);
                return result;
            }
        }
    }

    具体的缓存调用可以这样,是不是很灵活:-)

string key = string.Format(CacheKeyList.CONCRETEDATA_BY_TYPE_KEY, concreteType);
            var items = _cacheManager.Get(key, () =>
            {
                var list = ConcreteDataApi.FindAllByType(concreteType).Where(c => c.Status == 0);
                return list;
            });

2、审计日志功能:

    审计的概念涉及到系统安全,数据审计的目的之一是解决“授权侵犯”的问题(特指已授权用户执行非法操作的情况)。本系统的审计日志功能和log4net相兼容,可以通过配置log4net组件进行日志记录的读写,同时增强的数据审计功能还可自动记录业务数据添加、修改、删除的详细变化情况,是不是很强大:-)

 

3、数据导出功能:

    在ASP.NET MVC下数据导出比较简单,但本系统需实现的是在 web api下实现数据的导出和下载,目前这方面的资料较少。经过测试,目前下面的代码可行,感兴趣的朋友可以测试和了解一下,有了数据导出功能是不是很方便:-)

 1 public static HttpResponseMessage Download(Stream stream,string headerValue, string fileName)
 2         {
 3             var response = new HttpResponseMessage { Content = new StreamContent(stream) };
 4             response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
 5                 {
 6                     FileName = fileName
 7                 };
 8             response.Content.Headers.ContentType = new MediaTypeHeaderValue(headerValue);
 9             response.Content.Headers.ContentLength = stream.Length;
10             return response;
11         }

4、数据字典等数据访问层的实现:

    数据字典、审计日志等功能需要支持多种数据库环境,同时还要方便部署,这里通过Provider模式实现,其中连接字符串可以在配置文件中用标准方法进行配置,也能在程序中设置、并自定义加减密,而这无需在任何业务系统中编写代码,只需配置好数据库相关表就能进行调用,很是方便,如下是Provider的初始化代码:

#region Initialize

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            // Validate arguments
            if (config == nullthrow new ArgumentNullException("config");
            if (string.IsNullOrEmpty(name)) name = "YbHierarchyDataProvider";
            if (String.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description""Yb hierarchy data provider");
            }
            if (String.IsNullOrEmpty(config["tableName"]))
            {
                config.Remove("tableName");
                config.Add("tableName""HierarchyData");
            }
            // Initialize base class
            base.Initialize(name, config);

            // Read connection string
            this.ConnectionStringName = config.GetConfigValue("connectionStringName"null);
            if (string.IsNullOrWhiteSpace(this.ConnectionStringName)) throw new ConfigurationErrorsException(Resources.Required_connectionStringName_attribute_not_specified);
            this.connectionStringSetting = ConfigurationManager.ConnectionStrings[this.ConnectionStringName];
            if (this.connectionStringSetting == nullthrow new ConfigurationErrorsException(string.Format(Resources.Format_connection_string_was_not_found, this.ConnectionStringName));
            if (string.IsNullOrEmpty(this.connectionStringSetting.ProviderName)) throw new ConfigurationErrorsException(string.Format(Resources.Format_connection_string_does_not_have_specified_the_providerName_attribute, this.ConnectionStringName));

            //激发设置连接字符串前的事件处理程序,主要目的是解密连接字符串
            ConnectionStringChangingEventArgs args = RaiseConnectionStringChangingEvent(connectionStringSetting.ConnectionString);
            if (args == nullthrow new ProviderException(Resources.Connection_string_cannot_be_blank);
            if (!this.connectionStringSetting.ConnectionString.Equals(args.ConnectionString))
            {
                this.connectionStringSetting =
                    new ConnectionStringSettings(this.ConnectionStringName, args.ConnectionString, this.connectionStringSetting.ProviderName);
            }
            if (string.IsNullOrEmpty(this.connectionStringSetting.ConnectionString)) throw new ProviderException(Resources.Connection_string_cannot_be_blank);

            this.applicationName = config["applicationName"];

            this.tableName = config["tableName"];
            SecUtility.CheckParameter(ref tableName, truetruetrue256"tableName");
        }

        #endregion

    要兼容各类数据库,需要注意的是创建连接时需使用DbConnection而不是SqlConnection,需使用DbCommand而不是SqlCommand等,如下是创建数据库连接的代码,是不是很规范:-)

using (DbConnection db = this.connectionStringSetting.CreateDbConnection())

    最后附上Demo地址:http://mvcdemo.yellbuy.com/

    注:当前版本V1.1,已有V1.0版本及源码并需要升级的请及时联系。