From db7c439fd4ab379e02813fba56215ec8ae04f0dd Mon Sep 17 00:00:00 2001 From: Joe Harrison Date: Wed, 11 Feb 2015 10:45:19 -0500 Subject: [PATCH] dynamic instantiation of rendering engine --- MVCGrid/Engine/GridEngine.cs | 66 ++++++------------- MVCGrid/Interfaces/IMVCGridDefinition.cs | 2 +- MVCGrid/MVCGrid.csproj | 4 +- MVCGrid/Models/GridData.cs | 18 ----- MVCGrid/Models/GridDefinition.cs | 3 +- MVCGrid/Models/GridRow.cs | 23 ------- MVCGrid/Models/QueryOptions.cs | 2 + ...gEngine.cs => BootstrapRenderingEngine.cs} | 4 +- MVCGrid/Web/HtmlExtensions.cs | 4 +- MVCGrid/Web/MVCGridHandler.cs | 29 ++------ MVCGrid/Web/QueryStringParser.cs | 6 ++ MVCGridExample/App_Start/MVCGridConfig.cs | 3 +- MVCGridExample/Content/MVCGridConfig.txt | 3 +- MVCGridExample/Views/Demo/Export.cshtml | 1 - MVCGridExample/Views/Home/Index.cshtml | 7 ++ 15 files changed, 50 insertions(+), 125 deletions(-) delete mode 100644 MVCGrid/Models/GridData.cs delete mode 100644 MVCGrid/Models/GridRow.cs rename MVCGrid/Rendering/{HtmlRenderingEngine.cs => BootstrapRenderingEngine.cs} (98%) diff --git a/MVCGrid/Engine/GridEngine.cs b/MVCGrid/Engine/GridEngine.cs index c145c35..8215d65 100644 --- a/MVCGrid/Engine/GridEngine.cs +++ b/MVCGrid/Engine/GridEngine.cs @@ -13,6 +13,26 @@ namespace MVCGrid.Engine { public class GridEngine { + public IMVCGridRenderingEngine GetRenderingEngine(GridContext gridContext) + { + IMVCGridRenderingEngine renderingEngine = null; + + if (!String.IsNullOrWhiteSpace(gridContext.QueryOptions.RenderingEngineName)) + { + if (String.Compare(gridContext.QueryOptions.RenderingEngineName, "export", true) == 0) + { + renderingEngine = new CsvRenderingEngine(); + } + } + + if (renderingEngine == null) + { + renderingEngine = (IMVCGridRenderingEngine)Activator.CreateInstance(gridContext.GridDefinition.DefaultRenderingEngine, true); + } + + return renderingEngine; + } + public void Run(IMVCGridRenderingEngine renderingEngine, GridContext gridContext, Stream outputStream) { if (!renderingEngine.AllowsPaging) @@ -43,7 +63,6 @@ private RenderingModel PrepModel(int? totalRecords, List rows, Models.GridC model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName); PrepColumns(gridContext, model); - //PrepRows(data, gridContext, model); model.Rows = rows; if (model.Rows.Count == 0) @@ -78,51 +97,6 @@ private RenderingModel PrepModel(int? totalRecords, List rows, Models.GridC } return model; } - - //private void PrepRows(Models.GridData data, Models.GridContext gridContext, RenderingModel model) - //{ - // foreach (var item in data.Rows) - // { - // Row renderingRow = new Row(); - // model.Rows.Add(renderingRow); - - // if (!String.IsNullOrWhiteSpace(item.RowCssClass)) - // { - // renderingRow.CalculatedCssClass = item.RowCssClass; - // } - - // foreach (var col in gridContext.GetVisibleColumns()) - // { - // string val = ""; - - // if (item.Values.ContainsKey(col.ColumnName)) - // { - // val = item.Values[col.ColumnName]; - // } - - // Cell renderingCell = new Cell(); - // renderingRow.Cells.Add(col.ColumnName, renderingCell); - // if (item.CellCssClasses.ContainsKey(col.ColumnName)) - // { - // string cellCss = item.CellCssClasses[col.ColumnName]; - // if (!String.IsNullOrWhiteSpace(cellCss)) - // { - // renderingCell.CalculatedCssClass = cellCss; - // } - // } - - // if (col.HtmlEncode) - // { - // renderingCell.HtmlText = HttpUtility.HtmlEncode(val); - // } - // else - // { - // renderingCell.HtmlText = val; - // } - // } - // } - //} - private void PrepColumns(Models.GridContext gridContext, RenderingModel model) { foreach (var col in gridContext.GetVisibleColumns()) diff --git a/MVCGrid/Interfaces/IMVCGridDefinition.cs b/MVCGrid/Interfaces/IMVCGridDefinition.cs index 9c544e3..494d3e0 100644 --- a/MVCGrid/Interfaces/IMVCGridDefinition.cs +++ b/MVCGrid/Interfaces/IMVCGridDefinition.cs @@ -21,6 +21,6 @@ internal interface IMVCGridDefinition string ClientSideLoadingCompleteFunctionName { get; set; } bool Filtering { get; set; } - Type HtmlWriterType { get; set; } + Type DefaultRenderingEngine { get; set; } } } diff --git a/MVCGrid/MVCGrid.csproj b/MVCGrid/MVCGrid.csproj index 76e74f4..0ccf9b9 100644 --- a/MVCGrid/MVCGrid.csproj +++ b/MVCGrid/MVCGrid.csproj @@ -55,7 +55,7 @@ - + @@ -63,8 +63,6 @@ - - diff --git a/MVCGrid/Models/GridData.cs b/MVCGrid/Models/GridData.cs deleted file mode 100644 index 2e925c0..0000000 --- a/MVCGrid/Models/GridData.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace MVCGrid.Models -{ - public class GridData - { - public GridData() - { - Rows = new List(); - } - - public int? TotalRecords { get; set; } - public List Rows { get; set; } - } -} diff --git a/MVCGrid/Models/GridDefinition.cs b/MVCGrid/Models/GridDefinition.cs index cef95d0..98df8fd 100644 --- a/MVCGrid/Models/GridDefinition.cs +++ b/MVCGrid/Models/GridDefinition.cs @@ -20,7 +20,6 @@ public class GridDefinition : GridDefinitionBase, IMVCGridDefinition public GridDefinition() : this(null) { - //_defaultRenderingEngine= } public GridDefinition(GridConfiguration copyFromConfig):base() @@ -32,7 +31,7 @@ public GridDefinition(GridConfiguration copyFromConfig):base() Columns = new List>(); NoResultsMessage = DefaultNoResultsMessage; - //_htmlWriterType = typeof(MVCGrid.Rendering.BootstrapHtmlWriter); + _defaultRenderingEngine = typeof(MVCGrid.Rendering.BootstrapRenderingEngine); } public GridConfiguration GridConfiguration { get; set; } diff --git a/MVCGrid/Models/GridRow.cs b/MVCGrid/Models/GridRow.cs deleted file mode 100644 index c3044cc..0000000 --- a/MVCGrid/Models/GridRow.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace MVCGrid.Models -{ - public class GridRow - { - public GridRow() - { - Values = new Dictionary(); - PlainTextValues = new Dictionary(); - CellCssClasses = new Dictionary(); - } - - public Dictionary Values { get; set; } - public Dictionary PlainTextValues { get; set; } - public Dictionary CellCssClasses { get; set; } - - public string RowCssClass { get; set; } - } -} diff --git a/MVCGrid/Models/QueryOptions.cs b/MVCGrid/Models/QueryOptions.cs index 1add7ec..993488f 100644 --- a/MVCGrid/Models/QueryOptions.cs +++ b/MVCGrid/Models/QueryOptions.cs @@ -12,6 +12,8 @@ public QueryOptions() Filters = new Dictionary(); } + public string RenderingEngineName { get; set; } + public SortDirection SortDirection { get; set; } public string SortColumn { get; set; } diff --git a/MVCGrid/Rendering/HtmlRenderingEngine.cs b/MVCGrid/Rendering/BootstrapRenderingEngine.cs similarity index 98% rename from MVCGrid/Rendering/HtmlRenderingEngine.cs rename to MVCGrid/Rendering/BootstrapRenderingEngine.cs index fab2e75..f514d72 100644 --- a/MVCGrid/Rendering/HtmlRenderingEngine.cs +++ b/MVCGrid/Rendering/BootstrapRenderingEngine.cs @@ -11,14 +11,14 @@ namespace MVCGrid.Rendering { - public class HtmlRenderingEngine : IMVCGridRenderingEngine + public class BootstrapRenderingEngine : IMVCGridRenderingEngine { private string CssTable; private string HtmlImageSortAsc; private string HtmlImageSortDsc; private string HtmlImageSort; - public HtmlRenderingEngine() + public BootstrapRenderingEngine() { CssTable = "table table-striped table-bordered"; } diff --git a/MVCGrid/Web/HtmlExtensions.cs b/MVCGrid/Web/HtmlExtensions.cs index 031dfab..01964f6 100644 --- a/MVCGrid/Web/HtmlExtensions.cs +++ b/MVCGrid/Web/HtmlExtensions.cs @@ -36,9 +36,9 @@ internal static IHtmlString MVCGrid(this HtmlHelper helper, string name, IMVCGri var gridContext = GridContextUtility.Create(HttpContext.Current, gridName, grid, options); - IMVCGridRenderingEngine renderingEngine = new HtmlRenderingEngine(); - GridEngine engine = new GridEngine(); + + IMVCGridRenderingEngine renderingEngine = engine.GetRenderingEngine(gridContext); using (MemoryStream ms = new MemoryStream()) { diff --git a/MVCGrid/Web/MVCGridHandler.cs b/MVCGrid/Web/MVCGridHandler.cs index 5146f42..5bc0c00 100644 --- a/MVCGrid/Web/MVCGridHandler.cs +++ b/MVCGrid/Web/MVCGridHandler.cs @@ -152,7 +152,7 @@ private void HandelPngImage(HttpContext context, string imageName) context.Response.Flush(); } - + private void HandleTable(HttpContext context) { @@ -173,34 +173,13 @@ private void HandleTable(HttpContext context) var gridContext = GridContextUtility.Create(context, gridName, grid, options); - IMVCGridRenderingEngine renderingEngine = DetermineRenderingEngine(context); - GridEngine engine = new GridEngine(); - renderingEngine.PrepareResponse(context.Response); - engine.Run(renderingEngine, gridContext, context.Response.OutputStream); - } - - private IMVCGridRenderingEngine DetermineRenderingEngine(HttpContext context) - { - IMVCGridRenderingEngine engine = null; - - if (context.Request.QueryString["engine"] != null) - { - string re = context.Request.QueryString["engine"]; - if (String.Compare(re, "export", true) == 0) - { - engine = new CsvRenderingEngine(); - } - } - if (engine == null) - { - engine = new HtmlRenderingEngine(); - } + IMVCGridRenderingEngine renderingEngine = engine.GetRenderingEngine(gridContext); - return engine; + renderingEngine.PrepareResponse(context.Response); + engine.Run(renderingEngine, gridContext, context.Response.OutputStream); } - private void HandleScript(HttpContext context) { diff --git a/MVCGrid/Web/QueryStringParser.cs b/MVCGrid/Web/QueryStringParser.cs index facea55..0768a17 100644 --- a/MVCGrid/Web/QueryStringParser.cs +++ b/MVCGrid/Web/QueryStringParser.cs @@ -19,6 +19,12 @@ public static QueryOptions ParseOptions(IMVCGridDefinition grid, HttpRequest htt var options = new QueryOptions(); + if (httpRequest.QueryString["engine"] != null) + { + string re = httpRequest.QueryString["engine"]; + options.RenderingEngineName = re; + } + if (!grid.Paging) { options.ItemsPerPage = null; diff --git a/MVCGridExample/App_Start/MVCGridConfig.cs b/MVCGridExample/App_Start/MVCGridConfig.cs index 487c73d..1d4a06d 100644 --- a/MVCGridExample/App_Start/MVCGridConfig.cs +++ b/MVCGridExample/App_Start/MVCGridConfig.cs @@ -27,7 +27,8 @@ public static void RegisterGrids() { return String.Format("{1}", c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id); - }); + }) + .WithPlainTextValueExpression((p,c) => p.Id.ToString()); cols.Add().WithColumnName("FirstName") .WithHeaderText("First Name") .WithValueExpression((p, c) => p.FirstName); diff --git a/MVCGridExample/Content/MVCGridConfig.txt b/MVCGridExample/Content/MVCGridConfig.txt index 487c73d..1d4a06d 100644 --- a/MVCGridExample/Content/MVCGridConfig.txt +++ b/MVCGridExample/Content/MVCGridConfig.txt @@ -27,7 +27,8 @@ namespace MVCGridExample { return String.Format("{1}", c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id); - }); + }) + .WithPlainTextValueExpression((p,c) => p.Id.ToString()); cols.Add().WithColumnName("FirstName") .WithHeaderText("First Name") .WithValueExpression((p, c) => p.FirstName); diff --git a/MVCGridExample/Views/Demo/Export.cshtml b/MVCGridExample/Views/Demo/Export.cshtml index aab9a91..d00db43 100644 --- a/MVCGridExample/Views/Demo/Export.cshtml +++ b/MVCGridExample/Views/Demo/Export.cshtml @@ -12,7 +12,6 @@ -

 

@Html.MVCGrid("ExportGrid") diff --git a/MVCGridExample/Views/Home/Index.cshtml b/MVCGridExample/Views/Home/Index.cshtml index e1ef478..792e3b3 100644 --- a/MVCGridExample/Views/Home/Index.cshtml +++ b/MVCGridExample/Views/Home/Index.cshtml @@ -16,6 +16,13 @@

Example

+
+
+ +
+
@(Html.MVCGrid("TestGrid")) @*