Skip to content

Commit

Permalink
added strongly typed default injection InjectFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
omuleanu committed May 27, 2021
1 parent 694300c commit ea38cd3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Tests/InjectFromTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using NUnit.Framework;
using Omu.ValueInjecter;
using Tests.SampleTypes;

namespace Tests
{
[TestFixture]
public class InjectFromTests
{
[Test]
public void InjectStronglyTyped()
{
var c1 = new Customer();
var c2 = c1.InjectFrom(GetCustomer());

Assert.IsTrue(c1 == c2);
}

private static Customer GetCustomer()
{
return new Customer { FirstName = "Art", LastName = "Vandelay", Id = 123, RegDate = DateTime.UtcNow };
}
}
}
6 changes: 3 additions & 3 deletions Tests/LoopInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

using Omu.ValueInjecter;
using Omu.ValueInjecter.Injections;

using Tests.Injections;
using Tests.Misc;
using Tests.SampleTypes;

namespace Tests
Expand All @@ -25,8 +26,7 @@ public void ShouldIgnoreProperties()

private static Customer GetCustomer()
{
var customer = new Customer { FirstName = "Art", LastName = "Vandelay", Id = 123, RegDate = DateTime.UtcNow };
return customer;
return new Customer { FirstName = "Art", LastName = "Vandelay", Id = 123, RegDate = DateTime.UtcNow };
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<Compile Include="InjectFromTests.cs" />
<Compile Include="Injections\CloneInjection.cs" />
<Compile Include="Cloning.cs" />
<Compile Include="Injections\DateTimeToStr.cs" />
Expand Down
18 changes: 16 additions & 2 deletions ValueInjecter/StaticValueInjecter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using Omu.ValueInjecter.Injections;
using System.Reflection;

namespace Omu.ValueInjecter
{
/// <summary>
///
/// </summary>
public static class StaticValueInjecter
{
/// <summary>
/// Default injection, can be replaced
/// </summary>
public static IValueInjection DefaultInjection = new SameNameType();

/// <summary>
/// Injects values from source to target
/// </summary>
Expand Down Expand Up @@ -63,5 +68,14 @@ public static object InjectFrom(this object target, object source)
{
return DefaultInjection.Map(source, target);
}

/// <summary>
/// Inject properties with exact same name and type
/// </summary>
public static TTarget InjectFrom<TTarget>(this TTarget target, object source)
{
target = (TTarget) DefaultInjection.Map(source, target);
return target;
}
}
}

0 comments on commit ea38cd3

Please sign in to comment.