Skip to content

Instantly share code, notes, and snippets.

View driscollwebdev's full-sized avatar

Brian Driscoll driscollwebdev

View GitHub Profile
@driscollwebdev
driscollwebdev / BetterSaveExample.cs
Last active December 11, 2015 03:38
A better example of parsing and saving an object from JSON using the Single Responsibility Principle
public void SaveFromJson(string jsonData, DbContext saveContext)
{
Person person = GetFromJson(jsonData);
Save(person, saveContext);
}
public Person GetFromJson(string jsonData)
{
try
{
@driscollwebdev
driscollwebdev / SavePersonJsonExample.cs
Created January 15, 2013 14:41
An example of parsing an object's data from its representation and saving it somewhere all in one method (not the best approach).
public void SaveFromJson(string jsonData, DbContext saveContext)
{
try
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Person person = serializer.Deserialize<Person>(jsonData);
//This is just an example, but in prod code you'd need
//to make sure the entity doesn't already exist.
saveContext.Entry<Person>(person).State = System.Data.EntityState.Added;
saveContext.SaveChanges();
@driscollwebdev
driscollwebdev / structuredData.json
Last active December 11, 2015 01:59
Structured data
{
"FirstName" : "Brian",
"LastName" : "Driscoll",
"Age" : 33,
"Children" : [
{
"FirstName" : "Mae",
"LastName" : "Driscoll",
"Age" : 2
},
bool something = (foo == bar) ? true : false;
return something;
@driscollwebdev
driscollwebdev / bad.cs
Created December 4, 2012 14:04
Null + Type checking with cast (bad) and with is operator (good)
//This is bad, don't do this:
if ((SubclassType)BaseClassInstance != null)
{
//this will run, maybe, who knows?
}
@driscollwebdev
driscollwebdev / bad.cs
Created December 4, 2012 14:04
Null + Type checking with cast (bad) and with is operator (good)
//This is bad, don't do this:
if ((SubclassType)BaseClassInstance != null)
{
//this will run, maybe, who knows?
}
@driscollwebdev
driscollwebdev / ObjectExtensions.cs
Created November 8, 2012 14:35
ToJson and ToXml C# Extension Methods
/**
Copyright (c) 2012 Brian Driscoll
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@driscollwebdev
driscollwebdev / Query.cs
Created October 11, 2012 16:30
A generic Query and QueryResult implementation
/**
Copyright (c) 2012 Brian Driscoll
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@driscollwebdev
driscollwebdev / usefulStyles.css
Created October 8, 2012 14:45
CSS to horizontally center an IMG element
img.centered {
display: block;
margin-left: auto;
margin-right: auto;
}
@driscollwebdev
driscollwebdev / Command.cs
Created October 4, 2012 13:33
Generic Command Pattern Implementation in Entity Framework Using System.Data.Entity.DbContext
/**
Copyright (c) 2012 Brian Driscoll
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: