Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
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,
@akimboyko
akimboyko / gist:4530172
Last active December 11, 2015 02:19
How to work with sealed class w/o virtual methods using DynamicProxy
void Main()
{
ProxyGenerator proxyGenerator = CreateProxyGenerator();
IService proxy =
proxyGenerator
// http://kozmic.pl/2009/04/01/castle-dynamic-proxy-tutorial-part-ix-interface-proxy-with-target/
.CreateInterfaceProxyWithTargetInterface(new Service() as IService, new TracingInterceptorAspect());
proxy.ProcessRequest();
@akimboyko
akimboyko / NUnit.Framework.Constraints.Constraint.cs
Last active December 11, 2015 03:28
Build constraints inside a single test suing NUnit.Framework.Constraints.Constraint
void Main()
{
// nunit runner
NUnit.ConsoleRunner.Runner.Main(new string[]
{
Assembly.GetExecutingAssembly().Location,
});
}
public class ClassUnderTest
@akimboyko
akimboyko / gist:4587186
Created January 21, 2013 16:24
Ninject post-poned dependency initialization: factory, lazy loading, combination
void Main()
{
var kernel = new StandardKernel();
kernel.Load<FuncModule>(); // for sake of LinqPAD
kernel.Bind<IDependencyFactory>().ToFactory();
// wire concreet implementation of
kernel
@akimboyko
akimboyko / gist:4594189
Last active December 11, 2015 11:28
Inject dependency into PostSharp aspect with instance scope using factory method There is a problem with parralel unittests of this implementation because of static factory method
// More about aspect life time
// http://www.sharpcrafters.com/blog/post/Day-10-Aspect-Lifetime-Scope-Part-2.aspx
[Serializable]
public class InjectionAspectWithCallbackAttribute : OnMethodBoundaryAspect, IInstanceScopedAspect
{
private ILogger Logger { get; set; }
public static Func<ILogger> InjectLogger { get; set; }
#region OnMethodBoundaryAspect overrides