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
};
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 / 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 / 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;
@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
//ASYNC
[TestMethod]
public void GetTypesByName_WhenNoNameProvided_ThrowsArgumentException()
{
//Arrange
var builder = new TypeServiceBuilder();
var service = builder.Build();
//Act
Func<Task> call = async () => await service.GetTypesByName(null);
public Mock<IService> DefaultService()
{
var mock = new Mock<IService>();
mock.Setup(x => x.MyMethod(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns<string, string, string>((typeName, comments, updatedBy) => Task.Run(() =>
new MyType
{
TypeName = typeName,
Comments = comments,
UpdatedBy = updatedBy
@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 / FileHelper.cs
Last active January 4, 2019 21:12
File utilities
using BusinessLogic.Utilities.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BusinessLogic.Logging;