Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:41:19+00:00 2026-06-07T05:41:19+00:00

I am learning Ninject as my first DI container, I guess all containers will

  • 0

I am learning Ninject as my first DI container, I guess all containers will have similar idea, so here is my question.

I have a class UserRepository that has a function:

//constructor
public UserRepository(Kernel kernel) { _kernel = kernel; } 

public UserDataModel CreateNewUser(string title, string firstname,
    string lastname, int age ...) 
{
    //I have the kernel registered everything properly
    //Getting a new user object from the kernel
    var user = _kernel.Get<UserDataModel>(); 

    user.Title = title;
    user.Firstname = firstname;
    user.Lastname = lastname;
    ...
    return user;
} 

Is it the correct way to use the kernel? I read from other posts that exposing and passing around the kernel is a bad bad idea. If so, what is the correct way to do it?

EDIT

some of my code demostrating the use of a tempUser object

public int UpdateSaUser(Guid msuserid, UserProfile saprofile,
    string accesstoken = "") 
{
    using (var db = new SaModelContainer()) 
    {
        var row1 = MakeNewRow(msuserid, saprofile, accesstoken);
        var row2 = db.SaUser.SingleOrDefault(
            r => r.MsUserId.Equals(msuserid));
        if (row2 == null) { //add new
            db.sauth_User.Add(row1);
        } else { //modify
            var entry = db.Entry(row2);
            entry.OriginalValues.SetValues(row2); 
            entry.CurrentValues.SetValues(row1); 
        }
        return db.SaveChanges();
    }
}

private SaUser MakeNewRow(Guid msuserid, UserProfile saprofile,
    string accesstoken = "") 
{
        var row = new SaUser {
            MsUserId = msuserid,
            Provider = (int)saprofile.Provider,
            ProfileId = saprofile.ID,
            Email = saprofile.Email,
            AccessToken = accesstoken,
            FirstName = saprofile.FirstName,
            LastName = saprofile.LastName,
            FullName = saprofile.FullName,
            UserName = saprofile.Username,
            DisplayName = saprofile.DisplayName,
            Country = saprofile.Country,
            DoB = StringParser.ParseDate(saprofile.DateOfBirth),
            Gender = (int)saprofile.GenderType,
            Language = saprofile.Language,
            ProfileURL = saprofile.ProfileURL,
            ProfilePictureURL = saprofile.ProfilePictureURL
        };

        return row;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:41:20+00:00Added an answer on June 7, 2026 at 5:41 am

    You should inject your dependencies also know as dependency Injection. Instead of injecting the kernel.

    Example (this is know as constructor injection):

    
    
    private UserDataModel _userDataModel;
    
    [Inject]
    public UserRepository(UserDataModel userDataModel)
    {
        _userDataModel = userDataModel;
    }
    
    //Any other method can now use _userDataModel
    
    

    You should probably make an interface of your UserDataModel so you can mock or stub it in your unit tests. And bind that interface to the correct implementation using ninject see the example. Then there is no need for the attributes.

    Ninject MVC Documentation

    Good mvc example

    A list of UserDataModels is not a dependency. I.E. You have some kind of database or other dependecy (WCF Service, Web Service, Entity Framework, nHibernate, …) where the users are coming from that is your dependency.

    
    
    public UserRepository(EntityFrameworkContext context)
    {
        //The entity framework context is the dependency that you don't want to test.
        _context = context;
    }
    
    public List GetAllUsers()
    {
       return _context.Users.ToList();
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning from my broad question here ... I was wondering how I could go
Learning .NET: I have a form with a split container, I want to load
I'm new to IOC containers and learning Ninject. I've using version 2.0, freshly downloaded
Learning Javascript and have a question about global variables. From my reading, most recommend
I am learning to use dependency injection with ninject. Most of the properties and
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
Learning about notifyAll made me question something about notify: in a typical situation we
Just learning the world of jquery, and all my googling gives examples like this:
Ninject, Sprint.NET, Unity, Autofac, Castle.Windsor are all examples are IoC frameworks that are available.
Learning jquery, so please be kind :) Using PHP, I have a table with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.