Skip to content

Instantly share code, notes, and snippets.

View hanssens's full-sized avatar

Juliën Hanssens hanssens

View GitHub Profile
@hanssens
hanssens / CacheProvider.cs
Created June 10, 2016 10:19
Poor man's example of read/write of objects to a file, let's call it "cache", using JSON serialize/deserialize. Note that this stores the file hardcoded in ~/Data/APPNAME.cache.
[Serializable]
public static class CacheProvider
{
public const string CacheFilename = "APPNAME.cache";
public static string DataStoragePath()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
}
@hanssens
hanssens / app.html
Created May 6, 2016 08:01 — forked from JeroenVinke/app.html
Aurelia - Kendo UI Playground
<template>
<ak-chart k-title.bind="{text: 'Gross Domestic product growth \n /GDP annual %/'}"
k-legend.bind="{position: 'bottom'}"
k-series-defaults.bind="seriesDefaults"
k-series.bind="series"
k-value-axis.bind="valueAxis"
k-category-axis.bind="categoryAxis"
k-tooltip.bind="tooltip">
</ak-chart>
</template>
@hanssens
hanssens / WebApi Guidelines.md
Created April 9, 2016 13:57
WebApi Guidelines

... just some arbitrary notes on webapi guidelines. #wip

Naming Conventions

Access Control

  • Is CORS enabled and/or properly configured?
@hanssens
hanssens / GenericDocumentRepository.cs
Last active December 2, 2015 14:51
GenericDataRepository for easy CRUD operations with RavenDB
namespace Hanssens.Integrations.RavenDb
{
/**
* Built by : hanssens.com
* License : MIT
* See also : https://gist.github.com/hanssens/63db5bce5674cc950f3e
*/
/// <summary>
@hanssens
hanssens / TheNullableQuestion.cs
Created October 21, 2015 11:39
The Nullable Question
// consider the following model
var person = new {
Name = "Harry Potter",
DateOfBirth = DateTime.Now,
DateOfBirthSpecified = true
};
// now if the 'DateOfBirth' is specified, use it's value
// otherwise, simply use null
DateTime? x = (person.DateOfBirthSpecified) ? person.DateOfBirth : null;
@hanssens
hanssens / DataController.cs
Created August 12, 2015 12:55
DataTables server-side pageable, sorteable and filterable grid
public JsonResult Products(IDataTablesRequest request)
{
// Nothing important here. Just creates some mock data.
var data = ProductFactory.Create();
// Global filtering.
// Filter is being manually applied due to in-memmory (IEnumerable) data.
// If you want something rather easier, check IEnumerableExtensions Sample.
var filteredData = data.Where(_item => _item.Name.Contains(request.Search.Value));
@hanssens
hanssens / DataController.cs
Created August 12, 2015 12:03
Kendo server-side pageable grid
public class DataController : Controller
{
public JsonResult Products(int pageSize = 25, int skip = 0)
{
var products = ProductFactory.Create();
var total = products.Count();
var data = products.Skip(skip).Take(pageSize).ToList();
return Json(new { total = total, data = data }, JsonRequestBehavior.AllowGet);
}
@hanssens
hanssens / RequireSessionKeyAttribute.cs
Created August 4, 2015 15:44
RequireSessionKeyAttribute - Authenticates a token provided as a a header in a webapi request.
/// <summary>
/// Authenticates a token provided as a a header in a webapi request.
/// </summary>
public class RequireSessionKeyAttribute : ActionFilterAttribute
{
public string TokenFieldName { get; set; }
public RequireSessionKey() : this("token") { }
public RequireSessionKey(string tokenFieldName)
@hanssens
hanssens / BasicAuthAttribute.cs
Created August 4, 2015 15:23
BasicAuthAttribute - Attribute that validates the provided credentials in the XHR header of an AJAX request.
/// <summary>
/// Attribute that validates the provided credentials in the XHR header of an AJAX request.
/// </summary>
/// <example>
/// Example call:
/// http://stackoverflow.com/a/11960692/1039247
/// </example>
public class BasicAuthAttribute : ActionFilterAttribute
{
@hanssens
hanssens / IsAnyNullOrEmpty.cs
Created July 23, 2015 14:29
Extension for checking if all properties in an object contains values with null
public static bool IsAnyNullOrEmpty(this object obj)
{
// could be less verbose, in on linq query,
// but it would come at the cost of readability
foreach (var pi in obj.GetType().GetProperties())
{
var value = (string)pi.GetValue(obj);
if (string.IsNullOrEmpty(value)) return false;
}