Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added override to InlineModule.Name #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 70 additions & 59 deletions src/Core/Modules/InlineModule.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,71 @@
#region License
//
// Author: Nate Kohari <[email protected]>
// 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
{
/// <summary>
/// A module that uses a callback for its Load() implementation. Useful for creating simple
/// modules, especially for testing.
/// </summary>
public class InlineModule : StandardModule
{
/*----------------------------------------------------------------------------------------*/
#region Fields
private readonly List<Action<InlineModule>> _loadCallbacks;
#endregion
/*----------------------------------------------------------------------------------------*/
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="InlineModule"/> class.
/// </summary>
/// <param name="loadCallbacks">One or more methods to call when the module is loaded.</param>
public InlineModule(params Action<InlineModule>[] loadCallbacks)
{
_loadCallbacks = new List<Action<InlineModule>>(loadCallbacks);
}
#endregion
/*----------------------------------------------------------------------------------------*/
#region Public Methods
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
_loadCallbacks.Each(callback => callback(this));
}
#endregion
/*----------------------------------------------------------------------------------------*/
}
#region License
//
// Author: Nate Kohari <[email protected]>
// 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
{
/// <summary>
/// A module that uses a callback for its Load() implementation. Useful for creating simple
/// modules, especially for testing.
/// </summary>
public class InlineModule : StandardModule
{
/*----------------------------------------------------------------------------------------*/
#region Fields
private readonly List<Action<InlineModule>> _loadCallbacks;
#endregion
/*----------------------------------------------------------------------------------------*/
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="InlineModule"/> class.
/// </summary>
/// <param name="loadCallbacks">One or more methods to call when the module is loaded.</param>
public InlineModule(params Action<InlineModule>[] loadCallbacks)
{
_loadCallbacks = new List<Action<InlineModule>>(loadCallbacks);
}
#endregion
/*----------------------------------------------------------------------------------------*/
#region Public Methods
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
_loadCallbacks.Each(callback => callback(this));
}
#endregion
/*----------------------------------------------------------------------------------------*/
/// <summary>
/// Gets the name of the module. Unique per instance.
/// </summary>
public override string Name
{
get
{
return this.GetType().Name + this._loadCallbacks.GetHashCode();
}
}
/*----------------------------------------------------------------------------------------*/
}
}
17 changes: 17 additions & 0 deletions src/Tests/Core/Binding/BindingFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,22 @@ public void IncompleteBindingCausesKernelToThrowException()
IKernel kernel = new StandardKernel(module);
}
/*----------------------------------------------------------------------------------------*/
[Test]
public void CanBindInlineModuleTwoSeperateTimes()
{
var moduleA = new InlineModule(m => m.Bind<IMock>().To<ImplA>());
var moduleB = new InlineModule(m => m.Bind<string>().ToConstant("test"));

using (var kernel = new StandardKernel(moduleA, moduleB))
{
var mock1 = kernel.Get<IMock>();
var mock2 = kernel.Get<string>();

Assert.That(mock1, Is.Not.Null);
Assert.That(mock2, Is.EqualTo("test"));
}

}
/*----------------------------------------------------------------------------------------*/
}
}