Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@akimboyko
akimboyko / gist:4379572
Created December 26, 2012 10:50
Call generic implementation of method using Reflection
void Main()
{
GetPersisterFor(new Foo());
}
public interface IPersister { }
public interface IEntity { }
public class Foo : IEntity { }
public class Bar : IEntity { }
@akimboyko
akimboyko / gist:4437787
Created January 2, 2013 20:41
Ninject, static constructor and named bindings
void Main()
{
using(var kernel = new StandardKernel())
{
kernel
.Bind<IDocumentService>()
.ToMethod(x => CoDocumentService.Create(x.Kernel.Get<IMessage>(),x.Kernel.Get<IClientChannel>("IClientChannel")))
.InTransientScope();
kernel
void Main()
{
// FindBaseClassWith
Assert.That(typeof(DeriviedLeft).FindBaseClassWith(typeof(DeriviedLeft)), Is.EqualTo(typeof(DeriviedLeft)));
Assert.That(typeof(DeriviedLeft).FindBaseClassWith(typeof(SubDeriviedLeft)), Is.EqualTo(typeof(DeriviedLeft)));
Assert.That(typeof(DeriviedLeft).FindBaseClassWith(typeof(DeriviedRight)), Is.EqualTo(typeof(object)));
Assert.That(typeof(DeriviedLeft).FindBaseClassWith(typeof(object)), Is.EqualTo(typeof(object)));
Assert.That(typeof(DeriviedLeft).FindBaseClassWith(typeof(Another)), Is.EqualTo(typeof(object)));
Assert.That(typeof(DeriviedRight).FindBaseClassWith(typeof(DeriviedRight)), Is.EqualTo(typeof(DeriviedRight)));
Assert.That(typeof(DeriviedRight).FindBaseClassWith(typeof(DeriviedLeft)), Is.EqualTo(typeof(object)));
@akimboyko
akimboyko / gist:4457261
Created January 4, 2013 21:47
AOP: handwritten tracing decorator
void Main()
{
TraceDecorator.Aspect(() => StaticLogic.SuccessfulCall());
TraceDecorator.Aspect(() => StaticLogic.ExceptionCall());
TraceDecorator.Aspect(() => StaticLogic.SuccessfulCallWithReturn(42)).Dump();
TraceDecorator.Aspect(() => StaticLogic.ExceptionCallWithReturn(42)).Dump();
}
public static class TraceDecorator
{
using System;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using NUnit.Framework;
namespace Examples
{
public interface IServiceWithDependency
{
@akimboyko
akimboyko / gist:4483835
Created January 8, 2013 13:29
Prepare data for unittests. Deserialize JSON to object hierarchy. Take from http://stackoverflow.com/a/9988494/443366
void Main()
{
JsonConvert.DeserializeObject<Root>(testJson).Dump();
}
readonly string testJson =
"{\"Profile\": [{" +
" \"Name\":\"Joe\"," +
" \"Last\":\"Doe\"," +
" \"Client\":" +
@akimboyko
akimboyko / gist:4483905
Created January 8, 2013 13:40
Load data from XML to in-memory SQLite instance
SQLiteConnection connection = boundSession.GetConnection();
using (var cmd = new SQLiteCommand("PRAGMA foreign_keys = OFF", connection))
{
cmd.ExecuteNonQuery();
}
var sqlLiteUnitTest = new SqlLiteDbUnitTest(connection);
sqlLiteUnitTest.ReadXmlSchema(xsdFile);
@akimboyko
akimboyko / gist:4495440
Last active December 10, 2015 21:29
Inject dependency into PostSharp aspect
[Serializable]
public class TracingAspectAttribute : OnMethodBoundaryAspect
{
// Question #1: Is there any better way to design aspect
// and to inject dependency into it?
public Type AbstractFactoryType { get; set; }
private ILogger Logger { get; set; }
// Compile time validation.
// Question #2: Better approach to ensure during post-compile time
@akimboyko
akimboyko / gist:4502263
Last active December 10, 2015 22:28
Configuration by Convention using Ninject.Extensions.Conventions
void Main()
{
// 4 from your Compositon Root ...
using(var kernel = InitializeKernel())
{
// 4.1 resolve delivery services by names
var upsWorldShip = kernel.Get<IShippingCompanyService>("ShippingUpsWorldShip");
var fedExDesktopApps = kernel.Get<IShippingCompanyService>("ShippingFedExDesktopApps");
// 4.2 delivery processing
@akimboyko
akimboyko / gist:4530019
Last active December 11, 2015 02:19
Ninject.Extensions.Conventions unable to resolve which of two implementation to use
void Main()
{
// create kernel with conventions
using(var kernel = InitializeKernel())
{
// ninject.extensions.conventions would not automaticaly select wich of
// two implementations of IDependency to resolve
var dependency = kernel.Get<IDependency>();
Assert.That(dependency,