Background
I am developing a WPF project.
This is the basic structure:
- User Interface (WPF Project);
- Interfaces (class library, contains all the interfaces and the entities used by the application;
- Modules (every module contains the logic of a specific argument, e.g. File Management, and can eventually contains Wpf User Controls).
In the WPF Controls, to facilitate the binding operations I have created a BaseViewModel class which contains a Raise method that automates the binding mechanism (for further details, I used a technique similar to that one described in this article).
The problem
Understand which is the best way to separate Presentation form from the Business form in the entities classes.
The case
In the Interfaces project I have, for instance, the class User
public class User
{
public virtual string Name { get; set; }
// Other properties
}
In one of the modules I need to use the User
class and to bind its properties to the User Interface controls. To do so I have to use a custom implementation of the get
and set
keywords.
At first point, I thought to create a class in the Module called, for instance, ClientUser and override the properties that I need:
public class ClientUser : User
{
private string name;
public override string Name { get { return name; } set { Raise(out name, value); } }
// Other properties
}
The problem is the Raise method, which is declared in the BaseViewModel
class, but due to C# single inheritance constraint, I can’t inherit from both classes.
Which is the right way to implement this architecture?
2
Use interfaces
public interface IUser
{
string Name { get; set;}
}
And now you can have two implementations,
public class User : IUser
{
public string Name { get; set; }
}
and
public class ClientUser : BaseViewModel, IUser
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
Raise(out _name, value);
}
}
}
As a positive side effect, you got rid of the virtual.
If your client user is simply a viewmodel for a user entity (which it seems to be), you most likely want a different design. Given IUser and User from above, you would write a viewmodel class:
public UserViewModel : BaseViewModel
{
private readonly IUser _user;
UserViewModel(IUser user)
{
_user = user;
}
public string Name
{
get
{
return _user.Name;
}
set
{
_user.Name = value;
Raise(out _name, value);
}
}
}
5