Coder Social home page Coder Social logo

ninject's Introduction

Ninject NuGet Version NuGet Downloads

Ninject is a lightning-fast, ultra-lightweight dependency injector for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software's architecture, your code will become easier to write, reuse, test, and modify.

Write your code so it's flexible...

public class Samurai {
    public IWeapon Weapon { get; private set; }
    public Samurai(IWeapon weapon) 
    {
        this.Weapon = weapon;
    }
}

...and let Ninject glue it together for you.

public class WarriorModule : NinjectModule
{
    public override void Load() 
    {
        this.Bind<IWeapon>().To<Sword>();
    }
}

Features:

  1. Focused. Too many existing dependency injection projects sacrifice usability for features that aren't often necessary. Each time a feature is added to Ninject, its benefit is weighed against the complexity it adds to everyday use. Our goal is to keep the barrier to entry - the baseline level of knowledge required to use Ninject - as low as possible. Ninject has many advanced features, but understanding them is not required to use the basic features.

  2. Sleek. Framework bloat is a major concern for some projects, and as such, all of Ninject's core functionality is in a single assembly with no dependencies outside the .NET base class library. This single assembly's footprint is approximately 85KB when compiled for release.

  3. Fast. Instead of relying on reflection for invocation, Ninject takes advantage of lightweight code generation in the CLR. This can result in a dramatic (8-50x) improvement in performance in many situations.

  4. Precise. Ninject helps developers get things right the first time around. Rather than relying on XML mapping files and string identifiers to wire up components, Ninject provides a robust domain-specific language. This means that Ninject takes advantage of the capabilities of the language (like type-safety) and the IDE (like IntelliSense and code completion).

  5. Agile. Ninject is designed around a component-based architecture, with customization and evolution in mind. Many facets of the system can be augmented or modified to fit the requirements of each project.

  6. Stealthy. Ninject will not invade your code. You can easily isolate the dependency on Ninject to a single assembly in your project.

  7. Powerful. Ninject includes many advanced features. For example, Ninject is the first dependency injector to support contextual binding, in which a different concrete implementation of a service may be injected depending on the context in which it is requested.

Everything else is in Extensions

Yes, sounds slim and focused, but where is the support for all the features that the competitors have?

Generally, they are maintained as specific focused extensions with owners who keep them in sync and pull in new ideas and fixes fast. These are summarized on the extensions section of the project website. Most are hosted alongside the core project right here.

License

Ninject is intended to be used in both open-source and commercial environments. To allow its use in as many situations as possible, Ninject is dual-licensed. You may choose to use Ninject under either the Apache License, Version 2.0, or the Microsoft Public License (Ms-PL). These licenses are essentially identical, but you are encouraged to evaluate both to determine which best fits your intended use.

Refer to LICENSE.txt for detailed information.

CI build status

Build Status

Changes history

Resources

ninject's People

Contributors

remogloor avatar casualjim avatar danielmarbach avatar scott-xu avatar iappert avatar idavis avatar jarrettmeyer avatar bartelink avatar apdmatos avatar idisposable avatar brunojuchli avatar sglienke avatar ungood avatar jamesmanning avatar aaubry avatar litee avatar dholtdev avatar giorgi avatar petejohanson avatar rolandka avatar scottcarr avatar kaylanimis avatar philippdolder avatar

Stargazers

Media Explorer avatar Marco Aurelyo Araujo Borges avatar  avatar  avatar Colin Gourlay avatar  avatar Ben Buttigieg avatar Alexei Vinidiktov avatar Luca Fattorini avatar Tamás Deme avatar Camilo E. Hidalgo Estevez avatar Alexander avatar Tuan Luong avatar Tommaso Scalici avatar Vinicius Jarina avatar David Luu avatar Has AlTaiar avatar Andy Bradford avatar José Augusto Guimarães avatar Igor Kulman avatar Shane Church avatar  avatar Shady M. Najib avatar Claire Novotny avatar Brad Pillow avatar  avatar

Watchers

James Cloos avatar Claire Novotny avatar  avatar  avatar TAE HWAN KIM avatar Media Explorer avatar  avatar

ninject's Issues

ArgumentNullException in release build

