Skip to content

Commit

Permalink
Able to override inline dependencies with explicit args. Closes GH-519
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Feb 20, 2017
1 parent 1a88b29 commit f7d08ce
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/CommonAssemblyInfo.cs
Expand Up @@ -3,7 +3,7 @@
[assembly: AssemblyDescription("IoC Container for .Net")]
[assembly: AssemblyProduct("StructureMap")]
[assembly: AssemblyCopyright("Copyright 2004-2016 Jeremy D. Miller, Joshua Flanagan, Frank Quednau, Tim Kellogg, et al. All rights reserved.")]
[assembly: AssemblyTrademark("ee519455b8cfd21b5deaf505dd35ec1212f98f6e")]
[assembly: AssemblyVersion("4.4.2.50833")]
[assembly: AssemblyFileVersion("4.4.2.50833")]
[assembly: AssemblyInformationalVersion("4.4.2.50833")]
[assembly: AssemblyTrademark("1a88b2974d481918cc8360c9d0825d9cb5c887ff")]
[assembly: AssemblyVersion("4.4.2.50921")]
[assembly: AssemblyFileVersion("4.4.2.50921")]
[assembly: AssemblyInformationalVersion("4.4.2.50921")]
60 changes: 60 additions & 0 deletions src/StructureMap.Testing/Bugs/Bug_519_Uri_as_explicit_arg.cs
@@ -0,0 +1,60 @@
using System;
using Shouldly;
using Xunit;

namespace StructureMap.Testing.Bugs
{
public class Bug_519_Uri_as_explicit_arg
{
[Fact]
public void restclient_uri_should_not_be_null()
{
var container = new Container(_ =>
{
_.ForConcreteType<RestClientTest>()
.Configure
.Ctor<string>("userAgent")
.Is(() => "UA")
.Ctor<Uri>("uri")
.Is(() => null);
});


var rc = container.With("uri").EqualTo(new Uri("http://www.google.com")).GetInstance<RestClientTest>();

rc.Uri.ShouldBe(new Uri("http://www.google.com"));
}

[Fact]
public void restclient_uris_should_be_different()
{
var container = new Container(_ =>
{
_.ForConcreteType<RestClientTest>()
.Configure
.Ctor<string>("userAgent")
.Is(() => "UA")
.Ctor<Uri>("uri")
.Is(() => null);
});


var rc1 = container.With("uri").EqualTo(new Uri("http://www.google.com")).GetInstance<RestClientTest>();
var rc2 = container.With("uri").EqualTo(new Uri("http://www.amazon.com")).GetInstance<RestClientTest>();

Assert.True(rc1.Uri.AbsoluteUri != rc2.Uri.AbsoluteUri);
}

public class RestClientTest
{
public string UserAgent { get; set; }
public Uri Uri { get; set; }

public RestClientTest(string userAgent, Uri uri)
{
UserAgent = userAgent;
Uri = uri;
}
}
}
}
17 changes: 0 additions & 17 deletions src/StructureMap.Testing/Pipeline/DependencyCollectionTester.cs
Expand Up @@ -62,23 +62,6 @@ public void add_and_retrieve_by_type_only()
.ShouldBe(service1);
}

[Fact]
public void use_most_specific_criteria_if_possible()
{
var collection = new DependencyCollection();
collection.Add("foo", typeof(IGateway), gateway1);
collection.Add("bar", typeof(IGateway), gateway2);
collection.Add("bar", typeof(IService), service1);

collection.FindByTypeOrName(typeof(IGateway), "foo")
.ShouldBeTheSameAs(gateway1);

collection.FindByTypeOrName(typeof(IGateway), "bar")
.ShouldBeTheSameAs(gateway2);

collection.FindByTypeOrName(typeof(IService), "bar")
.ShouldBeTheSameAs(service1);
}

[Fact]
public void can_override_and_last_one_in_wins()
Expand Down
10 changes: 10 additions & 0 deletions src/StructureMap/Pipeline/DependencyCollection.cs
Expand Up @@ -208,6 +208,16 @@ public void Add(string name, Type type, object @dependency)
}
}

// Making sure you can override the default
if (name.IsNotEmpty())
{
_dependencies.RemoveAll(x => x.Name == name);
}
else if (type != null)
{
_dependencies.RemoveAll(x => x.Type == type && x.Name.IsEmpty());
}

_dependencies.Add(new Argument
{
Name = name,
Expand Down

0 comments on commit f7d08ce

Please sign in to comment.