diff --git a/src/Core/Modules/InlineModule.cs b/src/Core/Modules/InlineModule.cs index 3443d18..3e4a63b 100644 --- a/src/Core/Modules/InlineModule.cs +++ b/src/Core/Modules/InlineModule.cs @@ -1,60 +1,71 @@ -#region License -// -// Author: Nate Kohari -// Copyright (c) 2007-2008, Enkari, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion -#region Using Directives -using System; -using System.Collections.Generic; -using Ninject.Core.Infrastructure; -#endregion - -namespace Ninject.Core -{ - /// - /// A module that uses a callback for its Load() implementation. Useful for creating simple - /// modules, especially for testing. - /// - public class InlineModule : StandardModule - { - /*----------------------------------------------------------------------------------------*/ - #region Fields - private readonly List> _loadCallbacks; - #endregion - /*----------------------------------------------------------------------------------------*/ - #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// One or more methods to call when the module is loaded. - public InlineModule(params Action[] loadCallbacks) - { - _loadCallbacks = new List>(loadCallbacks); - } - #endregion - /*----------------------------------------------------------------------------------------*/ - #region Public Methods - /// - /// Loads the module into the kernel. - /// - public override void Load() - { - _loadCallbacks.Each(callback => callback(this)); - } - #endregion - /*----------------------------------------------------------------------------------------*/ - } +#region License +// +// Author: Nate Kohari +// Copyright (c) 2007-2008, Enkari, Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion +#region Using Directives +using System; +using System.Collections.Generic; +using Ninject.Core.Infrastructure; +#endregion + +namespace Ninject.Core +{ + /// + /// A module that uses a callback for its Load() implementation. Useful for creating simple + /// modules, especially for testing. + /// + public class InlineModule : StandardModule + { + /*----------------------------------------------------------------------------------------*/ + #region Fields + private readonly List> _loadCallbacks; + #endregion + /*----------------------------------------------------------------------------------------*/ + #region Constructors + /// + /// Initializes a new instance of the class. + /// + /// One or more methods to call when the module is loaded. + public InlineModule(params Action[] loadCallbacks) + { + _loadCallbacks = new List>(loadCallbacks); + } + #endregion + /*----------------------------------------------------------------------------------------*/ + #region Public Methods + /// + /// Loads the module into the kernel. + /// + public override void Load() + { + _loadCallbacks.Each(callback => callback(this)); + } + #endregion + /*----------------------------------------------------------------------------------------*/ + /// + /// Gets the name of the module. Unique per instance. + /// + public override string Name + { + get + { + return this.GetType().Name + this._loadCallbacks.GetHashCode(); + } + } + /*----------------------------------------------------------------------------------------*/ + } } \ No newline at end of file diff --git a/src/Tests/Core/Binding/BindingFixture.cs b/src/Tests/Core/Binding/BindingFixture.cs index eb56a0f..4d856f4 100644 --- a/src/Tests/Core/Binding/BindingFixture.cs +++ b/src/Tests/Core/Binding/BindingFixture.cs @@ -138,5 +138,22 @@ public void IncompleteBindingCausesKernelToThrowException() IKernel kernel = new StandardKernel(module); } /*----------------------------------------------------------------------------------------*/ + [Test] + public void CanBindInlineModuleTwoSeperateTimes() + { + var moduleA = new InlineModule(m => m.Bind().To()); + var moduleB = new InlineModule(m => m.Bind().ToConstant("test")); + + using (var kernel = new StandardKernel(moduleA, moduleB)) + { + var mock1 = kernel.Get(); + var mock2 = kernel.Get(); + + Assert.That(mock1, Is.Not.Null); + Assert.That(mock2, Is.EqualTo("test")); + } + + } + /*----------------------------------------------------------------------------------------*/ } } \ No newline at end of file