I'm using Portable.Ninject v3.3.1 installed via Nuget for Windows UAP project
The ArgumentNullException is thrown while trying to inject object of class with async methods.
StackTrace:

Ninject.Platform.dll!Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetPropertyFromDeclaredType.AnonymousMethod__0(System.Reflection.PropertyInfo p)   Unknown
    System.Linq.dll!System.Linq.Enumerable.FirstOrDefault<Windows.Foundation.AsyncOperationCompletedHandler_A_System_Collections_Generic_IDictionary_A_string_j_System_Object_V__V___Impl.Vtbl>(System.Collections.Generic.IEnumerable<Windows.Foundation.AsyncOperationCompletedHandler_A_System_Collections_Generic_IDictionary_A_string_j_System_Object_V__V___Impl.Vtbl> source, System.Func<Windows.Foundation.AsyncOperationCompletedHandler_A_System_Collections_Generic_IDictionary_A_string_j_System_Object_V__V___Impl.Vtbl,bool> predicate) Line 317 C#

It seems that it fails on some runtime AsyncOperationCompletedHandler property.
Note that it happens in release mode only. Debug build runs correctly.
P.S. I'm using new StandardKernel(new NinjectSettings {LoadExtensions = false}); because of AssemblyNotFound error in release build.

"An item with the same key has already been added" in ActivationCache

Running into this exception when trying to get something from the container. I've tried clearing the activation cache, but this doesn't seem to help. Also tried to get the source for the latest published nuget package (3.3.1) on github so that I can make the change to check for existence, but don't see a tagged 3.3.1. Last tagged is 3.2.3.

Non-fatal Exception: android.runtime.JavaProxyThrowable: System.ArgumentException: An item with the same key has already been added. Key: Ninject.Infrastructure.ReferenceEqualWeakReference
at System.Collections.Generic.Dictionary2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x000c1] in <2c2663dbba3a4ea68e74453e2f54e4a4>:0 at System.Collections.Generic.Dictionary2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <2c2663dbba3a4ea68e74453e2f54e4a4>:0
at Ninject.Activation.Caching.ActivationCache.AddActivatedInstance (System.Object instance) [0x0001d] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.Activation.Strategies.ActivationCacheStrategy.Activate (Ninject.Activation.IContext context, Ninject.Activation.InstanceReference reference) [0x0000c] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.Activation.Pipeline+<>c__DisplayClass2.b__0 (Ninject.Activation.Strategies.IActivationStrategy s) [0x00000] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T] (System.Collections.Generic.IEnumerable1[T] series, System.Action1[T] action) [0x00010] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.Activation.Pipeline.Activate (Ninject.Activation.IContext context, Ninject.Activation.InstanceReference reference) [0x00044] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.Activation.Context.ResolveInternal (System.Object scope) [0x000d9] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.Activation.Context.Resolve () [0x0004e] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.KernelBase.Resolve (Ninject.Activation.IRequest request, System.Boolean handleMissingBindings) [0x00160] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.KernelBase.Resolve (Ninject.Activation.IRequest request) [0x00000] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.ResolutionExtensions.GetResolutionIterator (Ninject.Syntax.IResolutionRoot root, System.Type service, System.Func2[T,TResult] constraint, System.Collections.Generic.IEnumerable1[T] parameters, System.Boolean isOptional, System.Boolean isUnique) [0x0002f] in <50364816da6e4b04a5a9b75a4002aaa2>:0
at Ninject.ResolutionExtensions.Get[T] (Ninject.Syntax.IResolutionRoot root, Ninject.Parameters.IParameter[] parameters) [0x00000] in <50364816da6e4b04a5a9b75a4002aaa2>:0

dot net core version

Hello! Is it possible to make nuget packages with dot net core support? Or PCL version have some big troubles with dot net core? Thank you!

Compatibility .NET 5 Core, Xamarin.Android Version 10.2+

Symptom: When building a Xamarin Android project using the most recent release of Portable.Ninject version 3.3.1 the following warning occurs:

LINKASSEMBLIESNOSHRINK : warning XA2000: Use of AppDomain.CreateDomain() detected in assembly: Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7. .NET 5 will only support a single AppDomain, so this API will no longer be available in Xamarin.Android once .NET 5 is released.

