Skip to content

Instantly share code, notes, and snippets.

Avatar
🏁

Wes Higbee g0t4

🏁
View GitHub Profile
@g0t4
g0t4 / gist:8718256
Last active Aug 29, 2015
Razor allow custom html builders to use @ syntax and not encode the output
View gist:8718256
// Out of the box, WebViewPage essentially does this to write content:
Output.Write(HttpUtility.HtmlEncode(content));
// And HttpUtility.HtmlEncode will encode anything not marked with the IHtmlString interface
// If we have a custom html builder we have to use something like this:
@Html.Raw(safeBuilder)
// This is a lot of extra typing
@g0t4
g0t4 / hackNancyRazorPageBaseType
Last active Aug 29, 2015
Hack to set a custom base view with Nancy and Razor
View hackNancyRazorPageBaseType
// At this time Nancy's Razor view engine doesn't let you set a default base class like ASP.NET web pages does with pageBaseType, if you want to hack one in here is how:
// First create your custom type, this one writes out hack for everything, so obviously remove that unless you want that :)
public abstract class MyRazorPage<T> : NancyRazorViewBase<T>
{
public override void Write(object value)
{
base.WriteLiteral("hack");
}
@g0t4
g0t4 / gist:30a5d7f5a07146d6bad3
Created Oct 15, 2014
Null safety, proving something is null in kotlin and then not needing to deal with it thereafter
View gist:30a5d7f5a07146d6bad3
// In Kotlin, I can force the source of a reference to ensure it's not null, that way every thing thereafter doesn't have to deal with null references
// This is what I refer to as fixing things up stream, of course when appropriate.
// Typically sources of references don't both to check for null and even if they do, they can't make explicit that they've taken care of that check. So every consumer of that reference theoretically may need to check.
// In this example, there's no reason the userId, password and serverUrl should ever be null. Unfortunately because of interop with java, that's not a contractual guarantee.
// So at the point that I produce these values for my own use, I perfom the check in one place, stop everything if my assumption is not valid, and otherwise be on my merry way never to worry about null references after this point
abstract class Restore(val runner: BuildRunnerContext) {
val logger: SimpleBuildLogger = runner.getBuild().getBuildLogger()
val client: TeamCityClient
{
@g0t4
g0t4 / ArgumentBuilder.kt
Created Oct 24, 2014
Demonstrating one off extension functions in Kotlin for improving readability
View ArgumentBuilder.kt
public class ArgumentBuilder(private val runnerParameters: Map<String, String>) {
public fun build(): List<String> {
return arrayListOf<String>()
.addIfSet("--Project", RunnerSettings.PROJECT_FILE)
.addIfSet("--Output", RunnerSettings.OUTPUT)
.addFlagIfSet("--Flag1", RunnerSettings.FLAG_1)
.addFlagIfSet("--Flag2", RunnerSettings.FLAG_2)
.addFlagIfSet("--Flag3", RunnerSettings.FLAG_3)
.addMultipleIfSet("--InputFiles", RunnerSettings.INPUTS)
@g0t4
g0t4 / gist:a97bcc64dff4ac9394ef
Created Nov 5, 2014
Building an QueryAnalysis instances that contain the results of compiling and executing the query, then using this to find rules and rule violations
View gist:a97bcc64dff4ac9394ef
private QueryAnalysis[] GetQueriesToAnalyze()
{
var queries = _Project.GetRulesInProjectFileAndInRuleFilesAndDeclaredInSourceCode(_Analysis.RulesExtractedFromCode).RootParent;
var activeQueries = queries.GetActiveQueries();
var compiledQueriesByQueryString = GetCompiledQueriesByQueryString(activeQueries);
var justMyCode = queries.ComputeJustMyCode(_Analysis.CodeBase);
return activeQueries
.Select(query => new QueryAnalysis(query, compiledQueriesByQueryString[query.QueryString], justMyCode, _Project))
.ToArray();
@g0t4
g0t4 / gist:1074245
Created Jul 10, 2011
Rake task to install missing packages from nuget when not checking in packages
View gist:1074245
desc "Setup dependencies for nuget packages"
task :dep do
package_folder = File.expand_path('src\\packages')
packages = FileList["**/packages.config"].map{|f| File.expand_path(f)}
packages.each do |file|
sh %Q{nuget install #{file} /OutputDirectory #{package_folder}}
end
end
@g0t4
g0t4 / NotifyIfTakesMoreThan
Created Sep 20, 2012
NotifyIfTakesMoreThan
View NotifyIfTakesMoreThan
// Sometimes it's easier to make assumptions about performance as a trade off for simplified code, but why not make it easy to notify us when that assumption is violated? Next step might be StopAndNotify
// Usage
using (new NotifyIfTakesMoreThan(TimeSpan.FromMinutes(5), "XYZ Import - checking all data instead of more complex code to optimize it"))
{
//operation
}
// Implementation
@g0t4
g0t4 / gist:3790082
Created Sep 26, 2012
Mapping a collection of Guids in FluentNHibernate
View gist:3790082
public class Approved
{
public virtual Guid Id { get; set; }
public virtual IEnumerable<Guid> EntryIds { get; set; }
}
// Mapping, Table specifies the table name to use, element specifies the column the Guid is stored in, KeyColumn specifies the foreign key in the table to link back to the parent Approved item
HasMany(x => x.EntryIds)
.Table("ApprovedEntryIds")
@g0t4
g0t4 / gist:3790130
Created Sep 26, 2012
Mapping a collection of Components in FluentNhibernate
View gist:3790130
public class Approved
{
public virtual Guid Id { get; set; }
public virtual IEnumerable<Entry> Entries { get; set; }
}
public class Entry
{
public virtual Guid Id { get; set; }
public virtual Guid Description { get; set; }
@g0t4
g0t4 / gist:4261025
Created Dec 11, 2012
KISS & AJAX Deletes
View gist:4261025
// I run across this type of code often (note ASP.Net MVC)
[HttpPost]
public string Delete(ObjectId id)
{
var record = _Database.Get<Record>(id);
if (record == null)
{
return "No matching record!";
}