0

I am new to MVVM so please excuse me. I need to create a demo project regarding Employee Salary Slip(Just for my practise). where i need to have a Employee model which implements the IEmployee interface and ISalary Interface. My question was where should the IEmployee and ISalary interface should be? because i have seen plenty of examples where all are using direct class itself rather using a Interface so i am confused to use Interfaces. where should i place my following interfaces either in Model where i have my Employee class or shall i have some interface folder and have all my interfaces there.

ICustomer;

public interface IEmployee
{
    string EmployeeName { get; set; }
    string Department { get; set; }
    string Contact { get; set; }
}

ISalaryDetails

public interface ISalaryDetails
{
    int Salary { get; set; }
    int Month { get; set; }
}

Employee

 public class Employee : IEmployee, ISalaryDetails
{

    public string EmployeeName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public string Department
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public string Contact
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public int Salary
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public int Month
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}
  • 1
    Those seem like very odd interfaces... Either way, I don't understand what you are asking. What are you confused about? If it is just where to put them in your project, thats totally up to you and this question should be closed as opinion based. – BradleyDotNET Jul 07 '14 at 17:08
  • 1
    Interfaces are about contracts. You inherit an interface in class if it has to implement the methods the interface defines, that's about it. In your case, you could have a class "Boss" that inherits from ISalaryDetails but not from IEmployee, for example (even though you define members more than methods, which is not the point of an interface). In the end, your question is clearly not about wpf nor mvvm, and somehow not about c# and .net either if you think about it. – Kilazur Jul 07 '14 at 17:11
  • May be i was no clear enough. As i said i was totally new to MVVM, and i would have messed with normal project with MVVM sorry about that. Hope you guys are experts in this. so let me know where to put Interfaces in MVVM and in my project as well. as the below answer says looks like i would have to consider the unit testing projects as well. ok any way my question WAS where should i have my interfaces in my project. either in Model folder or in view model folder hope am now clear. – NavinKumar K Subramanian Jul 07 '14 at 17:58

2 Answers2

1

In a large scale WPF Application, it is customary to find interface classes in several different projects. You'll definitely find some in a data model project of the type that you have displayed, so to answer your question in short... they should generally go in a folder named Interfaces in your data model class library project.

However, to answer in more detail, it is also customary to find service classes in large WPF Applications. These are responsible for providing certain functionality to the view models. For example, I have a WindowManager class that is responsible for opening dialogs among other things.

Now when I'm testing a view model that pops up a confirmation dialog, not having a user there to click Ok would break my test. So I have to have a MockWindowManager class to use while I'm testing. Therefore, I have to have an IWindowManager interface to make it all possible. Now this interface is in the services project, along with the service classes, but in a folder named Interfaces.

There are other places that we can use interfaces too, but the point is that you store the interfaces in the place that makes most sense... the place where they will be mostly used, near their implementing classes. Just to be clear though... this has nothing to do with MVVM, it's more of a general rule of thumb.

You could also have a look at my answer to the Project structure for MVVM in WPF question to see a typical MVVM project structure, which shows numerous Interfaces folders that I was referring to.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

I would move Department, Contact etc. into separate classes - they are not really part of the employee and deserve their own logic - hence their own class. Also, you might need to have different classes of salary for different people, e.g. a hourly paid COnsultant or a fixed pay person etc. pp.

Your class might look like:

public class Employee : IEmployee
{

    public string EmployeeName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

public ISalary Salary{get; set;}
}

public class Salary: ISalary
{
  public int Money{get; set;}
}

public class BigBucksSalary: ISalary
{
  public int Money{get; set;}
}

The benefit if the Interfaces are twofold: First you can change the behaviour on the fly, e.g. when you need different types of salary . Second: If you want to unit test something, it is way easier to mock an interface than a raw class.

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85