0

I am using Ninject with Windows Phone application.

My logic includes IBarViewModel interface which is:

public interface IBarViewModel
    {
        double Width { get; set; }
        bool IsAchieved { get; set; }
        CornerRadius Corner { get; set; }
    }

Currently, there is only one implementatation of IBarViewModel - GenericBarViewModel.

All the IViewModel implementators will have to have these three main properties set at creation time.

I want to create an instances of these IBarViewModels at runtime from my code. How can I do it.

When I was programming against an implementation (the class was called simply Bar), I was just calling an object initializer like this:

_bars.Add(new Bar
                {
                    Width = _totalWidth,
                    IsAchieved = false,
                    Corner = new CornerRadius(5, 5, 5, 5)
                });

As per the answer to this question: Inject value into injected dependency I can pass a parameter to ninject module.

The only question remains: will I have to carry my ninject kernel deep into the logic to do such parametrized dependency resolution?

Community
  • 1
  • 1
Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
  • See [here](http://stackoverflow.com/questions/2227548/creating-an-instance-using-ninject-with-additional-parameters-in-the-constructor). Just make your class accept those parameters in the constructor and instruct ninject which values to supply. – Klaus Byskov Pedersen Dec 03 '11 at 01:57

1 Answers1

1

I don't think you should be using IoC at all for this. It doesn't make any sense whatsoever.

Why not simply let the GenericBarViewModel set the default values for the 3 properties, make it abstract, and let any other "Bar" ViewModel implementations inherit from the GenericBarViewModel ?

What exactly are you trying to do?

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150