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

  • Home
  • SEARCH
  • 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 6813369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:33:43+00:00 2026-05-26T20:33:43+00:00

I’m specifically using C# with Ninject but the problem stretches beyond just Ninject. My

  • 0

I’m specifically using C# with Ninject but the problem stretches beyond just Ninject. My problem is that I have a few classes all with different constructor arguments plus the injected ones. I know I can use kernel.Get<MyObject>(constructor args here) to instantiate the objects. This doesn’t feel right to me as I’d have the kernel all over the place. I’ll do my best to list examples below.

What I have now:

public interface IStore<T>
{
    void CommitToDatabase(T item);
}

public abstract class Thing
{
    private IStore<Thing> _store;

    protected Thing(object key, IStore<Thing> store)
    {
        Key = key;
        _store = store;
    }

    public object Key { get; private set; }

    public virtual void Update()
    {
        _store.CommitToDatabase(this);
    }
}

public class Person :Thing
{
    public Person(object key, string name, int age, IStore<Thing> store)
        : base(key, store)
    {
        Name = name;
        Age = age;
    }

    public string Name { get; private set; }
    public int Age { get; private set; }
}

public class Car :Thing
{
    public Car(object key, int year, string make, string model, IStore<Thing> store)
        : base(key, store)
    {
        Year = year;
        Make = make;
        Model = model;
    }

    public int Year { get; private set; }    
    public string Make { get; private set; }
    public string Model { get; private set; }
}

I know in Ninject I can do the following:

kernel.Get<Car>(new ConstructorArgument("key", 1), new ConstructorArgument("year", 2010), new ConstructorArgument("make", "Astin Martin"), new ConstructorArgument("model", "Vanquish"));

but that doesn’t feel right to me. What I was thinking of doing was to change it to have an Initialize method, but I’m not sure if this is best-practice or if there’s a better way.

Possible new stuff:

public interface IStore<T>
{
    void CommitToDatabase(T item);
}

public abstract class Thing
{
    private IStore<Thing> _store;

    protected bool _isInitialised;

    protected Thing(IStore<Thing> store)
    {
        Key = null;
        _store = store;
        _isInitialised = false;
    }

    public object Key { get; private set; }

    public virtual void Initialize(object key)
    {
        if (!_isInitialised) {
            Key = key;
            _isInitialised = true;
        }
    }

    public virtual void Update()
    {
        _store.CommitToDatabase(this);
    }

    protected bool IsInitialised()
    {
        return _isInitialised;
    }
}

public class Person :Thing
{
    public Person(IStore<Thing> store)
        : base(store)
    {
        Name = string.Empty;
        Age = int.MinValue;
    }

    public string Name { get; private set; }
    public int Age { get; private set; }

    public void Initialize(object key, string name, int age)
    {
        if (!base.IsInitialised()) {
            Name = name;
            Age = age;
        }

        base.Initialize(key);
    }
}

public class Car :Thing
{
    public Car(IStore<Thing> store)
        : base(store)
    {
        Year = 0;
        Make = "Ford";
        Model = "Model T";
    }

    public int Year { get; private set; }
    public string Make { get; private set; }
    public string Model { get; private set; }

    public void Initialize(object key, int year, string make, string model)
    {
        if (!base.IsInitialised()) {
            Year = year;
            Make = make;
            Model = model;
        }

        base.Initialize(key);
    }
}

Question:
Is the ‘Possible new stuff’ a common practice, bad idea, good idea with poor implementation, or is there a better way to do it altogether?

  • 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-05-26T20:33:44+00:00Added an answer on May 26, 2026 at 8:33 pm

    You shouldn’t inject IStore into your DTO’s. They should be plain objects. Instead inject IStore<IThing> into the classes that currently call Update and call CommitToDatabase from there.

    e.g.

    public class PersonService
    {
        private readonly IStore<Person> store;
        public PersonService(IStore<Person> store)
        {
           this.store = store;
        }
    
        public void CreatePerson(string name, int age)
        {
           var person = new Person(name, age);
           this.store.CommitToDatabase(person);
        }
    }
    

    Also DTOs like Person shouldn’t be created using an IoC container. Get them from your persistence layer, create them using AutoMapper or create them using new. But do not use an IoC container for them. They shouldn’t have any dependencies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't

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.