A preliminary introduction to TableResolver
The functions introduced in the introduction can be realized only by configuring XML, which is very convenient. However, in the actual project, the situation will be more flexible and changeable, which can not be covered only through configuration. From the beginning, we will introduce how to extend or modify the previous configuration through plug-ins on the basis of XML configuration.
General Tk5TableResolver plug-in example
It is assumed that you have understood the method of adding plug-in modules. If you are not clear, please click How to add a plug-in module .
Take the agent management function as an example:
-
Add AgentResolver class in the plug-in project. The code is as follows
using System; using System.Data; using YJC.Toolkit.Data; using YJC.Toolkit.Sys; namespace TK55.Agent { [Resolver(Author = "XXX", CreateDate = "2020-06-15", Description = "Data access layer class of vendor table")] public class AgentResolver : Tk5TableResolver { internal const string DATAXML = "Agent/Agent.xml"; /// <summary> ///Construct the function and set the attached Xml file. /// </summary> ///< param name = "context" > database connection context < / param > ///< param name = "source" > attached data source < / param > public AgentResolver(IDbDataSource source) : base(DATAXML, source) { AutoUpdateKey = false; AutoTrackField = true; } /// <summary> ///It is triggered when a table is created, modified or deleted. Be careful not to delete base OnUpdatingRow(e); ///The UpdatingRow event is attached to the base class function. /// </summary> ///< param name = "e" > event parameters < / param > protected override void OnUpdatingRow(UpdatingEventArgs e) { base.OnUpdatingRow(e); switch (e.Status) { case UpdateKind.Insert: break; case UpdateKind.Update: break; case UpdateKind.Delete: break; } } } }
Note: you need to mark ResolverAttribute on the class name AgentResolver. Only in this way can the plug-in be registered. If the specified registration name is not displayed in ResolverAttribute, the system will automatically remove the suffix of Resolver from the class name to obtain the registration name. Take the above code as an example and register it as Agent.
-
Configure access to AgentResolver class in Module Xml. The configuration is as follows:
<tk:Module> <tk:Title> <tk:Content>Vendor management</tk:Content> </tk:Title> <tk:MetaData> <tk:SingleXmlMetaData DataXml="Agent/Agent.xml" ColumnCount="1"/> </tk:MetaData> <tk:Source> <tk:SingleDbSource DisablePage="Delete" PageSize="15" OrderBy="order by if_show desc,update_date desc"> <tk:Resolver> <tk:RegResolver>Agent</tk:RegResolver> </tk:Resolver> </tk:SingleDbSource> </tk:Source> <tk:PageMaker> <tk:RazorModuleTemplatePageMaker ModuleTemplate="Single" /> </tk:PageMaker> </tk:Module>
The AgentResolver class will be called for data operations such as the above configuration, addition, deletion, modification and query on the page. The following explains the common properties and methods of Tk5TableResolver class.
Common properties and methods of Tk5TableResolver
-
AutoUpdateKey
Whether to generate primary key automatically when adding. When set to true, the framework will automatically generate a unique Id for the corresponding primary key according to different databases.
-
AutoTrackField
Whether to automatically set the value of the tracking field. When set to true, the framework will update the following four fields according to the current login user, system time, creation or modification. The nicknames of these four fields must be: CreateDate, CreateId, UpdateDate and UpdateId. Please note that the nicknames of these four fields are case sensitive.
-
OnUpdatingRow(UpdatingEventArgs e)
Triggered when each record of the table is created, modified or deleted (note that this is record level, for each record. If two records are modified, this method will be triggered twice). You can modify the values of other related fields or records of other tables according to business needs.
For example, if AutoTrackField is set to false, you can realize the same functions by yourself, as follows:
/// <summary> ///It is triggered when a table is created, modified or deleted. Be careful not to delete base OnUpdatingRow(e); ///The UpdatingRow event is attached to the base class function. /// </summary> ///< param name = "e" > event parameters < / param > protected override void OnUpdatingRow(UpdatingEventArgs e) { base.OnUpdatingRow(e); switch (e.Status) { case UpdateKind.Insert: e.Row["CreateId"] = e.Row["UpdateId"] = BaseGlobalVariable.UserId; e.Row["CreateDate"] = e.Row["UpdateDate"] = DateTime.Now; break; case UpdateKind.Update: e.Row["UpdateId"] = BaseGlobalVariable.UserId; e.Row["UpdateDate"] = DateTime.Now; break; case UpdateKind.Delete: break; } }
WeChat official account for latest TKCore information: