0

I've defined a class which takes a repository instance as the sole constructor argument. And now that I created a static instance of that class, MainViewModel the constructor arguments need to be specified.

Issue:

In order to specify the required arguments, I added the IRepository argument as expected in the static instance of the MainVM class.

This didn't work as expected as the compiler is throwing the following errors as a result of this:

Using the generic type 'ParkingTagPicker.DAL.IRepository<T>' requires 1 type arguments  

ParkingTagPicker.Models.Zone' is a 'type' but is used like a 'variable' 

The name 'zoneDataService' does not exist in the current context    

From looking at the errors it seems the IRepository is being supplied with the correct Zone type.

Does anyone know how to correctly define the constructor args in this situation?

MainViewModel:

    //IRepository instance with Zone type arg
    //(Zone is a model class)
    private IRepository<Zone> _zoneDataService;

    public MainViewModel(IRepository<Zone> zoneDataService)
    {
        this._zoneDataService = zoneDataService;
    }

App.xaml.cs:

    /// <summary>
    /// A static ViewModel used by the views to bind against.
    /// </summary>
    /// <returns>The MainViewModel object.</returns>
    public static MainViewModel ViewModel
    {
        get
        {
            // Delay creation of the view model until necessary
            if (viewModel == null)
                viewModel = new MainViewModel(IRepository<Zone> zoneDataService);

            return viewModel;
        }
    }
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • 1
    Don't you mean `viewModel = new MainViewModel(new IRepository());`?? (Otherwise, code is illegal) – Amit Jan 16 '16 at 19:54
  • I tried that, but I can't new IRepository it is an interface. Any other ideas? – Brian Var Jan 16 '16 at 19:58
  • What concrete type that implements `IRepository` do you expect to use? (`new` *that*). – Amit Jan 16 '16 at 19:59
  • The type that implements IRepository is the CouncilZoneRepository. So it implements like so..public class CouncilZoneRespository : IRepository. Then I just need to new CouncilZoneRepository? – Brian Var Jan 16 '16 at 20:11
  • `new CouncilZoneRepository`. No template type since that type is not templated (it inherits a templated base, but passes it's own type) – Amit Jan 16 '16 at 20:18
  • Ok my CouncilZoneRepository provides a static instance. So I tried the following, `viewModel = new MainViewModel(CouncilZoneRespository.Instance);` Should that work in this case? – Brian Var Jan 16 '16 at 20:29
  • 1
    I guess so but usage of static instances (and singleton pattern in general) is largely deprecated. You should consider using Factory Pattern and injection instead. – Amit Jan 16 '16 at 20:37

0 Answers0