Skip to content

Instantly share code, notes, and snippets.

View danielmackay's full-sized avatar

Daniel Mackay [SSW] danielmackay

View GitHub Profile
@danielmackay
danielmackay / MvcSelectList.cs
Last active December 27, 2015 15:28
MVC select list optional selected value. #mvc.
private void FillSelectLists(int? parentPageID)
{
ViewBag.ParentPageSelectList = new SelectList(pageBL.GetParentPages(), "PageID", "Title", parentPageID);
}
@danielmackay
danielmackay / DbGeographyModelBinder.cs
Created November 11, 2013 09:20
MVC Model Binder. First we register a ModelBinderProvider. When it runs it calls the DbGeographyModelBinder if it finds a type of DbGeography.
public class DbGeographyModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
string[] latLongStr = valueProviderResult.AttemptedValue.Split(',');
string point = string.Format("POINT ({0} {1})",latLongStr[1], latLongStr[0]);
//4326 format puts LONGITUDE first then LATITUDE
DbGeography result = valueProviderResult == null ? null :
DbGeography.FromText(point,4326);
@danielmackay
danielmackay / DbContextExt.cs
Created November 11, 2013 10:58
Deleting all entities from a table. #ef, #eftable
public static void DeleteAllEntities<T>(this DbContext db)
where T : class
{
var adapter = (IObjectContextAdapter)db;
var objectContext = adapter.ObjectContext;
var sql = string.Format("DELETE FROM {0}", objectContext.GetTableName<T>());
var entityConnection = objectContext.ExecuteStoreCommand(sql);
}
public static string GetTableName<T>(this ObjectContext context)
@danielmackay
danielmackay / fadeVisible.js
Created November 11, 2013 11:06
Knockout fadeVisible binding. #ko
ko.bindingHandlers.fadeVisible = {
init: function(element, valueAccessor) {
var shouldDisplay = valueAccessor();
$(element).toggle(shouldDisplay);
},
update: function(element, valueAccessor) {
// On update, fade in/out
var shouldDisplay = valueAccessor();
shouldDisplay ? $(element).fadeIn() : $(element).fadeOut();
}
@danielmackay
danielmackay / MulticoreJit.cs
Created November 11, 2013 11:07
Enabling multicore JIT. This is "opt-int" for desktop applications and "opt-out" for server environments like ASP.NET.
public App()
{
ProfileOptimization.SetProfileRoot(@"C:\MyAppFolder");
ProfileOptimization.StartProfile("Startup.Profile");
}
@danielmackay
danielmackay / DeepClone.cs
Created November 11, 2013 11:08
Deep clone of an object. #.net
public static class ObjectExt
{
/// <summary>
/// Provides a copy/deep clone of the object
/// </summary>
public static T Copy<T>(this T opSource) where T : class
{
//grab the type and create a new instance of that type
Type opSourceType = opSource.GetType();
var opTarget = Activator.CreateInstance<T>();
@danielmackay
danielmackay / CachedRepository.cs
Created November 11, 2013 11:08
Example of a cached repository.
/// <summary>
/// Provides a cached repository of the DM lists
/// </summary>
internal class CachedRepository
{
private readonly ObjectCache cache = MemoryCache.Default;
private const string CacheName = "DM.Lists";
private const int ExpiryMinutes = 1;
public List<DMList> GetLists()
@danielmackay
danielmackay / EfStoredProc.cs
Created November 11, 2013 11:09
Executing a stored procedure with entity framework. #ef, #sql, #storedproc
public void Create(User user)
{
db.Users.Add(user);
var errors = db.GetValidationErrors().ToList();
if (errors.Count() != 0)
throw new DbEntityValidationException("Validation errors", errors);
int result = db.Database.ExecuteSqlCommand(
"exec dbo.CreateUser @FirstName, @Surname, @Email, @Phone, @Postcode, @IsCustomer, @Suitcase, @TravelTypeId",
new SqlParameter("FirstName", user.FirstName),
@danielmackay
danielmackay / FieldEncryption.sql
Created November 11, 2013 11:09
Field level encryption in a SQL DB.
-- Create SQL Key
create master key encryption
by password = 'Glitch##7'
create certificate globe_cert
with subject = 'globetrotter encryption certificate',
expiry_date = '20200101'
create symmetric key globe_key with algorithm = AES_256
encryption by certificate globe_cert
@danielmackay
danielmackay / MsDeployCli.bat
Created November 11, 2013 11:10
MSDeploy parameters when called from the CLI.
/p:DeployOnBuild=True /p:DeployTarget=MSDeployPublish /p:MSDeployPublishMethod=WMSVC /p:MsDeployServiceUrl=https://192.168.203.248:8172/msdeploy.axd /p:AllowUntrustedCertificate=true /p:DeployIISAppPath=dev2.lav.syd\IlluminateCDS /p:username=lav\build /p:password=lavenderbuild13 /p:Configuration=Dev /p:SkipExtraFilesOnServer=True