Skip to content

Commit

Permalink
Merge pull request #9 from mavickers/feature/new-to-class
Browse files Browse the repository at this point in the history
Change package shape from new() to class.
  • Loading branch information
mavickers authored Aug 16, 2022
2 parents e544833 + f2d8532 commit 9dae114
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
10 changes: 10 additions & 0 deletions Cargo.Tests/Unit/Common/ContentModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public ContentModel1()
Int1 = 0;
}
}

public class ContentModel2
{
public string String1 { get; set; }

public ContentModel2(string string1)
{
String1 = string1;
}
}
}
29 changes: 18 additions & 11 deletions Cargo.Tests/Unit/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ public class Package
[Fact]
public void Instantiation()
{
Package<ContentModel1> package;
Package<ContentModel1> package1;
Package<ContentModel2> package2;

var contents = new ContentModel1();
var contents1 = new ContentModel1();

Assert.Throws<ArgumentException>(() => package = Cargo.Package.New<ContentModel1>());
Assert.Throws<ArgumentException>(() => package1 = Cargo.Package.New<ContentModel1>());

package = Cargo.Package.New<ContentModel1>(contents);
package1 = Cargo.Package.New<ContentModel1>(contents1);

Assert.False(package.IsAborted);
Assert.False(package.IsErrored);
Assert.Null(package.LastStationResult);
Assert.NotNull(package.Results);
Assert.Empty(package.Results);
Assert.Equal(package.Contents, contents);
Assert.Null(package.AbortedWith);
Assert.False(package1.IsAborted);
Assert.False(package1.IsErrored);
Assert.Null(package1.LastStationResult);
Assert.NotNull(package1.Results);
Assert.Empty(package1.Results);
Assert.Equal(package1.Contents, contents1);
Assert.Null(package1.AbortedWith);

var contents2 = new ContentModel2("testing123");

package2 = Cargo.Package.New<ContentModel2>(contents2);

Assert.Equal("testing123", package2.Contents.String1);
}
}
}
8 changes: 4 additions & 4 deletions Cargo/Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace LightPath.Cargo
{
public static class Bus
{
public static Bus<TContent> New<TContent>() where TContent : new()
public static Bus<TContent> New<TContent>() where TContent : class
{
return Bus<TContent>.New();
}

internal static Bus<TContent> SetAndReturn<TContent>(this Bus<TContent> bus, string propertyName, object value) where TContent : new()
internal static Bus<TContent> SetAndReturn<TContent>(this Bus<TContent> bus, string propertyName, object value) where TContent : class
{
var property = typeof(Bus<TContent>).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance);

Expand All @@ -25,7 +25,7 @@ public static class Bus
}
}

public class Bus<TContent> where TContent : new()
public class Bus<TContent> where TContent : class
{
private Type _finalStation { get; set; }
private Package<TContent> _package { get; set; }
Expand Down Expand Up @@ -122,7 +122,7 @@ public Bus<TContent> WithService<TService>(TService service)
return this;
}

public Bus<TContent> WithStation<TStation>() where TStation : new()
public Bus<TContent> WithStation<TStation>() where TStation : class
{
_stations.Add(typeof(TStation));

Expand Down
2 changes: 1 addition & 1 deletion Cargo/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LightPath.Cargo
{
public static class Package
{
public static Package<TContents> New<TContents>(params object[] parameters) where TContents : new()
public static Package<TContents> New<TContents>(params object[] parameters) where TContents : class
{
return Package<TContents>.New(parameters);
}
Expand Down
2 changes: 1 addition & 1 deletion Cargo/Station.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum Output

public static class Result
{
public static Result<T> New<T>(Station<T> station, Output output, Exception exception = null) where T : new()
public static Result<T> New<T>(Station<T> station, Output output, Exception exception = null) where T : class
{
return new Result<T>(station, output, exception);
}
Expand Down

0 comments on commit 9dae114

Please sign in to comment.