Skip to content

Commit

Permalink
feat: Update container with my needs
Browse files Browse the repository at this point in the history
- Simplify container by removing interfaces
- Added QueueInitialize to Installer binding
- Remove IFactory as a creation strategy. It should be added from the application side instead.
  • Loading branch information
PereViader committed Jun 2, 2024
1 parent e4b6676 commit c774a00
Show file tree
Hide file tree
Showing 92 changed files with 1,133 additions and 1,609 deletions.
3 changes: 2 additions & 1 deletion ManualDi.Main.Tests/ManualDi.Main.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<IsPackable>false</IsPackable>
<RootNamespace>ManualDi.Main.Tests</RootNamespace>
<AssemblyName>ManualDi.Main.Tests</AssemblyName>
Expand Down
6 changes: 3 additions & 3 deletions ManualDi.Main.Tests/TestDiContainerBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void TestQueueDispose()
var action = Substitute.For<Action>();

var container = new DiContainerBuilder()
.WithInstallDelegate(x => x.QueueDispose(action))
.Install(x => x.QueueDispose(action))
.Build();

action.DidNotReceive().Invoke();
Expand All @@ -25,10 +25,10 @@ public void TestQueueDispose()
[Test]
public void TestQueueInitialization()
{
var initializationDelegate = Substitute.For<InitializationDelegate>();
var initializationDelegate = Substitute.For<ContainerDelegate>();

var container = new DiContainerBuilder()
.WithInstallDelegate(x => x.QueueInitialization(initializationDelegate))
.Install(x => x.QueueInitialization(initializationDelegate))
.Build();

initializationDelegate.Received(1).Invoke(Arg.Is<IDiContainer>(container));
Expand Down
4 changes: 2 additions & 2 deletions ManualDi.Main.Tests/TestDiContainerDispose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public void TestDispose()
var instance = new object();
var disposeAction = Substitute.For<Action>();

IDiContainer container = new DiContainerBuilder().WithInstallDelegate(x =>
IDiContainer container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>()
.FromInstance(instance)
.RegisterDispose((o, c) => disposeAction);
.Dispose((o, c) => disposeAction);
}).Build();

_ = container.Resolve<object>();
Expand Down
8 changes: 4 additions & 4 deletions ManualDi.Main.Tests/TestDiContainerFromMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void TestFromInstance()
{
var instance = new object();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromInstance(instance);
Expand All @@ -24,7 +24,7 @@ public void TestFromInstance()
public void TestFromMethod()
{
var instance = new object();
var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromMethod(c => instance);
}).Build();
Expand All @@ -37,7 +37,7 @@ public void TestFromMethod()
public void TestFromContainer()
{
int instance = 5;
var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<int>().FromInstance(instance);
x.Bind<object, int>().FromContainer();
Expand All @@ -50,7 +50,7 @@ public void TestFromContainer()
[Test]
public void TestFromContainerAll()
{
var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<int>().FromInstance(1);
x.Bind<int>().FromInstance(2);
Expand Down
5 changes: 2 additions & 3 deletions ManualDi.Main.Tests/TestDiContainerInitialize.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ManualDi.Main.Initialization;
using NSubstitute;
using NSubstitute;
using NUnit.Framework;

namespace ManualDi.Main.Tests
Expand All @@ -12,7 +11,7 @@ public void TestInitialize()
var instance = new object();
var initializationDelegate = Substitute.For<InitializationDelegate<object>>();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>()
.FromInstance(instance)
Expand Down
2 changes: 1 addition & 1 deletion ManualDi.Main.Tests/TestDiContainerInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void TestInject()
var instance = new object();
var injectMethod = Substitute.For<InjectionDelegate<object>>();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>()
.FromInstance(instance)
Expand Down
8 changes: 4 additions & 4 deletions ManualDi.Main.Tests/TestDiContainerLazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class TestDiContainerLazy
[Test]
public void TestLazy()
{
var builderFunc = Substitute.For<FactoryMethodDelegate<object>>();
var builderFunc = Substitute.For<CreateDelegate<object>>();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromMethod(builderFunc).Lazy();
}).Build();
Expand All @@ -21,9 +21,9 @@ public void TestLazy()
[Test]
public void TestNonLazy()
{
var builderFunc = Substitute.For<FactoryMethodDelegate<object>>();
var builderFunc = Substitute.For<CreateDelegate<object>>();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromMethod(builderFunc).NonLazy();
}).Build();
Expand Down
31 changes: 25 additions & 6 deletions ManualDi.Main.Tests/TestDiContainerMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void TestOnlyKeyMetadata()
var instance1 = new object();
var instance2 = new object();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromInstance(instance1).WithMetadata(nameof(instance1));
x.Bind<object>().FromInstance(instance2).WithMetadata(nameof(instance2));
Expand All @@ -29,7 +29,7 @@ public void TestKeyValueMetadata()
var instance1 = new object();
var instance2 = new object();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromInstance(instance1).WithMetadata("Key", 5);
x.Bind<object>().FromInstance(instance2).WithMetadata("Key", 10);
Expand All @@ -43,19 +43,38 @@ public void TestKeyValueMetadata()
}

[Test]
public void TestKeyValueExpressionMetadata()
public void TestKeyValueTypeBindingExpressionMetadata()
{
var instance1 = new object();
var instance2 = new object();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromInstance(instance1).WithMetadata("Key", 5);
x.Bind<object>().FromInstance(instance2).WithMetadata("Key", 10);
}).Build();

var resolution1 = container.Resolve<object>(b => b.WhereMetadata(x => x.Get<int>("Key") < 6));
var resolution2 = container.Resolve<object>(b => b.WhereMetadata(x => x.Get<int>("Key") > 6));
var resolution1 = container.Resolve<object>(b => b.Where(x => ((int)x.Metadata!["Key"]) < 6));
var resolution2 = container.Resolve<object>(b => b.Where(x => ((int)x.Metadata!["Key"]) > 6));

Assert.That(resolution1, Is.EqualTo(instance1));
Assert.That(resolution2, Is.EqualTo(instance2));
}

[Test]
public void TestKeyValueMetadataExpressionMetadata()
{
var instance1 = new object();
var instance2 = new object();

var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromInstance(instance1).WithMetadata("Key", 5);
x.Bind<object>().FromInstance(instance2).WithMetadata("Key", 10);
}).Build();

var resolution1 = container.Resolve<object>(b => b.WhereMetadata<int>("Key", x => x < 6));
var resolution2 = container.Resolve<object>(b => b.WhereMetadata<int>("Key", x => x > 6));

Assert.That(resolution1, Is.EqualTo(instance1));
Assert.That(resolution2, Is.EqualTo(instance2));
Expand Down
2 changes: 1 addition & 1 deletion ManualDi.Main.Tests/TestDiContainerNestedResolutions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void TestCallOrder()
var childInject = Substitute.For<IChildInject>();
var childInit = Substitute.For<IChildInit>();

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<IParent>()
.FromInstance(parent)
Expand Down
8 changes: 4 additions & 4 deletions ManualDi.Main.Tests/TestDiContainerNonGenericResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class TestDiContainerNonGenericResolve
[Test]
public void TestResolveWithoutConstraint()
{
var container = new DiContainerBuilder().WithInstallDelegate(b =>
var container = new DiContainerBuilder().Install(b =>
{
b.Bind<int>().FromInstance(5);
}).Build();
Expand All @@ -21,7 +21,7 @@ public void TestResolveWithoutConstraint()
[Test]
public void TestResolveWithConstraint()
{
var container = new DiContainerBuilder().WithInstallDelegate(b =>
var container = new DiContainerBuilder().Install(b =>
{
b.Bind<int>().FromInstance(2).WithMetadata("A");
b.Bind<int>().FromInstance(5).WithMetadata("B");
Expand All @@ -35,7 +35,7 @@ public void TestResolveWithConstraint()
[Test]
public void TestResolveAllWithoutConstraint()
{
var container = new DiContainerBuilder().WithInstallDelegate(b =>
var container = new DiContainerBuilder().Install(b =>
{
b.Bind<int>().FromInstance(2);
b.Bind<int>().FromInstance(5);
Expand All @@ -49,7 +49,7 @@ public void TestResolveAllWithoutConstraint()
[Test]
public void TestResolveAllWithConstraint()
{
var container = new DiContainerBuilder().WithInstallDelegate(b =>
var container = new DiContainerBuilder().Install(b =>
{
b.Bind<int>().FromInstance(2).WithMetadata("A");
b.Bind<int>().FromInstance(5).WithMetadata("B");
Expand Down
2 changes: 1 addition & 1 deletion ManualDi.Main.Tests/TestDiContainerNonGenericResolveAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void TestResolveAllEmptyReturnsEmpty()
public void TestResolveAllDifferentTypeList()
{
var child = new Child();
var container = new DiContainerBuilder().WithInstallDelegate(b =>
var container = new DiContainerBuilder().Install(b =>
{
b.Bind<Child>().FromInstance(child);
}).Build();
Expand Down
6 changes: 3 additions & 3 deletions ManualDi.Main.Tests/TestDiContainerParent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void TestResolve()
var instance = new object();

var parentContainer = new DiContainerBuilder()
.WithInstallDelegate(x =>
.Install(x =>
{
x.Bind<object>().FromInstance(instance);
})
Expand All @@ -32,15 +32,15 @@ public void TestResolveAll()
var instanceChild = new object();

var parentContainer = new DiContainerBuilder()
.WithInstallDelegate(x =>
.Install(x =>
{
x.Bind<object>().FromInstance(instanceParent);
})
.Build();

var childContainer = new DiContainerBuilder()
.WithParentContainer(parentContainer)
.WithInstallDelegate(x =>
.Install(x =>
{
x.Bind<object>().FromInstance(instanceChild);
})
Expand Down
8 changes: 4 additions & 4 deletions ManualDi.Main.Tests/TestDiContainerScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class TestDiContainerScope
[Test]
public void TestSingle()
{
var factoryMethodDelegate = Substitute.For<FactoryMethodDelegate<object>>();
var factoryMethodDelegate = Substitute.For<CreateDelegate<object>>();
var injectionDelegate = Substitute.For<InjectionDelegate<object>>();

factoryMethodDelegate.Invoke(Arg.Any<IDiContainer>()).Returns(c => new object());

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromMethod(factoryMethodDelegate).Inject(injectionDelegate).Single();
}).Build();
Expand All @@ -30,12 +30,12 @@ public void TestSingle()
[Test]
public void TestTransient()
{
var factoryMethodDelegate = Substitute.For<FactoryMethodDelegate<object>>();
var factoryMethodDelegate = Substitute.For<CreateDelegate<object>>();
var injectionDelegate = Substitute.For<InjectionDelegate<object>>();

factoryMethodDelegate.Invoke(Arg.Any<IDiContainer>()).Returns(c => new object());

var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<object>().FromMethod(factoryMethodDelegate).Inject(injectionDelegate).Transient();
}).Build();
Expand Down
4 changes: 2 additions & 2 deletions ManualDi.Main.Tests/TestDiContianerTryResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class TestDiContianerTryResolve
[Test]
public void TestTryResolveGenericSuccess()
{
var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<int>().FromInstance(1);
}).Build();
Expand All @@ -29,7 +29,7 @@ public void TestTryResolveGenericFailure()
[Test]
public void TestTryResolveNonGenericSuccess()
{
var container = new DiContainerBuilder().WithInstallDelegate(x =>
var container = new DiContainerBuilder().Install(x =>
{
x.Bind<int>().FromInstance(1);
}).Build();
Expand Down
25 changes: 25 additions & 0 deletions ManualDi.Main/Binding/DiContainerBindingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace ManualDi.Main
{
public static class DiContainerBindingExtensions
{
public static TypeBinding<TInterface, TInterface> Bind<TInterface>(this DiContainerBindings diContainerBindings)
{
TypeBinding<TInterface, TInterface> typeBinding = new();
diContainerBindings.AddBinding(typeBinding);
return typeBinding;
}

public static TypeBinding<TInterface, TConcrete> Bind<TInterface, TConcrete>(this DiContainerBindings diContainerBindings)
{
TypeBinding<TInterface, TConcrete> typeBinding = new();
diContainerBindings.AddBinding(typeBinding);
return typeBinding;
}

public static TypeBinding<TInterface, TConcrete> Bind<TInterface, TConcrete>(this DiContainerBindings diContainerBindings, TypeBinding<TInterface, TConcrete> typeBinding)
{
diContainerBindings.AddBinding(typeBinding);
return typeBinding;
}
}
}
24 changes: 24 additions & 0 deletions ManualDi.Main/Binding/ITypeBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using ManualDi.Main.Scopes;

namespace ManualDi.Main
{
public delegate T CreateDelegate<out T>(IDiContainer diContainer);
public delegate void InitializationDelegate<in T>(T instance, IDiContainer container);
public delegate void InjectionDelegate<in T>(T instance, IDiContainer diContainer);

public interface ITypeBinding
{
Type InterfaceType { get; }
Type ConcreteType { get; }
ITypeScope TypeScope { get; set; }
bool IsLazy { get; set; }
public Dictionary<object, object>? Metadata { get; set; }

object Create(IDiContainer container);
public bool NeedsInitialize { get; }
void Initialize(object instance, IDiContainer container);
void Inject(object instance, IDiContainer container);
}
}
Loading

0 comments on commit c774a00

Please sign in to comment.