Skip to content

Instantly share code, notes, and snippets.

@gazlu
gazlu / Retrier.cs
Created December 10, 2013 05:44
Retry logic in c#, for max number of attempts and for expected value.
public class Retrier<TResult>
{
/// <summary>
/// Retry to perform an operation/function
/// </summary>
/// <param name="func">Function to retry</param>
/// <param name="maxRetries">Maximum retires</param>
/// <param name="delayInMilliseconds">Delay on millies, 0 by default</param>
/// <returns>Result of the Function</returns>
public TResult TryBool(Func<TResult> func, int maxRetries, int delayInMilliseconds = 0)
@gazlu
gazlu / CSVHelper.cs
Created July 4, 2015 20:24
Simple.Data ResultSet to CSV
public static class CSVHelper
{
/// <summary>
/// Usage:
/// CSVHelper.ToCsv(
/// ",",
/// resultSet,
/// new List<KeyValuePair<string, string>>
/// {
/// new KeyValuePair<string,string>("caf_no", "'")
@gazlu
gazlu / DataRepository.cs
Created September 4, 2015 05:12
Call Stored Procedure with DbContext without including in EDMX
public class DataRepository<T> : IDataRepository<T>
where T : class, new()
{
private DbContext context;
Logger log = new Logger();
public DataRepository()
{
}
@gazlu
gazlu / select2-custom-ajax
Created October 16, 2015 18:40
Custom ajax implementation with Select2 - angular, xhr friendly
S2.define('select2/data/remoteQuery', [
'./array',
'../utils',
'jquery'
], function (ArrayAdapter, Utils, $) {
function RemoteQueryAdapter($element, options) {
this.ajaxOptions = this._applyDefaults(options.get('remoteQuery'));
if (this.ajaxOptions.processResults != null) {
this.processResults = this.ajaxOptions.processResults;
@gazlu
gazlu / DynamicBundles.cs
Last active October 20, 2015 10:19
Create dynamic script and css bundles right from your views
//@Html.ScriptsBundle("~/bundles/ng-modules", "~/Scripts/vendor/ng/modules", true)
public static class DynamicBundles
{
public static IHtmlString StylesBundle(this HtmlHelper helper, string virtualPath, params string[] additionalPaths)
{
if (BundleTable.Bundles.GetBundleFor(virtualPath) == null)
{
BundleTable.Bundles.Add(new StyleBundle(virtualPath).Include(additionalPaths));
}
return MvcHtmlString.Create(@"<link href=""" + HttpUtility.HtmlAttributeEncode(BundleTable.Bundles.ResolveBundleUrl(virtualPath)) + @""" rel=""stylesheet""/>");
@gazlu
gazlu / react.wrapper.js
Created September 18, 2016 17:17
A wrapper class to access ReactJS components in ES-5 easily, access Router, Route, Link, EventEmitter and Flux.Dispatcher in ES-5
//Include following js files
//"/scripts/react/react.js",
//"/scripts/react/react-dom.js",
//"/scripts/react/ReactRouter.js",
//"/scripts/Flux/Flux.js",
//"/scripts/EventEmitter/EventEmitter.js",
/// <reference path="scripts/react/react.js" />
/// <reference path="scripts/react/react-dom.js" />
/// <reference path="scripts/react/ReactRouter.js" />
@gazlu
gazlu / XMLWrite.cs
Created September 21, 2016 09:18
Write XML in memory to XmlDocument
XmlWriterSettings setting = new XmlWriterSettings();
setting.ConformanceLevel = ConformanceLevel.Auto;
StringBuilder builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder, setting))
{
writer.WriteStartElement("Users");
foreach (var user in users)
{
writer.WriteStartElement("User");
@gazlu
gazlu / RestSharpImageDownload.cs
Created October 6, 2016 18:20
Download Image or file with RestSharp (Rest Sharp)
RestClient restClient = new RestClient(@"http://ia.media-imdb.com/images/M/MV5BMjM5OTQ1MTY5Nl5BMl5BanBnXkFtZTgwMjM3NzMxODE@._V1_SX300.jpg");
var fileBytes = restClient.DownloadData(new RestRequest("#", Method.GET));
File.WriteAllBytes(Path.Combine(directory, "poster-got.jpg"), fileBytes);
@gazlu
gazlu / extract-photos.bat
Created October 15, 2016 10:11
Extract photos from the Video with VLC
vlc "{video path from which you want to exctract photos}" --video-filter=scene --vout=dummy --start-time=1 --scene-ratio=15 --scene-prefix=VTS_01_4-img- --scene-path="{folder path where you want to exctract photos}" vlc://quit
@gazlu
gazlu / multi-insert-identity.sql
Created October 27, 2016 14:57
Insert multiple records and get the identity value
CREATE TABLE #A (ID int Identity(1,1), fn varchar(10))
DECLARE @output TABLE (id int)
Insert into #A (fn)
OUTPUT inserted.ID INTO @output
values ('1'), ('2'), ('3')
select * from @output