Skip to content

Commit

Permalink
Implement Ability to await Bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
KallynGowdy committed Jun 22, 2015
1 parent ae091fb commit a1fbd48
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Better and more natural way of defining dependencies in .Net, LINDI is a languag
- [ ] Binding Scopes
- [ ] Automatic Factory Generation
- [ ] Named Bindings
- [ ] `await` keyword for resolving dependencies
- [x] `await` keyword for resolving dependencies

## Issues
Submit new issues as needed, reuse non-closed issues if possible.
Expand Down
1 change: 1 addition & 0 deletions src/LINDI.Core/IBinding.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Runtime.CompilerServices;
using Lindi.Core.Bindings;

namespace Lindi.Core
Expand Down
4 changes: 3 additions & 1 deletion src/LINDI.Core/LINDI.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Lindi.Core</RootNamespace>
<AssemblyName>Lindi.Core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Fasterflect, Version=2.1.3.0, Culture=neutral, PublicKeyToken=38d18473284c1ca7, processorArchitecture=MSIL">
Expand Down
12 changes: 12 additions & 0 deletions src/LINDI.Core/Linq/BindingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Lindi.Core.Bindings;

Expand Down Expand Up @@ -55,5 +57,15 @@ public static IBinding<T> GroupBy<T>(this IBinding<T> t, Func<T, bool> e)
{
return null;
}

/// <summary>
/// Gets an awaiter used to await the resolution of this binding.
/// </summary>
/// <returns></returns>
public static TaskAwaiter<T> GetAwaiter<T>([NotNull] this IBinding<T> binding)
{
if (binding == null) throw new ArgumentNullException(nameof(binding));
return Task.FromResult(binding.Resolve()).GetAwaiter();
}
}
}
40 changes: 40 additions & 0 deletions src/LINDI.Tests/AwaitableBindingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lindi.Core;
using Lindi.Core.Linq;
using Xunit;
using Xunit.Abstractions;
using static Lindi.Core.LindiMethods;

namespace Lindi.Tests
{
public class AwaitableBindingTests
{

[Fact]
public async void Test_Binding_Is_Awaitable()
{
var binding = from value in Bind<ISample>()
select new Sample();

var v = await binding;

Assert.NotNull(v);
Assert.IsType<Sample>(v);
}

[Fact]
public async void Test_Get_Awaiter_On_Null_Binding_Throws_ArgumentNullException()
{
IBinding<ISample> binding = null;

await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
var v = await binding;
});
}
}
}
1 change: 1 addition & 0 deletions src/LINDI.Tests/LINDI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AwaitableBindingTests.cs" />
<Compile Include="BindToConstructorTests.cs" />
<Compile Include="ConceptTests.cs" />
<Compile Include="HasSample.cs" />
Expand Down

0 comments on commit a1fbd48

Please sign in to comment.