Click here to Skip to main content
15,886,578 members
Articles / Web Development / ASP.NET

Filtering LINQ Queries Using Business Rules Engine

19 Nov 2012CPOL7 min read 35.4K   779   22  
This article discusses the use of one such new feature, namely, rule-based filtering of LINQ queries using Web Rule, the XML-based super-fast rules engine, implementable as an ASP.NET or MVC component.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using CodeEffects.Rule.Core;
using CodeEffects.Rule.Mvc;
using CodeEffects.Rule.Models;
using CodeEffects.Rule.Common;
using CodeEffects.Rule.Demo.Filter.Mvc.Models;

namespace CodeEffects.Rule.Demo.Filter.Mvc.Controllers
{
    public class AjaxController : BaseController
    {
		// The main action that loads the initial Web Rule scripts, styles and client-side data
		public ActionResult Index()
		{
			// Create a new model and add it to the ViewBag for the view to consume
			ViewBag.Rule = RuleModel.Create(base.GetSource(DEFAULT));
			return View();
		}

		// The action that returns WEb Rule's UI settings based on the values selected by the user
		[HttpPost]
		public ActionResult LoadSettings(string entity, ThemeType theme, bool help)
		{
			// Create the filter control based on the selected entity
			// (which is control's source object)
			RuleEditor editor = this.GetFilter(entity);

			// Set the UI values selected by the user
			editor.ShowHelpString = help;
			editor.Theme = theme;

			// Get the actual UI settings
			string settings = editor.GetClientSettings();

			// Send the settings back to the client
			return Json(settings, JsonRequestBehavior.DenyGet);
		}

		// The action that does the search and returns results
		[HttpPost]
		public ActionResult Search(string entity, string clientData)
		{
			// Init the return type
			SearchResult result = new SearchResult();

			// Get the filter control
			RuleEditor filter = GetFilter(entity);

			// Load client's data (the actual filter) into the control
			filter.LoadClientData(clientData);

			// Check if the filter is empty
			if(filter.Rule.IsEmpty()) result.IsFilterEmpty = true;
			else if(!filter.Rule.IsValid())
			{
				// Check if it's valid
				result.IsFilterValid = false;
				result.ClientInvalidData = filter.GetClientInvalidData();
			}
			else
			{
				// The result type returns both the grid headers and search results in the same object
				// Load the headers based on the currently selected entity
				result.Headers = base.GetHeaders(entity);

				// Get filter's XML (called Rule XML)
				string xml = filter.Rule.GetRuleXml();

				// Perform the search based on the currently selected entity
				switch(entity)
				{
					case "Orders":
						var orders = from o in new WebRuleLinqEntities().Orders.Filter(xml)
									 select o;
						foreach(Order order in orders.ToList<Order>().Distinct<Order>(new EntityComparer()))
						{
							List<string> values = base.GetRow("Orders", order);
							result.Records.Add(values);
						}
						break;
					case "Products":
						var products = from p in new WebRuleLinqEntities().Products.Filter(xml)
									   select p;
						foreach(Product product in products.ToList<Product>().Distinct<Product>(new EntityComparer()))
						{
							List<string> values = base.GetRow("Products", product);
							result.Records.Add(values);
						}
						break;
					default:
						var customers = from c in new WebRuleLinqEntities().Customers.Filter(xml)
										select c;
						foreach(Customer customer in customers.ToList<Customer>().Distinct<Customer>(new EntityComparer()))
						{
							List<string> values = base.GetRow("Customers", customer);
							result.Records.Add(values);
						}
						break;
				}
			}

			// Return the result to the client
			return Json(result, JsonRequestBehavior.DenyGet);
		}

		private RuleEditor GetFilter(string entity)
		{
			// The ID here must be the same ID set in the view
			RuleEditor editor = new RuleEditor("filterControl");

			// Don't forget to set it as client-only control - this is an Ajax view
			editor.ClientOnly = true;

			// Tell Web Rule that we want it to use data filtering-related UI messages and settings
			editor.Mode = Common.RuleType.Filter;

			// Create an instance of the rule model, passing it the source object (current entity)
			editor.Rule = RuleModel.Create(base.GetSource(entity));

			return editor;
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions