From 64b5867884ee17abc5d7a33ba9adb1779a1a094b Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Wed, 7 Jun 2023 14:36:38 +0200 Subject: [PATCH] - egy-két felesleges lokális változó deklaráció törlése --- Vrh.OneReport/Enums.cs | 5 ----- Vrh.OneReport/ReportService.cs | 26 +++++--------------------- Vrh.OneReport/XmlProcessing/Dataset.cs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- 3 files changed, 66 insertions(+), 58 deletions(-) diff --git a/Vrh.OneReport/Enums.cs b/Vrh.OneReport/Enums.cs index 5457913..2462395 100644 --- a/Vrh.OneReport/Enums.cs +++ b/Vrh.OneReport/Enums.cs @@ -20,11 +20,6 @@ namespace Vrh.OneReport /// /// IENumerable adattibpust visszaadó plugin /// - Plugin, - - /// - /// IENumerable adattibpust visszaadó plugin - /// DataPlugin, /// diff --git a/Vrh.OneReport/ReportService.cs b/Vrh.OneReport/ReportService.cs index 1458ab8..e3f4e61 100644 --- a/Vrh.OneReport/ReportService.cs +++ b/Vrh.OneReport/ReportService.cs @@ -764,30 +764,16 @@ namespace Vrh.OneReport if (parameters != null && !parameters.AllKeys.Contains("SQLTEXT")) { parameters.Add("SQLTEXT", dataset.Command);} break; case DatasetTypes.SQLTextPlugin: - if (dataset.Plugin != null) - { - dataset.Command = dataset.Plugin.GetSqlText(dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, parameters); - } - else if (dataset.GetMethod != null) - { - dataset.Command = (string)dataset.GetMethod.Invoke(dataset.Plugin, new object[] { dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, parameters }); - } + if (dataset.Plugin == null || dataset.GetMethod == null) { throw new Exception((dataset.Plugin == null?"Plugin object is null! ":"") + (dataset.GetMethod == null ? "Method object is null!" : "")); } + dataset.Command = (string)dataset.GetMethod.Invoke(dataset.Plugin, new object[] { dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, parameters }); if (parameters == null) { } else if (parameters.AllKeys.Contains("SQLTEXT")) { parameters["SQLTEXT"]=dataset.Command; } else { parameters.Add("SQLTEXT", dataset.Command); } newReportDataSource = new ReportDataSource(dataset.Name, LoadDataTable(dataset.Type, dataset.ConnectionString, dataset.Command)); break; case DatasetTypes.DataPlugin: - IEnumerable resultdata = null; - if (dataset.Plugin != null) - { - resultdata = (IEnumerable)dataset.Plugin.GetData(dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml,parameters); - } - else if (dataset.GetMethod != null) - { - resultdata = (IEnumerable)dataset.GetMethod.Invoke(dataset.Plugin, new object[] { dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml,parameters }); - } - var returndataList = resultdata.ToList(); + if (dataset.Plugin == null || dataset.GetMethod == null) { throw new Exception((dataset.Plugin == null ? "Plugin object is null! " : "") + (dataset.GetMethod == null ? "Method object is null!" : "")); } + var returndataList = ((IEnumerable)dataset.GetMethod.Invoke(dataset.Plugin, new object[] { dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, parameters })); //.ToList(); newReportDataSource = new ReportDataSource(dataset.Name, returndataList); break; default: break;//ide nem jöhet, mert már korábban kibukna @@ -837,9 +823,7 @@ namespace Vrh.OneReport ReportService.ExportSQLText(dataset.ConnectionString, dataset.Command, dataset.ExportFile, this.Report.Timeout.SqlQuery, allformat: this.Report.Formats.AllFormats); break; case DatasetTypes.DataPlugin: - IEnumerable resultdata = (IEnumerable)(dataset.Plugin != null - ? dataset.Plugin.GetData(dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, this.Report.Parameters) - : dataset.GetMethod.Invoke(dataset.Plugin, new object[] { dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, this.Report.Parameters })); + var resultdata = (IEnumerable)dataset.GetMethod.Invoke(dataset.Plugin, new object[] { dataset.PluginViewName, dataset.ConnectionString, dataset.PluginAssemblyXml, dataset.DatasetXml, this.Report.Parameters }); //TODO: ezt meg kell csinálni!!!! ReportService.ExportSQLData(dataset.ConnectionString, resultdata, dataset.ExportFile, this.Report.Timeout.SqlQuery, allformat: this.Report.Formats.AllFormats); break; diff --git a/Vrh.OneReport/XmlProcessing/Dataset.cs b/Vrh.OneReport/XmlProcessing/Dataset.cs index f4e26d5..4e1238b 100644 --- a/Vrh.OneReport/XmlProcessing/Dataset.cs +++ b/Vrh.OneReport/XmlProcessing/Dataset.cs @@ -107,9 +107,14 @@ namespace Vrh.OneReport public XElement DatasetXml { get; set; } /// + /// DatasetTypes: SQL, Plugin, DataPlugin, SQLTextPlugin, + /// + public DatasetTypes DatasetType { get; set; } + + /// /// A riport adatait előállító plugin kerül ide bele /// - public IOneReport Plugin { get; set; } + public object Plugin { get; set; } /// /// A plugin-ban levő meghívandó metódus leírója @@ -125,23 +130,22 @@ namespace Vrh.OneReport /// A riport id-ja; exception üzenetben kerül CSAK felhasználásra /// A "Dataset" elem. /// /// Alapértelmezett adatok a plugin típusú működéshez - /// az alapértelmezett kapcsolat leíró arra az esetre, ha a datasetxml-ben nincs megadva - public Dataset(string reportid,XElement datasetxelement,List datasourcelist,string defaultconnectionstring) + /// az alapértelmezett kapcsolat leíró arra az esetre, ha a datasetxml-ben nincs megadva + public Dataset(string reportid,XElement datasetxelement,List datasourcelist,string defaultconnectionstringstorename) { if (datasetxelement != null) { this.Name = GetValue(AttributeNames.NAME, datasetxelement, "", true, true); - var plugintype = GetEnumValue(datasetxelement.Attribute(AttributeNames.TYPE), DatasetTypes.DataPlugin); - IOneReport plugin; MethodInfo sqlgetmethod; - switch (plugintype) + this.DatasetType = GetEnumValue(datasetxelement.Attribute(AttributeNames.TYPE), DatasetTypes.DataPlugin); + switch (this.DatasetType) { default: throw new ApplicationException($"Unknown Dataset type! Type:{this.Type}. Report Id:{reportid}."); case DatasetTypes.SQL: { this.Type = DatasetTypes.SQL; var connectionstringstorename = GetValue(AttributeNames.CONNECTIONSTRING, datasetxelement, (string)null); - if (string.IsNullOrWhiteSpace(connectionstringstorename)) { connectionstringstorename = defaultconnectionstring;} - this.ConnectionString = ConnectionStringStore.Get(connectionstringstorename); + if (string.IsNullOrWhiteSpace(connectionstringstorename)) { connectionstringstorename = defaultconnectionstringstorename;} + this.ConnectionString = ConnectionStringStore.GetSQL(connectionstringstorename); this.Command = GetValue(datasetxelement, "", true, true); break; } @@ -160,8 +164,7 @@ namespace Vrh.OneReport this.PluginMethodName = plugindescriptor.PluginMethodName; this.PluginViewName = GetValue(AttributeNames.PLUGINVIEWNAME, datasetxelement, (string)null); if (string.IsNullOrWhiteSpace(this.PluginViewName)) { this.PluginViewName = this.Name; } - LoadPlugin(this.PluginAssemblyPath, this.PluginTypeFullName, this.PluginMethodName, out plugin, out sqlgetmethod); - this.Plugin = plugin; + this.Plugin = GetPlugin(this.DatasetType, this.PluginAssemblyPath, this.PluginTypeFullName, this.PluginMethodName, new object[] { this.ConnectionString }, out MethodInfo sqlgetmethod); this.GetMethod = sqlgetmethod; this.DatasetXml = datasetxelement; break; @@ -169,39 +172,65 @@ namespace Vrh.OneReport } } } - private void LoadPlugin(string assemblypath, string plugintypename, string pluginmethodname, out IOneReport plugin, out MethodInfo getsqlmethod) + private object GetPlugin(DatasetTypes datasettype, string assemblypath, string plugintypename, string pluginmethodname, object[] plugintypeconstructorparameters, out MethodInfo getmethod) { try { - plugin = null; - getsqlmethod = null; + object plugin = null; + getmethod = null; //Assembly asm = Assembly.LoadFile(assemblypath); Assembly asm = Assembly.LoadFrom(assemblypath); - if (!string.IsNullOrWhiteSpace(plugintypename)) - { - System.Type type = asm.GetExportedTypes().FirstOrDefault(x => x.FullName == plugintypename && x.IsAssignableFrom(x)); - if (type == null) { throw new ApplicationException(""); } - if (string.IsNullOrWhiteSpace(pluginmethodname)) { pluginmethodname = nameof(IOneReport.GetSqlText); } - getsqlmethod = type.GetMethod(pluginmethodname); - if (getsqlmethod == null) { throw new ApplicationException(""); } - plugin = null; - } - else - { - plugintypename = typeof(IOneReport).FullName; - pluginmethodname = nameof(IOneReport.GetSqlText); - System.Type type = asm.GetExportedTypes().FirstOrDefault(x => typeof(IOneReport).IsAssignableFrom(x)); - if (type == null) { throw new ApplicationException(""); } - getsqlmethod = null; - plugin = (IOneReport)Activator.CreateInstance(type); - } + if (string.IsNullOrWhiteSpace(plugintypename)) plugintypename = typeof(IOneReport).FullName; + if (string.IsNullOrWhiteSpace(pluginmethodname)) pluginmethodname = datasettype == DatasetTypes.DataPlugin ? nameof(IOneReport.GetData) : datasettype == DatasetTypes.SQLTextPlugin ? nameof(IOneReport.GetSqlText) : null; + + var ptype = asm.GetExportedTypes().FirstOrDefault(x => x.FullName == plugintypename && x.IsAssignableFrom(x)); + if (ptype == null) { throw new ApplicationException("Type not found!"); } + plugin = Activator.CreateInstance(ptype, plugintypeconstructorparameters); + if (plugin == null) { throw new ApplicationException("Type constructor failed!"); } + getmethod = ptype.GetMethod(pluginmethodname); + if (getmethod == null) { throw new ApplicationException("Method not found!"); } + return plugin; } catch (Exception ex) { if (string.IsNullOrWhiteSpace(pluginmethodname)) { pluginmethodname = nameof(IOneReport.GetSqlText); } - throw new ApplicationException($"The implementation of the referred interface or method in the assembly, or the assembly (DLL) itself was not found! Assembly='{this.PluginAssemblyPath}', type='{plugintypename}',method='{pluginmethodname}'", ex); + throw new ApplicationException($"ERROR Not found!!! the implementation of the referred interface or method in the assembly! Assembly='{this.PluginAssemblyPath}', type='{plugintypename}',method='{pluginmethodname}'", ex); + return null; } } + //private void LoadPluginBAK(string assemblypath, string plugintypename, string pluginmethodname, out IOneReport plugin, out MethodInfo getsqlmethod) + //{ + // try + // { + // plugin = null; + // getsqlmethod = null; + // //Assembly asm = Assembly.LoadFile(assemblypath); + // Assembly asm = Assembly.LoadFrom(assemblypath); + // if (!string.IsNullOrWhiteSpace(plugintypename)) + // { + // System.Type type = asm.GetExportedTypes().FirstOrDefault(x => x.FullName == plugintypename && x.IsAssignableFrom(x)); + // if (type == null) { throw new ApplicationException(""); } + // plugin = (IOneReport)Activator.CreateInstance(type); + // if (string.IsNullOrWhiteSpace(pluginmethodname)) { pluginmethodname = nameof(IOneReport.GetSqlText); } + // getsqlmethod = type.GetMethod(pluginmethodname); + // if (getsqlmethod == null) { throw new ApplicationException(""); } + // } + // else + // { + // plugintypename = typeof(IOneReport).FullName; + // pluginmethodname = nameof(IOneReport.GetSqlText); + // System.Type type = asm.GetExportedTypes().FirstOrDefault(x => typeof(IOneReport).IsAssignableFrom(x)); + // if (type == null) { throw new ApplicationException(""); } + // getsqlmethod = null; + // plugin = (IOneReport)Activator.CreateInstance(type); + // } + // } + // catch (Exception ex) + // { + // if (string.IsNullOrWhiteSpace(pluginmethodname)) { pluginmethodname = nameof(IOneReport.GetSqlText); } + // throw new ApplicationException($"The implementation of the referred interface or method in the assembly, or the assembly (DLL) itself was not found! Assembly='{this.PluginAssemblyPath}', type='{plugintypename}',method='{pluginmethodname}'", ex); + // } + //} #endregion Constructor } } -- libgit2 0.21.2