Skip to content

Instantly share code, notes, and snippets.

View SoulFireMage's full-sized avatar

Richard Griffiths SoulFireMage

View GitHub Profile
@SoulFireMage
SoulFireMage / Datalayer T4 template
Created January 15, 2018 17:34
Datalayer T4 template for use with Generic Save (MVC 5)
/// T4 file to generate some basic methods for a datalayer class. I like to keep Entity Framework inside of the Datalayer only and leave a fairly simple set of methods exposed outside of it.
///Works with my Generic save class
<#@ template language="C#" debug="false" hostspecific="true" compilerOptions="/langversion:6" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Data.Entity.Design" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ include file="EF6.Utility.CS.ttinclude"#>
<#@ import namespace="System.Data.Entity.Design" #>
<#@ import namespace="System.Data.Entity.Design.PluralizationServices" #>
@SoulFireMage
SoulFireMage / Generic Save
Created January 15, 2018 17:31
Generic Save for use in MVC projects builts with my custom T4 files. EF 6.1+
///Generic Save class for use in the Datalayer where I'm reluctant to couple the Model layer to EF!
///Please see the T4 generator for creating files that work with this.
namespace DataLayer.Data
{
public class GenericSave<T> where T : ICrud
{
public static T Update(T item)
{
@SoulFireMage
SoulFireMage / Generic Save and Find for DBML (Linq to SQL projects)
Last active September 2, 2017 10:28
Generic Update Class for Legacy VB projects that use Linq to SQL (DBML
'''I work with a lot of legacy projects that use Linq to Sql and a DBML file. They are typically one layer that I turn into two layers.
'''As they are large winforms projects that are in daily use in business systems, there isn't a need to force the project into the latest
'''architectural trends - whether that be moving to WPF or migrating to Entity Framework.
'''Instead I have this generic central class for the datalayer merely to centralise where ALL the context work happens.
'''Then using a mix of T4 templates, reflection and old fashioned code generation (in LinqPad!), I'll generate all the gruntwork classes.
'''In the same move, basic tests can be generated.
'''This allows me to have a win-win scenario in such projects - either A)I've centralised the logic for the database access, context lifetime and thus
'''left myself with a reasonable to maintain datalayer class.
'''Or B)I've made it far simpler to work through the Winforms layer and make decisions on whether to migrate stuff to WPF or something
@SoulFireMage
SoulFireMage / BlogTemplate.fs
Created January 13, 2016 13:53
World's most basic HTML blog generator.
//Might be the world's simplest HTML page generator, however, I got fedup of tracking all the markup I needed to make one of my typical pages.
let NL = System.Environment.NewLine
let Tab = " "
[<Literal>]
let head = """<!--Template for blog -->
<!DOCTYPE html5>
<html>
<head>
@SoulFireMage
SoulFireMage / Dynpinvoke.fs
Last active January 12, 2016 12:16
Example from Daniel Fabian of using his Dynamic pinvoke library - gist created for blog!
//By Daniel Fabian
open FSharp.InteropServices.Dynamic
let user32 = Library("user32.dll")
(user32?MessageBox(0, "Hello world", "MyTitle", 0) : unit)
@SoulFireMage
SoulFireMage / PowerRule.js
Created January 7, 2016 12:13
Very simple example of using mathjax in Javascript
<script>
//Day 2 learning Javascript
//Very simple evaluation of a power rule - note, it's NOT a true evaluation: you can't check the answer correctly in the code this way.
function Calculation(){
let base = Math.floor((Math.random() * 100) + 1);
let exp = Math.floor((Math.random() * 20) + 1);
this.printQuestion = function(){
let calcString = `$$f(x)=${base}x^{${exp}}$$`;
document.writeln(calcString);
};
@SoulFireMage
SoulFireMage / bitmapToStream.fs
Last active December 24, 2015 09:36
Save bitmap to stream routines used in the advent snowflake project
//The flakes need to be a stream. Remember to dispose bitmaps :)
let bitmapSave (bitmap :Bitmap) =
let stream = new System.IO.MemoryStream()
bitmap.Save(stream, Imaging.ImageFormat.Png)
bitmap.Dispose()
stream.Position <- 0L
stream
//we need a graphics object to draw with and a bitmap to save onto.
//Return a tuple of graphics with its associated bitmmap
@SoulFireMage
SoulFireMage / SnowflakesWPF.fs
Created December 24, 2015 09:12
Phil Trelford did this one on the train: Snowflakes using WriteableBitmap
//#r "WriteableBitmapEx.Wpf.dll" // requires custom version with DrawString
//Email me if you want the custom dll - if I haven't already dumped it on Github!
module flake
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Media.Imaging
open System.Windows.Threading
@SoulFireMage
SoulFireMage / Shader.fs
Created December 24, 2015 08:58
Part of DXPlay.fs showing the bit you code to make shapes :)
let computeShader =
let code = @"
RWTexture2D<float4> Output;
[numthreads(32, 32, 1)]
void main( uint3 threadID : SV_DispatchThreadID )
{
for (int i = 1.0f; i <= 1280.0f; i+=320.0f){
Output[threadID.xy] = float4(threadID.xy / i, 0, 1);
}
@SoulFireMage
SoulFireMage / DXPlay.fs
Created December 24, 2015 08:55
A quick attempt to learn to use Shaders
open SlimDX
open SlimDX.D3DCompiler
open SlimDX.Direct3D11
open SlimDX.Windows
open SlimDX.DXGI
open System.Windows.Forms
open System.Drawing
[<EntryPoint>]
let main args =
let form = new RenderForm("Test Window")