# HG changeset patch # User srobbins <> # Date 1270068347 -3600 # Node ID 143bc550d4d5617048e11c9efe70f650cea09921 # Parent 5fa9032de6ee5fdf70c3596a8a22bf7eec4d3fa7 more tryresolve overloads diff -r 5fa9032de6ee5fdf70c3596a8a22bf7eec4d3fa7 -r 143bc550d4d5617048e11c9efe70f650cea09921 TinyIoC.Tests/TinyIoCTests.cs --- a/TinyIoC.Tests/TinyIoCTests.cs Wed Mar 31 21:25:31 2010 +0100 +++ b/TinyIoC.Tests/TinyIoCTests.cs Wed Mar 31 21:45:47 2010 +0100 @@ -2037,5 +2037,76 @@ Assert.IsFalse(result); } + + [TestMethod] + public void TryResolve_ValidResolveWithOptions_ReturnsTrue() + { + var container = UtilityMethods.GetContainer(); + container.Register(); + + ITestInterface output; + var result = container.TryResolve(new TinyIoC.ResolveOptions(), out output); + + Assert.IsTrue(result); + } + + [TestMethod] + public void TryResolve_ValidResolveWithOptions_ReturnsType() + { + var container = UtilityMethods.GetContainer(); + container.Register(); + + ITestInterface output; + var result = container.TryResolve(new TinyIoC.ResolveOptions(), out output); + + Assert.IsInstanceOfType(output, typeof(ITestInterface)); + } + + [TestMethod] + public void TryResolve_InvalidResolveWithOptions_ReturnsFalse() + { + var container = UtilityMethods.GetContainer(); + + ITestInterface output; + var result = container.TryResolve(new TinyIoC.ResolveOptions(), out output); + + Assert.IsFalse(result); + } + + + [TestMethod] + public void TryResolve_ValidResolveWithName_ReturnsTrue() + { + var container = UtilityMethods.GetContainer(); + container.Register("Testing"); + + ITestInterface output; + var result = container.TryResolve("Testing", out output); + + Assert.IsTrue(result); + } + + [TestMethod] + public void TryResolve_ValidResolveWithName_ReturnsType() + { + var container = UtilityMethods.GetContainer(); + container.Register("Testing"); + + ITestInterface output; + container.TryResolve("Testing", out output); + + Assert.IsInstanceOfType(output, typeof(ITestInterface)); + } + + [TestMethod] + public void TryResolve_InvalidResolveWithName_ReturnsFalse() + { + var container = UtilityMethods.GetContainer(); + + ITestInterface output; + var result = container.TryResolve("Testing", out output); + + Assert.IsFalse(result); + } } } diff -r 5fa9032de6ee5fdf70c3596a8a22bf7eec4d3fa7 -r 143bc550d4d5617048e11c9efe70f650cea09921 TinyIoC/TinyIoC.cs --- a/TinyIoC/TinyIoC.cs Wed Mar 31 21:25:31 2010 +0100 +++ b/TinyIoC/TinyIoC.cs Wed Mar 31 21:45:47 2010 +0100 @@ -866,6 +866,12 @@ return CanResolveInternal(new TypeRegistration(typeof(ResolveType), name), parameters, options); } + /// + /// Attemps to resolve a type using the default options + /// + /// Type to resolve + /// Resolved type or default if resolve fails + /// True if resolved sucessfully, false otherwise public bool TryResolve(out ResolveType resolvedType) where ResolveType : class { @@ -882,6 +888,50 @@ } /// + /// Attemps to resolve a type using the default options and given name + /// + /// Type to resolve + /// Resolution options + /// Resolved type or default if resolve fails + /// True if resolved sucessfully, false otherwise + public bool TryResolve(ResolveOptions options, out ResolveType resolvedType) + where ResolveType : class + { + try + { + resolvedType = this.Resolve(options); + return true; + } + catch (TinyIoCResolutionException) + { + resolvedType = default(ResolveType); + return false; + } + } + + /// + /// Attemps to resolve a type using the default options and given name + /// + /// Type to resolve + /// Name of registration + /// Resolved type or default if resolve fails + /// True if resolved sucessfully, false otherwise + public bool TryResolve(string name, out ResolveType resolvedType) + where ResolveType : class + { + try + { + resolvedType = this.Resolve(name); + return true; + } + catch (TinyIoCResolutionException) + { + resolvedType = default(ResolveType); + return false; + } + } + + /// /// Attempts to resolve all public property dependencies on the given object. /// /// Object to "build up"