Skip to content

Instantly share code, notes, and snippets.

@wasimf
wasimf / AttachToOrGet
Created November 10, 2013 14:06
AttachToOrGet from entity framework context
public static void AttachToOrGet<T>(this ObjectContext context, string entitySetName, ref T entity)
where T : IEntityWithKey
{
ObjectStateEntry entry;
// Track whether we need to perform an attach
bool attach = false;
if (
context.ObjectStateManager.TryGetObjectStateEntry
(
context.CreateEntityKey(entitySetName, entity),
@wasimf
wasimf / LoadImgaeSrc
Created September 8, 2013 07:27
Use Data URIs Instead of Image SRC's
<!-- before... -->
<img src="/images/logo.png" />
<!-- ...after -->
<img src="data: image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fH ...." />
<-- live example at: http://davidwalsh.name/demo/data-uri-php.php -->
@wasimf
wasimf / Compress
Created September 1, 2013 10:23
CompressionService - .Net Gzip based compression service .
byte[] buffer = Encoding.UTF8.GetBytes(text);
var ms = new MemoryStream();
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
var compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
@wasimf
wasimf / shell
Created August 18, 2013 07:30
Shell html
<div>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<ul class="nav" data-bind="foreach: router.navigationModel">
<li data-bind="css: { active: isActive }">
<a data-bind="attr: { href: hash }, html: title"></a>
</li>
</ul>
</div>
</div>
@wasimf
wasimf / shell
Created August 18, 2013 07:29
Durandal shell.js
define(['plugins/router', 'durandal/app'], function (router, app) {
return {
router: router,
activate: function () {
router.map([
{ route: '', title:'Welcome', moduleId: 'viewmodels/welcome', nav: true },
{ route: 'flickr', moduleId: 'viewmodels/flickr', nav: true }
]).buildNavigationModel();
return router.activate();
@wasimf
wasimf / main
Created August 18, 2013 07:26
The main.js module is the first code that gets executed and it is where you configure Durandal and tell it to start up the app.
requirejs.config({
paths: {
'text': '../Scripts/text',
'durandal': '../Scripts/durandal',
'plugins': '../Scripts/durandal/plugins',
'transitions': '../Scripts/durandal/transitions'
}
});
define('jquery', function () { return jQuery; });
@wasimf
wasimf / index
Created August 18, 2013 07:25
Main index html file declaring Durandal application.
<body>
<div id="applicationHost"></div>
<script type="text/javascript" src="/Scripts/require.js" data-main="/App/main"></script>
</body>
@wasimf
wasimf / Base64
Created July 28, 2013 12:51
Basic64 javascript encoding
base64Keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
base64Encode = function (input) {
var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i, output;
output = "";
i = 0;
input = utf8Encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
@wasimf
wasimf / BackupDb
Created July 8, 2013 20:56
Backup database script
DECLARE @Filename varchar(100)
SET @Filename = N'D:\Google Drive\ShelegBackups\IceHouce\IceHouseDB_' +convert(varchar, GetDate(), 102) + '.bak'
BACKUP DATABASE [IceHouseDB] TO DISK = @Filename WITH NOFORMAT,
INIT, NAME = N'IceHouseDB-Full Database Backup', SKIP,
NOREWIND, NOUNLOAD, STATS = 10
declare @backupSetId as int
select @backupSetId = position from msdb..backupset _
where database_name=N'IceHouseDB' and backup_set_id=(select max(backup_set_id)
from msdb..backupset where database_name=N'IceHouseDB' )
@wasimf
wasimf / AspNetEventBroker
Created July 4, 2013 06:17
ASP.NET Event Broker - Event Broker implementation provides a way to broadcast and receive loosely coupled events. Behind the scenes the Event Broker uses the HttpContext.Items to keep track of its information.
using System;
using System.Web;
using System.ComponentModel;
public static class EventBroker
{
private static object EventsKey = new object();
public static void Subscribe<TType>(this HttpContextBase source, object key, EventHandler<TType> value)
where TType : EventArgs