A reference architecture (part 1) | Dunatis

The goal of this series is to show you an example how you could design a system. It’s kind of a reference architecture that I like to use (I have used it – a number of times in middle-sized projects, and I’m still quite happy about it), but it’s up to you to decide if you find some ideas to be usable in your specific environment.

Source: A reference architecture (part 1) | Dunatis

AngleSharp .NET library for scraping and parsing html/xml/css

AngleSharp is a .NET library that gives you the ability to parse angle bracket based hyper-texts like HTML, SVG, and MathML. XML without validation is also supported by the library. An important aspect of AngleSharp is that CSS can also be parsed. The parser is built upon the official W3C specification. This produces a perfectly portable HTML5 DOM representation of the given source code. Also current features such as querySelector or querySelectorAllwork for tree traversal.

https://github.com/AngleSharp/AngleSharp/

How to setup wep api to only return json and enable Cors: (web api v2)

In WebApiConfig.cs file from App_Start folder and add the following code in the Register method –

config.EnableCors(); //if cross origin requests should be enabled
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.Remove(config.Formatters.XmlFormatter);

This code does the following:

1. Converts names of properties to camel case while serializing the objects to JSON

2. Removes XML formatter from Web API’s formatters to make sure JSON data is returned on each request

Use this code to easily return json formatted response from api controller:

var employees= EmployeesRepository.GetAllEmployees();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, employees);
return response;

Example code for setting up controller and action with cors:

[EnableCors(origins: "http://localhost:55058", headers: "*", methods: "*")]
public classPTEmployeesController : ApiController
{

// GET api/ptemployees
[Route("api/ptemployees")]
public HttpResponseMessage Get()
{
var employees= EmployeesRepository.GetAllEmployees();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, employees);
return response;
}

...
}

From http://www.dotnetcurry.com/aspnet/1063/create-rest-service-using-aspnet-webapi

IEnumerable VS IQueryable

 

While query data from database, IQueryable execute select query on server side with all filters (e.g SQL select statement on database server). IEnumerable filters the data on client side. (in memory in application)

IEnumerable Example

  1. MyDataContext dc = new MyDataContext ();
  2. IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith(“S”));
  3. list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

  1. SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
  2. WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10” is missing since IEnumerable filters records on client side

IQueryable Example

  1. MyDataContext dc = new MyDataContext ();
  2. IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith(“S”));
  3. list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

  1. SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
  2. WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10” is exist since IQueryable executes query in SQL server with all filters.

 

Source: IEnumerable VS IQueryable