Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create class based on roles

Tags:

c#

oop

I am developing a application for sports clubs administraion. And my problem is that I have one primary class Member which contains all the "default" information (name, surname, gender...) and two other classes Coach and Practitioner which inherit from Member. A coach has some specific properties (salary, trainings held in current month...) wheres a practitioner has some others (isCompetitor, category ...)

The problem is that a Practitoner can also be a Trainer as well as the other way around. How can I model this into something that is better then having two entries for the same person?

Edit: this is how it looks now

Class Member {}
Class Coach:Member {}
Class Practitioner:Member {}
like image 628
Ivan Crojach Karačić Avatar asked Mar 02 '26 18:03

Ivan Crojach Karačić


1 Answers

You can create one class 'member' that contains a list of roles. Each role (coach and/or practitioner) inherit from a base class 'role' which contains all properties you now have in your member class. Coach and practitioner than have their own specific properties. So:

public class Member {

    public IList<Role> Roles { get; private set; }

    public Member(){
        Roles = new List<Role>();
    }

}

public class Role {

    public string SomeGlobalProperty { get; set; }

}

public class Coach : Role {

    public string SomeSpecificProperty { get; set; }

}

public class Practitioner : Role {

    public string SomeSpecificProperty { get; set; }

}
like image 120
Bas Slagter Avatar answered Mar 05 '26 12:03

Bas Slagter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!