Skip to content

Instantly share code, notes, and snippets.

View akimboyko's full-sized avatar
🙃

Akim Boyko akimboyko

🙃
View GitHub Profile
@akimboyko
akimboyko / gist:4530162
Created January 14, 2013 13:48
How to intercept method with custom attribute using DynamicProxy
void Main()
{
ProxyGenerator proxyGenerator = CreateProxyGenerator();
IService proxy =
proxyGenerator
.CreateInterfaceProxyWithTargetInterface(new Service() as IService, new TracingInterceptorAspect());
proxy.ProcessRequest();
@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:4538388
Created January 15, 2013 12:45
Ninject Contextual Binding for different environments
void Main()
{
// create kernel with conventions
using(var kernel = InitializeKernel())
{
var backend = kernel.Get<IBackend>();
Assert.That(backend, Is.Not.InstanceOfType(typeof(ProdBackend))
.And.InstanceOfType(typeof(TestBackend)));
}
@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:4593576
Created January 22, 2013 10:20
Ninject factory with custom naming instance provider
void Main()
{
using(var kernel = new StandardKernel(new CarModule()))
{
kernel.Load<FuncModule>(); // for sake of LinqPAD
var factory = kernel.Get<ICarFactory>();
Assert.That(factory, Is.Not.Null);
@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
@akimboyko
akimboyko / gist:4622557
Last active February 23, 2022 07:11
Custom scope for Ninject that returns same instance based on first constructor argument. Using both kernel and factory
void Main()
{
using(var kernel = new StandardKernel(new Module()))
{
kernel.Load<FuncModule>(); // for sake of LinqPAD
var instance1 = kernel.Get<MyClass>(new ConstructorArgument("myArg", "test"));
var instance2 = kernel.Get<MyClass>(new ConstructorArgument("myArg", "test"));
Assert.That(instance1, Is.Not.Null.And.SameAs(instance2));
@akimboyko
akimboyko / RoslynScriptProcessing
Last active December 14, 2015 16:29
Processing IEnumerable with Roslyn script. Sample for LinqPAD
void Main()
{
var engine = new ScriptEngine();
new[]
{
typeof (Math).Assembly,
this.GetType().Assembly
}.ToList().ForEach(assembly => engine.AddReference(assembly));
@akimboyko
akimboyko / RoslynAndAnonymousTypes
Last active December 15, 2015 03:28
Roslyn and anonumous types within script
void Main()
{
var engine = new ScriptEngine();
new[]
{
typeof (Math).Assembly,
this.GetType().Assembly
}.ToList().ForEach(assembly => engine.AddReference(assembly));