Skip to content

Instantly share code, notes, and snippets.

@osmyn
osmyn / CookieMonster
Created June 22, 2015 16:23
A javascript cookie module
<script>
var CookieMonster = (function() {
return {
checkCookie: checkCookie,
getCookie: getCookie,
setCookie: setCookie,
deleteCookie: deleteCookie
};
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Async Unit Test</Title>
<Author>Rusty Divine</Author>
<Description>Async Unit Test Template with 3 Parts</Description>
<HelpUrl>https://msdn.microsoft.com/en-us/library/ms165394.aspx</HelpUrl>
<SnippetTypes />
<Keywords />
@osmyn
osmyn / ServiceBuilder.cs
Created August 1, 2016 22:34
A helper to build dependencies for unit tests that still allow you to substitute your own behaviors in.
public class LocationServiceBuilder
{
//create default behaviors
IHttpService _httpService = Mock.Create<IHttpService>();
IFileService _fileService = Mock.CreateLike<IFileService>(fs => fs.ReadAllLines(Arg.AnyString) == new string[] { "key" });
//pass in your own dependency if needed
public LocationServiceBuilder WithHttpService(IHttpService httpService)
{
_httpService = httpService;
WITH cte AS
(SELECT *, ROW_NUMBER() OVER (PARTITION BY col1, col2, col3, col4 ORDER BY (SELECT 0)) AS DuplicateRowNumber
FROM table
)
--SELECT * FROM cte WHERE DuplicateRowNumber > 1
DELETE FROM cte WHERE DuplicateRowNumber > 1
@osmyn
osmyn / include.cs
Created March 7, 2017 17:01
Mock .Include for EF
private void HelpSetupActivitiesFakes(
List<Activity> fakeActivity,
ActivityOutcome fakeOutcome,
List<ArgumentHierarchy> fakeHierarchy)
{
var mockedActivity = RepositoryTestingHelper
.GetMockDbSet<Activity>(fakeActivity.AsQueryable());
mockedActivity.Setup(x => x.Include("InputArgument"))
.Returns(mockedActivity.Object);
@osmyn
osmyn / AlexaRequest.cs
Created July 1, 2017 20:44
Alexa Skill Validators
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace OneHundredCalories.Models
{
[JsonObject]
public class AlexaRequest
{
[JsonProperty("version")]
@osmyn
osmyn / gist:d3a2d1984ec538691779ebf04a987b2e
Created August 30, 2017 21:08
JsonCamelCaseFormatter
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
@osmyn
osmyn / httpHelper.cs
Created March 6, 2018 21:43
HttpClient Non-Default Headers
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://your.site.com");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", your_token);
httpClient.SendAsync(requestMessage);
@osmyn
osmyn / EFSaveChangesOverride.cs
Last active March 29, 2018 19:25
I often create an override in EF for save changes that handles read only and updatable entities
public override int SaveChanges()
{
var changed = ChangeTracker.Entries()
.Where(e => e.State == EntityState.Modified || e.State == EntityState.Added);
foreach (var item in changed)
{
if (item.Entity is IReadOnlyEntity)
{
item.State = EntityState.Unchanged;
@osmyn
osmyn / TSQL-to-POCO
Last active August 14, 2018 20:31 — forked from joey-qc/TSQL-to-POCO
A simple TSQL script to quickly generate c# POCO classes from SQL Server tables and views. You may tweak the output as needed. Not all datatypes are represented but this should save a bunch of boilerplate coding. USAGE: Run this query against the database of your choice. The script will loop through tables, views and their respective columns. Re…
declare @tableName varchar(200)
declare @columnName varchar(200)
declare @nullable varchar(50)
declare @datatype varchar(50)
declare @maxlen int
declare @sType varchar(50)
declare @sProperty varchar(200)
DECLARE table_cursor CURSOR FOR