Search code examples
c#wpfmvvmunity-container

Unity CS0308 Error


I have just started building apps using WPF and Unity from a great source implementing the MVVM architecture. I have followed it nearly identical and reviewed the source code they have used and everything is nearly identical. I have created a class ContainerHelper class:

private static IUnityContainer _container;
        static ContainerHelper()
        {
            _container = new UnityContainer();
            _container.RegisterType<IEmployeesRepository, EmployeesRepository>(
                new ContainerControlledLifetimeManager());
        }

        public static IUnityContainer Container
        {
            get { return _container; }
        }

And I have create a class EmployeeListViewModel:

private IEmployeesRepository _repo;

public EmployeeListViewModel(IEmployeesRepository repo)
{
    _repo = repo;
    EditEmployeeCommand = new RelayCommand<Employees>(OnEditEmployee);
    AddEmployeeCommand = new RelayCommand(OnAddEmployee);
    ClearSearchCommand = new RelayCommand(OnClearSearch);
}

private string _SearchInput;

public string SearchInput
{
    get { return _SearchInput; }
    set
    {
        SetProperty(ref _SearchInput, value);
        FilterEmployee(_SearchInput);
    }
}

private void FilterEmployee(string searchInput)
{
    if (string.IsNullOrWhiteSpace(searchInput))
    {
        Employees = new ObservableCollection<Employees>(_allEmployees);
    }
    else
    {
        Employees = new ObservableCollection<Employees>(_allEmployees.Where(e => e.FullName.ToLower().Contains(searchInput.ToLower())));
    }
}

private void OnClearSearch()
{
    SearchInput = null;
}

private void OnEditEmployee(Employees emp)
{
    EditEmployeeRequest(emp);
}

private void OnAddEmployee()
{
    AddEmployeeRequested(new Employees { Id = Guid.NewGuid() });
}

public event Action<Employees> AddEmployeeRequested = delegate { };
public event Action<Employees> EditEmployeeRequest = delegate { };

private ObservableCollection<Employees> _Employees;
public ObservableCollection<Employees> Employees
{
    get { return _Employees; }
    set { SetProperty(ref _Employees, value); }
}

private List<Employees> _allEmployees;
public async void LoadEmplooyees()
{
    _allEmployees = await _repo.GetEmployeesAsync();
    Employees = new ObservableCollection<Employees>(_allEmployees);
}

public RelayCommand<Employees> EditEmployeeCommand { get; private set; }
public RelayCommand AddEmployeeCommand { get; private set; }
public RelayCommand ClearSearchCommand { get; private set; }

}

And this is the line of code implementing and passing the EmployeeListViewModel using the ContainerHelper class:

private EmployeeListViewModel _EmployeeListViewModel;
 _EmployeeListViewModel = ContainerHelper.Container.Resolve<EmployeeListViewModel>();

I don't understand why I am getting a non-generic method error using IUnityContainer.Resolve(type, sring, params ResolverOverride[]) cannot be used with type arguments.

I would like to understand more about this error and what i can do to fix it...I have looked in various places to find an answer.


Solution

  • Add the following using directive at the top of the code file where you are calling the generic Resolve method:

    using Microsoft.Practices.Unity;