Let’s say there are 2 classes that have the same prop, with logic in that prop as well.
public class Manager
{
private string _ssn { get; set; }
[RegularExpression(@"\d{9}")]
public string SSN
{
get
{
return _ssn;
}
set
{
if (!string.IsNullOrEmpty(value))
_ssn = someLogicalMethod (value);
}
}
}
And
public class Employee
{
private string _ssn { get; set; }
[RegularExpression(@"\d{9}")]
public string SSN
{
get
{
return _ssn;
}
set
{
if (!string.IsNullOrEmpty(value))
_ssn = someLogicalMethod(value);
}
}
}
Ideally, you’d make an abstract base class (BaseSSN) these would inherit from. That’s all well and good, but not ideal! The reason for this is because in C# you can only inherit from 1 super class. That means that if you have a case where Manager and Employee both have SSN, but Manager and a new class (CEO) have a ManagerOf property, then you have to then create a new class that implements the BaseSSN class but it would also be an abstract class (BaseManagerOf) that has the logic for Managerof inside of it. Then, Manager and CEO would inherit from the new class. However, now you would have to create still another class if someone was a manager but themselves did not have an SSN. You would need to pull out the Manager prop and put it in a super, to separate it from the SSN class.
Do you see where I’m going with this? It could create N number of variations depending on the amount of props each class has that are similar to other classes.
So what I would like to do is simply
Public class Manager : BaseSSN, BaseManagerOf, X
Where X is anything. You can do this with interfaces, but interfaces can’t contain logic.
I believe there is probably a smooth strategy that will solve this, but can’t figure it out for the life of me.
EDIT:
There seems to be a small amount of confusion regarding my question: this is a hypothetical question. I understand that managers are employees. The simple question is that i want one class to implement 2 abstracts and cannot do that in C#. However, is there a strategy that would allow this to work in any way?
public class Manager : BaseManager
{
}
public abstract class BaseManager : BaseSSN
{
private string _managerOf;
public int ManagerOf
{
get
{
return _managerOf;
}
set
{
if (!string.IsNullOrEmpty(value))
_managerOf = someLogicalMethod(value);
}
}
}
public abstract class BaseSSN
{
private string _ssn { get; set; }
[RegularExpression(@"\d{9}")]
public string SSN
{
get
{
return _ssn;
}
set
{
if (!string.IsNullOrEmpty(value))
_ssn = someLogicalMethod(value);
}
}
}
public class CEO : ManagerOf {}
Now it begins to fall apart with CEO. I don't want the CEO's SSN, only the person he manages. So I would have to pull the logic out of the ManagerOf class that inherits SSN, and put that in its own class. Then CEO wouldn't have the SSN prop. However, now I can't have both ManagerOf logic and SSN logic in my Manager class without creating yet another abstract that looks like the following class...
public class ManagerOfAndSSN: SSN
{
// repetitive manager logic here
}