Search code examples
oopclass-diagram

Class Diagram for Social Media Platform


In a professional social network, how would I represent connections between users? (like Linkedin) Should I create a connection class for which there would be an instance for every connection between 2 users or is that redundant? Should the user class instead have a self-association (reflexive association)?


Solution

  • Your User class would have a collection of Followings:

    public class User
    {
        // ... the other code is omitted for the brevity
        public IEnumerable<User> Followings { get; set; }
    }
    

    So if your database has the Following table:

    CREATE TABLE Followings(
        User_Id INT NOT NULL, 
        Following_Id INT NOT NULL,
        DateCreated DATE NOT NULL,
    );
    

    Do not forget to create constraints and foreign keys in your table. Then it is possible to have Following class:

    public class Followings {
        public UserId int { get; set; }
        public FollowingId int { get; set; }
        public DateCreated DateTime { get; set; }
    }
    

    and then you can write easily the following query:

    select * from Following where UserId = x.id -- or vice versa