Issue: .NET 5 will follow the .NET Core semantics and there will be only one AppDomain and AppDomain.CreateDomain() throws a PlatformNotSupportedException.

Resolution: Switch from AppDomain.CreateDomain() to a different API like AssemblyLoadContext.

See Xamarin.Android 10.2 release notes for more detail

NetStandard / MacOS Support

It would be fabulous if this project were a NetStandard library or at least was updated to be compatible with Xamarin Mac

Assembly scanning fails on iOS Unified

Seems to be a null coming from one of the base path's in the ModuleLoader NormalizePaths.

Workaround for now:

Use one of the StandardKernel ctor's which takes a NinjectSettings and set the LoadExtensions property to false. That should bypass the code that’s throwing.

Android linker strips out essential stuff

The android linker strips a bunch of stuff from Ninject in my release build.
The log tells me ModuleLoader can not be loaded becasue it is missing a constructor if I enable the linker.

Maybe you can add the [Android.Runtime.Preserve] to the components that get loaded with reflection for the Android version?

In case someone else has this: Adding Ninject in the Ignore assemblies field under the linker setting seems to remedy the situation.

NotImplementedException

Hi,
I'm trying to use Ninject Portable in a tvOS project, but I get a "NotImplementedException" in
BindingRoot.Bind() when I call the StandardKernel constructor.
The same code is working for Android and iOS, so I can't think of what the problem is.
Does it enter in the #if PCL case if it's a platform that Ninject doesn't recognise? Since I could successfully add the ninject.portable package in the project I supposed that tvOS was supported. Is this the case?
Thanks

Xamarin Android 6.0 & ios 9.2 Binding String Crash

Hi!
I'm using the ninject portable nuget package (3.3.1) in my xamarin application and after updating xamarin yesterday, there is a strange error while injecting strings.

My Module code:

Bind<string> ().ToConstant ("value").Named ("key");

My Class Injection:

[Inject]
[Named("key")]
public string key { private get; set; }

