YbSoftwareFactory 代碼生成插件【十】:ASP.NET WebApi MVC下審計、緩存和導出功能的實現


    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版本及源碼並需要升級的請及時聯系。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM