Category: C#

Url Redirect for moved blog

namespace Director { public class HttpModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(this.context_BeginRequest); } private void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; if (context.Request.FilePath.TrimEnd(‘/’).Length > 0 && !File.Exists(context.Request.PhysicalPath)) { context.Response.Redirect( $”http://scrapbook.qujck.com{context.Request.FilePath}”, true); } } public void Dispose() { } } } <system.webServer> <modules runAllManagedModulesForAllRequests=”true”> <add name=”HttpModule” type=”Director.HttpModule” />… Read more →

Holistic Abstractions (Take 2)

In this post I will outline an updated perspective on Commands and Queries with respect to applying cross-cutting concerns (aka Aspects or Decorators). If you are unfamiliar with these two patterns then please see these posts here and here for a great introduction. Bertrand Meyer defines CQS as: every method should either be a command that performs an action, or… Read more →

dynamic code generation in c#

Copied directly from ayende.com Given a Dictionary<string, string>, convert that dictionary into a type in as performant a manner as possible. The conversion will happen many time, and first time costs are acceptable. public static Func<Dictionary<string, string>, T> Generate<T>() where T : new() { var dic = Expression.Parameter(typeof (Dictionary<string, string>), “dic”); var tmpVal = Expression.Parameter(typeof (string), “tmp”); var args =… Read more →

LINQ: Group a list of strings into chunks

LINQ: Group a list of strings into chunks, dividing the chunks by a whole-line marker (in the example “<Custom>”) public static partial class Queries { public static IEnumerable<string[]> Execute( this IQueryHandler<DataChunker, IEnumerable<string[]>> handler, IEnumerable<string> data) { return handler.Execute(new DataChunker(data)); } public class DataChunker : IQuery<IEnumerable<string[]>> { public DataChunker(IEnumerable<string> data) { this.Data = data; } public IEnumerable<string> Data { get; set;… Read more →

Give background threads time to complete in IIS

Thanks to this post public sealed class OutOfProcessMediatorDecorator<TCommand> : IMediatingHandler<TCommand>, IRegisteredObject where TCommand : IMediator { private readonly IMediatingHandler<TCommand> decorated; private readonly Container container; public OutOfProcessMediatorDecorator( IMediatingHandler<TCommand> decorated, Container container) { this.decorated = decorated; this.container = container; } public void Execute(TCommand command) { HostingEnvironment.RegisterObject(this); var task = Task.Factory.StartNew(() => { using(container.BeginLifetimeScope()) { try { this.decorated.Execute(command); } finally { HostingEnvironment.UnregisterObject(this); }… Read more →

System.Windows.Controls.WebBrowser

interaction from javascript in the Document to external C# code [ComVisible(true)] public class ScriptProxy { // This method can be called from JavaScript. public void AMethod(string message) { MessageBox.Show(message); } } this.webBrowser1.ObjectForScripting = new ScriptProxy(); <input type=”button” value=”Go Again!” onclick=”window.external.AMethod(‘Hello’);” /> subscriber to browser events thanks to this answer [ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDispatch)] public class EventListener { [DispId(0)] public void NameDoesNotMatter(object data)… Read more →

NDepend, a second look

After my previous post regarding NDepend I have decided to give it another go, this time on a new project. I am using Visual Studio 2015 for the first time so need to install NDepend for VS2015; this is now very easy after setting things up last time. Navigate to the NDepend folder, run NDepend.VisualStudioExtension.Installer.exe and click install for VS… Read more →

NDepend, a first look

After reading @DaedTech’s very persuasive arguments for static code analysis I have decided to give NDepend a go. Download and install is slightly fiddly, you get a zip file with a number of files. Once you have found the instructions on the web site it takes just a couple of minutes to get set up. I analysed my current project… Read more →

Property Copy – http://stackoverflow.com/a/2624847/1515209

using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; namespace MiscUtil.Reflection { /// <summary> /// Generic class which copies to its target type from a source /// type specified in the Copy method. The types are specified /// separately to take advantage of type inference on generic /// method arguments. /// </summary> public static class PropertyCopy<TTarget> where TTarget : class, new()… Read more →