Ninject | the ninja of .net dependency injectors | Dependency Injection library
kandi X-RAY | Ninject Summary
kandi X-RAY | Ninject Summary
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.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Ninject
Ninject Key Features
Ninject Examples and Code Snippets
Community Discussions
Trending Discussions on Ninject
QUESTION
ANSWER
Answered 2021-Oct-14 at 19:09As per linked Ninject example: MediatR.Examples.Ninject, I guess I found an error in the ContravariantBindingResolver.cs class.
The line that I guess is at fault is when getting the generic arguments for the second time.
QUESTION
I have an exception Must create DependencySource on same Thread as the DependencyObject
when i want to use ParallelForEachAsync:
...ANSWER
Answered 2021-Sep-10 at 15:57The exception is cleared by freezing the image source:
QUESTION
I have a repository that asks for a DbContext
in its constructor, and then I used ninject to solve this dependency, and I set the object scope to be InRequestScope
as it means instantiating an object per HTTP request, but I'm not sure that when an HTTP request actually happens? is it when the app is being loaded? or it happens when we call SaveChanges()
?
My approach for managing the DbContext is like that, I have a repository asking for a context as I said, and then the controller asks for this repository in its constructor:
ANSWER
Answered 2021-Aug-02 at 21:06Each time a controller action is called from any web client, that is a request. So when someone visits your site and visits /Pagegroups/Index resolved through routing, that is a request. When you do a Form.Submit from the client, that is a request, make an Ajax call, that is a request.
Do you want the DbContext scoped to be constructed for each request? Absolutely, and no "longer" than a request. For simple applications, using using()
within actions is perfectly fine, but it does add a bit of boilerplate code repeating it everywhere. In more complex, long lived applications where you might want to unit test or that could have more complex logic that benefits from breaking down into smaller components shared around, using
blocks are a bit of a mess to share the DbContext, so an injected DbContext scoped to the request serves that purpose just fine. Every class instance serving a request is given the exact same DbContext instance.
You don't want a DbContext scoped longer than a request (I.e. Singleton) because while requests from one client may be sequential, requests from multiple users are not. Web servers will respond to various user requests at a time on different threads. EF's DbContext is not thread safe. This catches out new developers where everything seems to work on their machine when testing, only to find that once deployed to a server and handling concurrent requests, errors start popping up.
Also, as DbContext's age, they get bigger and slower tracking more instances of entities. This leads to gradual performance loss, as well as issues as a DbContext serves up cached instances that doesn't reflect data changes from possibly other sources. A new development team might get caught out with the cross-thread issue but introduce locking or such because they want to use EF's caching rather than using a shorter lifespan. (assuming DbContext are "expensive" to create all the time [they're not!:]) This often is the cause of teams calling to abandon EF because it's "slow" without realizing that design decisions prevented them from taking advantage of most of EF's capabilities.
As a general tip I would strongly recommend avoiding the Generic Repository pattern when working with EF. It will give you no benefit other than pigeon-holing your data logic. The power of EF is in the ability to handle the translation of operations against Objects and their relationships down to SQL. It is not merely a wrapper to get down to data. Methods like this:
QUESTION
I'm struggling to build an App using WPF and MVVM pattern. In this App I have three buttons in the MainView - Camera, Boiler, Temperature Sensor. When I press a certain button, It opens additional control with information about a selected gadget in the second part of the window. I've done it, but architecture is not good, because the Main Form "knows" about certain objects. I have this code in my MainView (XAML), which should not look like this.
...ANSWER
Answered 2021-Jul-29 at 14:17It seems like you want to get another result. If you are planning to add devices in future (generate buttons dynamically and use custom view) you need to define base interface and implement for each device (sensor, boiler, etc.) which should be made as different module (library). Module should provide some metadata, custom view and something else you want. When you start your app, container loads assemblies with implemented base interface, so you get devices and can generate buttons dynamically using metadata. Then, when necessary, you can use provided view or other provided properties or methods.
QUESTION
I am new on .Net Core and Microsoft Dependency Injection and what I am trying to do is something similar to '.ToFactory()' (from ninject) which I can create a class containing all my interfaces services, avoiding a lot of IMyClassService on my controllers. In .Net Framework + Ninject I used to do:
NinjectModule
...ANSWER
Answered 2021-May-25 at 12:57You can request IServiceProvider
and get the service yourself.
QUESTION
I am creating Unit Tests for existing .NET Framework 4.5 API project. The existing project has parameterless constructor by design and dependency injection was implemented as per the class below using Ninject.
I would like to Mock the interface and create an instance of the class for testing as shown below but the constructor is parameterless. I can't figure out how to inject my Mock member.Object. The main goal is I don't want to change the design of existing classes unless there is no other way.
...ANSWER
Answered 2021-May-22 at 15:21This post has the answer to the question. If IMember member property is made public in MemberController, I can new up the MemberController class and set the value of its member property to the MOCK.
QUESTION
It is possible to register NodaTime.SystemClock
like this: builder.Register(_ => SystemClock.Instance).As().SingleInstance();
, according to Pac0's comment here.
How do I do the same for NodaTime.ZonedClock
?
ANSWER
Answered 2021-May-13 at 15:31ZonedClock
doesn't seem to have any singleton accessor like SystemClock
does, so you have to call it's constructor. Using the same approach you used for SystemClock
, it would look something like this:
QUESTION
I am using Ninject for Dependency Injection. I have to call two identical classes in the constructor.
...ANSWER
Answered 2021-Apr-19 at 13:06The common solution to this issue is to employ the factory pattern.
You create a factory, that based on some paramter set, during runtime, decide which of the classes to use, and then you merely dependency inject that factory.
QUESTION
I am converting .net framework to .net 5.0 and using the built in dependency injection in .net 5.0 instead of Ninject library I used in .net framework.
I have the following constructor that takes in a messageHandler (through dependency injection) as well as a web service root address:
(old) 1.
...ANSWER
Answered 2021-Mar-29 at 16:10There's a few options, here's a couple of them.
Use the
s
parameter to get the service:
QUESTION
I have loaded a solution developed in a older visual studio version in my local visual studio 2017. There are many reference to external dlls. When I compile the solution I get error as below despite the fact that the dll's are available under the "packages" folder in the solution. The target framework of the projects in the solution are set to .NET Framework 4. I have .NET Framework 4.7.1 installed in my computer. I would appreciate some help on this.
Updated with project file
...ANSWER
Answered 2021-Mar-02 at 05:38The issue is that the hintpath
of your project did not point to the right dll path of your packages
folder under the solution folder.
If the csproj
exists under the proejct folder while the packages folder is on one above folder of the file, then the error happens.
You should change ..\..\
to ..\
. Use the right path.
Or try this command under Tools-->Nuget Package Manager-->Package Manager Console
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Ninject
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page