Reputation: 355
public class StudentDm : EntityBaseDm {
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormattedName
{
get
{
return LastName + ", " + FirstName;
}
}
}
public class ProviderDm {
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormattedName
{
get
{
return LastName + ", " + FirstName;
}
}
}
I have a Student class that inherits EntityBaseDm. I have a Provider class that I do not want inheriting EntityBaseDm.
I want to extract the FormattedName get method and implementation into a generic class however I don't want to put it in EntityBaseDm since it is general. I will have to make an Interface such as IFirstLastName but an interface doesn't let me implement functions. I know there are many design patterns out there, one of which can solve this problems. I wonder if anyone can point me to the right direction (at least the right pattern to use)
Upvotes: 0
Views: 152
Reputation: 10879
I agree with @ScottHannen's advice on this. Object composition would be a good fit.
However, you could create that IFirstLastName
interface you mentioned, then create an extension method.
public interface IFirstLastName
{
string FirstName { get; }
string LastName { get; }
}
public static class IFirstLastNameExtensions
{
public static string FormattedName(this IFirstLastName fln)
{
return $"{fln.LastName}, {fln.FirstName}";
}
}
public class StudentDm : EntityBaseDm, IFirstLastName
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ProviderDm : IFirstLastName
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then you could call the FormattedName
method on any class which implements IFirstLastName
:
var dm = new ProviderDm();
dm.FormattedName();
Upvotes: 2
Reputation: 29202
Before going any further, consider what will happen if you create yet another method that some of these objects should share, but others shouldn't. Trying to bring together functions through inheritance can suddenly get messy. It doesn't work more often than it does.
You may have heard the recommendation to "favor composition over inheritance." In broad terms it means that it's better to bring things together that provide needed functions rather than try achieve a hierarchy where the functions you need are in base classes.
If a "name" needs consistent functionality like formatting, that's a case for making it its own class.
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormattedName { get { return LastName + ", " + FirstName; } }
}
Then have a property on StudentDm
public Name StudentName { get; set;}
However you do it, I wouldn't recommend placing classes in a hierarchy just because of a few shared properties. Sooner or later (usually sooner) something comes along that belongs in some of the inherited classes but not others and then it gets complicated.
Upvotes: 3
Reputation: 1092
You are asking for multiple inheritance and that is not possible with c#. Your option is to create an intermediate class that inherits from EntityBaseDM
and only implements the method you want. Your ProviderDm
or StudentDm
would derive from IntermediateBaseDM
and provide more properties.
class IntermediateBaseDM : EntityBaseDM
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormattedName
{
get
{
return LastName + ", " + FirstName;
}
}
}
Upvotes: 0
Reputation: 9561
The answer is an abstract class. This is similar to an interface in that you can define declarations for the implementing classes, but you can also define implementations in the abstract class itself.
For example, you could have an abstract NameBase
class like so:
public abstract NameBase : EntityBaseDm
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormattedName
{
get
{
return LastName + ", " + FirstName;
}
}
}
Your implementing classes then inherit the abstract class:
public class StudentDm : NameBase {
}
public class ProviderDm : NameBase {
}
Note, you do not need to define the name properties in either of these classes, but they are still available to use:
var student = new StudentDm();
student.FirstName = "John";
student.LastName = "Smith";
var fullName = student.FormattedName; // returns Smith, John
Upvotes: 0