Friday, March 21, 2014

Entity Framework wrapper to return custom objects using LINQ

The complex data types which are automatically generated by Entity Framework are very useful, but sometimes the data may need to be placed in a custom object which is reusable across multiple areas of the code. The entity framework custom type may also have additional unneeded columns or strange naming conventions on the columns which can lead to messy code, so a cleaner custom type is beneficial in this situation.

In the example below there is a wrapper class for getting a single object as well as a list of objects. Instead of using the default complex types (such as SelectEmployeeByDepartment_Result or SelectEmployee_Result), LINQ is used to parse the results into a custom object.


Business logic and other rules can also be placed at this layer (such as converting a SQL bit column into a C# bool object).

Thursday, March 20, 2014

URL Aliasing and Query String in Ektron using Regular Expressions


The Ektron CMS has a powerful URL Aliasing system which can be used to generate more user/search engine friendly URLS. For example instead of having a URL such as: http://mysite.com/Content.aspx?pageid=995&name=Ryan&number=102 you could display it as http://mysite.com/Employees/Ryan/102.

In this example post I am going to show you how to achieve this in Ektron (version 9.0). In this case the page URL and query string (which is validated against Regular Expressions) are formatted into a more friendly URL and the query string is still readable from code behind as with an un-aliased URL.

Firstly log into the Ektron work area and navigate to the 'Settings' tab. Under 'Configuration', then 'URL Aliasing', 'Aliasing Rules' and finally 'RegEx'. Click on 'Add Regular Expression':

The following attributes are required to be filled out before saving:
  • Active: Is the RegEx Alias acitve?
  • Site: Which Ektron site should the rule be applied to?
  • Expression Name: A name for your RegEx Alias.
  • Expression: Enter a RegEx expression that will replace the variables.
  • Expression Map: Enter the original URL with the relevant query string
  • Example URL: Enter an example URL which follows the expression's format.
  • Priority: Assign a priority for your RegEx Alias (the more frequent an alias is used, the higher the priority you would assign).
 In this case I have entered the following values:
  • Active: True
  • Site: Default
  • Expression Name: TestAlias
  • Expression: Employees/(?<name>(\w)/(?<number>([0-9])/
  • Expression Map: Templates/Content.aspx?pageid=995&nameA=${name}&numberA=${number}
  • Example URL: Employees/Ryan/102/
  • Priority: High
The expression contains 2 elements of query string: name which is validated with a regular expression to be a word (\w) and number which is validated with a regular expression to be numeric. You could add any number of query string elements (with advanced regular expression validation), along with static elements (such as 'employees' in this example). If someone attempts to accsess the URL incorrectly, such as http://mysite.com/employees/Jim/alpha they would get a 404 not found error (in this example, it would fail because 'alpha' is not numeric).

The expression map is the location of the actual page in Ektron which you want to place the alias over. In your code you can access the query string using the variables 'nameA' and 'numberA'.

This type of URL aliasing might be useful in a case where you have a code driven page that displays data for a number of elements (such as employees or products) where the query string drives what data is to be displayed.