The error is:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.ArgumentNullException: Cannot be null
[MonoDroid] Parameter name: member
[MonoDroid]   at Ninject.Infrastructure.Ensure.ArgumentNotNull (System.Object argument, System.String name) [0x00003] in <filename unknown>:0 
[MonoDroid]   at Ninject.Selection.Heuristics.StandardInjectionHeuristic.ShouldInject (System.Reflection.MemberInfo member) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Ninject.Selection.Selector+<>c__DisplayClass3.<SelectPropertiesForInjection>b__2 (IInjectionHeuristic h) [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Linq.Enumerable.Any[TSource] (IEnumerable`1 source, System.Func`2 predicate) [0x00035] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:1177 
[MonoDroid]   at Ninject.Selection.Selector.<SelectPropertiesForInjection>b__1 (System.Reflection.PropertyInfo p) [0x00013] in <filename unknown>:0 
[MonoDroid]   at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x00048] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:149 
[MonoDroid]   at System.Collections.Generic.List`1[T].InsertRange (Int32 index, IEnumerable`1 collection) [0x000ff] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:788 
[MonoDroid]   at System.Collections.Generic.List`1[T].AddRange (IEnumerable`1 collection) [0x00000] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:276 
[MonoDroid]   at Ninject.Selection.Selector.SelectPropertiesForInjection (System.Type type) [0x00040] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Strategies.PropertyReflectionStrategy.Execute (IPlan plan) [0x00017] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Planner+<>c__DisplayClass1.<CreateNewPlan>b__0 (IPlanningStrategy s) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T] (IEnumerable`1 series, System.Action`1 action) [0x00010] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Planner.CreateNewPlan (System.Type type) [0x00055] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Planner.GetPlan (System.Type type) [0x00027] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Context.ResolveInternal (System.Object scope) [0x000c9] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Context.Resolve () [0x0003a] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request, Boolean handleMissingBindings) [0x00160] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Targets.Target`1[T].GetValue (System.Type service, IContext parent) [0x00032] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Targets.Target`1[T].ResolveWithin (IContext parent) [0x000d6] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Providers.StandardProvider.GetValue (IContext context, ITarget target) [0x0005e] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Providers.StandardProvider+<>c__DisplayClass5.<Create>b__2 (ITarget target) [0x00000] in <filename unknown>:0 
[MonoDroid]   at System.Linq.Enumerable+WhereSelectArrayIterator`2[TSource,TResult].MoveNext () [0x0004d] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:327 
[MonoDroid]   at System.Linq.Buffer`1[TElement]..ctor (IEnumerable`1 source) [0x00083] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:2570 
[MonoDroid]   at System.Linq.Enumerable.ToArray[TSource] (IEnumerable`1 source) [0x00011] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:830 
[MonoDroid]   at Ninject.Activation.Providers.StandardProvider.Create (IContext context) [0x00129] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Context.ResolveInternal (System.Object scope) [0x00035] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Context.Resolve () [0x0003a] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request, Boolean handleMissingBindings) [0x00160] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Targets.Target`1[T].GetValue (System.Type service, IContext parent) [0x00032] in <filename unknown>:0 
[MonoDroid]   at Ninject.Planning.Targets.Target`1[T].ResolveWithin (IContext parent) [0x000d6] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Strategies.PropertyInjectionStrategy.GetValue (IContext context, ITarget target, IEnumerable`1 allPropertyValues) [0x0003e] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Strategies.PropertyInjectionStrategy.Activate (IContext context, Ninject.Activation.InstanceReference reference) [0x00049] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Pipeline+<>c__DisplayClass2.<Activate>b__0 (IActivationStrategy s) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T] (IEnumerable`1 series, System.Action`1 action) [0x00010] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Pipeline.Activate (IContext context, Ninject.Activation.InstanceReference reference) [0x00044] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Context.ResolveInternal (System.Object scope) [0x000d9] in <filename unknown>:0 
[MonoDroid]   at Ninject.Activation.Context.Resolve () [0x0004e] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request, Boolean handleMissingBindings) [0x00160] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request, Boolean handleMissingBindings) [0x00063] in <filename unknown>:0 
[MonoDroid]   at Ninject.KernelBase.Resolve (IRequest request) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Ninject.ResolutionExtensions.GetResolutionIterator (IResolutionRoot root, System.Type service, System.Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) [0x0002f] in <filename unknown>:0 
[MonoDroid]   at Ninject.ResolutionExtensions.Get[T] (IResolutionRoot root, Ninject.Parameters.IParameter[] parameters) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Atlas.Droid.Framework.AVMApplication`1[T].BuildViewModel () [0x0000d] in /Users/emmanuelevilla/atlas/Atlas.Droid/Framework/AVMApplication.cs:24 

In the same module I have others string bindings and all of them crashes while injecting.

Things I've tried:

  • Downgrading Xamarin Version: it works
  • Injecting a "StringWrapper" custom object containing the desired string: it works
  • Using a StringProvider: it doesn't work
  • Remove the "Named" part of code: it doesn't work

I've tried to download this repository and in the android test project it works, but I suppose that the nuGet package is older because the repository project shows me some warnings of deprecated classes, like StandardKernel, when I try to use them.

Thanks and don't hesitate if you want me to do some other tests!

under iOS, System.NotSupporedException raised when using Kernel.Load();

Repro step :
Create a dot net standard library project, call it "empty-library"
Create a iOS application that references "empty-library"
Add following code in AppDelegate.FinishedLaunching()
var Kernel = new StandardKernel();
Kernel.Load("empty-library.dll");
Launch on emulator

Result:
When executing instruction Kernel.Load("empty-library.dll"), exception system.NotSupportedException in thrown

Expected:
When executing instruction Kernel.Load("empty-library.dll"), no exception is thrown.

Note about context: I use Ninject for years with desktop applications. I need reuse parts of my code in Xamarin.iOS and Xamarin.Android apps.

error working with iPhone running ios 12.1.4

runs on prior ios without a problem but after try this ios i get
Error loading Ninject component IModuleLoader No constructor was available to create an instance of the registered implementation type ModuleLoader. Suggestions: 1) Ensure that the implementation type has a public constructor.nject The method or operation is not implemented.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.