Category: C#

Extract details from an Expression

I wanted to come up with a way of establishing the type name and property name when applying general validations within my code. So, for example for this validation:. var bool = Requires.IsNotNull(user.lastName) I have to explicitly code the exception: throw NullReferenceException(“User.lastName”) What I’d like is for all this to happen when I declare the test, without double typing the… Read more →

Multiple Dispatch, Double Dispatch and the Visitor Pattern

I’ve been looking into the Visitor pattern and figuring out how to make it work with overloaded methods. Starting with the abstractions interface IVisitor { void Visit(AbstractElement element); } abstract class AbstractElement { public void Accept(IVisitor visitor) { visitor.Visit(this); } } And initial implementations class TestElement1 : AbstractElement { } partial class TestVisitor1 : IVisitor { public string visitedMethod =… Read more →

Dynamic objects part II

I decided to extend my DynamicObject class to enable me to do this: [Test] public void MyDynamicObject_SetWithSetter_CanGetByReference() { // Arrange dynamic obj = new MyDynamicObject(); obj[“property1”] = true; // Act bool value = obj.property1; // Assert Assert.That(value); } And this: [Test] public void MyDynamicObject_SetDelegateWithSetter_CanCallDelegate() { // Arrange dynamic obj = new MyDynamicObject(); Func<bool> d = () => true; obj[“property1”] =… Read more →

Dynamic Objects in a Generic World

I have a situation where I want a dynamic object that can used within generic classes and methods. I have a generic service that accepts any instance that implements a predefined interface: public interface IMyInterface { bool enabled { get; set; } } public class MyGenericService<T> where T : IMyInterface { public void Process(T obj) { obj.enabled = true; }… Read more →

Simple Injector – How to discover the underlying Implementation Type of a decorated instance when calling GetAllInstances

I asked the question “How to discover the underlying Implementation Type of a decorated instance when calling GetAllInstances?” on stackoverflow and got a perfectly valid answer – here’s a sample implementation to prove that it would work. Start with some test abstractions and implementations: public interface ICommandHandler<TCommand> { void Execute(); } public class A { } public class B {… Read more →

Runtime Csv Exporter using Dapper

Today I decided to create a run-time compiled Csv generator and compare it to a compile-time version of the same code. It works a treat with little noticeable difference in performance over 10000 runs (of a very small table) These are the compile time definitions: public class Client { public long id { get; set; } public string forenames {… Read more →

Convert Html to Tiff

using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Forms; using System.Windows.Media.Imaging; class Program { private static WebBrowser InitialiseBrowser(string source) { // create a hidden web browser, which will navigate to the page WebBrowser browser = new WebBrowser(); browser.ScrollBarsEnabled = false; // we don’t want scrollbars on our image browser.ScriptErrorsSuppressed… Read more →

markdown in BlogEngine.NET

BlogEngine.NET has half baked support for markdown. You can install MarkdownFormatterExtension but this doesn’t do anything unless you enter raw text into the HTML editor view. There is an additional problem that the posts are saved as Xml and as such all the white space is removed during the save process. You can get around this by tweaking the Post… Read more →

NOT using repository pattern, use the ORM as is (EF)

This was originally posted as an answer to a question on stackoverflow here IMO both the Repository abstraction and the UnitOfWork abstraction have a very valuable place in any meaningful development. People will argue about implementation details, but just as there are many ways to skin a cat, there are many ways to implement an abstraction. Your question is specifically… Read more →