# HG changeset patch # User srobbins <> # Date 1270069692 -3600 # Node ID e7e0d1b655098e631ea8d808b297e8361136a118 # Parent 143bc550d4d5617048e11c9efe70f650cea09921 more tryresolve overloads diff -r 143bc550d4d5617048e11c9efe70f650cea09921 -r e7e0d1b655098e631ea8d808b297e8361136a118 TinyIoC.Tests/TinyIoCTests.cs --- a/TinyIoC.Tests/TinyIoCTests.cs Wed Mar 31 21:45:47 2010 +0100 +++ b/TinyIoC.Tests/TinyIoCTests.cs Wed Mar 31 22:08:12 2010 +0100 @@ -2073,7 +2073,6 @@ Assert.IsFalse(result); } - [TestMethod] public void TryResolve_ValidResolveWithName_ReturnsTrue() { @@ -2108,5 +2107,76 @@ Assert.IsFalse(result); } + + [TestMethod] + public void TryResolve_ValidResolveWithNameAndOptions_ReturnsTrue() + { + var container = UtilityMethods.GetContainer(); + container.Register("Testing"); + + ITestInterface output; + var result = container.TryResolve("Testing", new TinyIoC.ResolveOptions(), out output); + + Assert.IsTrue(result); + } + + [TestMethod] + public void TryResolve_ValidResolveWithNameAndOptions_ReturnsType() + { + var container = UtilityMethods.GetContainer(); + container.Register("Testing"); + + ITestInterface output; + container.TryResolve("Testing", new TinyIoC.ResolveOptions(), out output); + + Assert.IsInstanceOfType(output, typeof(ITestInterface)); + } + + [TestMethod] + public void TryResolve_InvalidResolveWithNameAndOptions_ReturnsFalse() + { + var container = UtilityMethods.GetContainer(); + + ITestInterface output; + var result = container.TryResolve("Testing", new TinyIoC.ResolveOptions(), out output); + + Assert.IsFalse(result); + } + + [TestMethod] + public void TryResolve_ValidResolveWithParameters_ReturnsTrue() + { + var container = UtilityMethods.GetContainer(); + container.Register(); + + TestClassWithParameters output; + var result = container.TryResolve(new NamedParameterOverloads() {{"stringProperty", "test"}, {"intProperty", 2}}, out output); + + Assert.IsTrue(result); + } + + [TestMethod] + public void TryResolve_ValidResolveWithParameters_ReturnsType() + { + var container = UtilityMethods.GetContainer(); + container.Register(); + + TestClassWithParameters output; + var result = container.TryResolve(new NamedParameterOverloads() { { "stringProperty", "test" }, { "intProperty", 2 } }, out output); + + Assert.IsInstanceOfType(output, typeof(TestClassWithParameters)); + } + + [TestMethod] + public void TryResolve_InvalidResolveWithParameters_ReturnsFalse() + { + var container = UtilityMethods.GetContainer(); + + TestClassWithParameters output; + var result = container.TryResolve(new NamedParameterOverloads() { { "intProperty", 2 } }, out output); + + Assert.IsFalse(result); + } + } } diff -r 143bc550d4d5617048e11c9efe70f650cea09921 -r e7e0d1b655098e631ea8d808b297e8361136a118 TinyIoC/TinyIoC.cs --- a/TinyIoC/TinyIoC.cs Wed Mar 31 21:45:47 2010 +0100 +++ b/TinyIoC/TinyIoC.cs Wed Mar 31 22:08:12 2010 +0100 @@ -888,7 +888,7 @@ } /// - /// Attemps to resolve a type using the default options and given name + /// Attemps to resolve a type using the given options /// /// Type to resolve /// Resolution options @@ -932,6 +932,50 @@ } /// + /// Attemps to resolve a type using the given options and name + /// + /// Type to resolve + /// Name of registration + /// Resolution options + /// Resolved type or default if resolve fails + /// True if resolved sucessfully, false otherwise + public bool TryResolve(string name, ResolveOptions options, out ResolveType resolvedType) + where ResolveType : class + { + try + { + resolvedType = this.Resolve(name, options); + return true; + } + catch (TinyIoCResolutionException) + { + resolvedType = default(ResolveType); + return false; + } + } + + /// + /// Attemps to resolve a type using the default options and supplied constructor parameters + /// + /// Type to resolve + /// User specified constructor parameters + /// Resolved type or default if resolve fails + /// True if resolved sucessfully, false otherwise + public bool TryResolve(NamedParameterOverloads parameters, out ResolveType resolvedType) + where ResolveType : class + { + try + { + resolvedType = this.Resolve(parameters); + return true; + } + catch (TinyIoCResolutionException) + { + resolvedType = default(ResolveType); + return false; + } + } + /// /// Attempts to resolve all public property dependencies on the given object. /// /// Object to "build up"