1

I am trying to build a very basic instant messaging platform. I have three basic immutable types, and each of them has a subclass for dealing with CRUD (for SRP purposes, separating the class from the database logic).

Conversation:

public class Conversation
{
    public uint Id { get; private set; }
    public List<Participant> Participants { get; private set; }
    public List<Message> Messages { get; private set; }
    /* Some extra properties */        

    public Conversation(uint id, List<Participant> participants, List<Message> messages)
    {
        Id = id;
        Participants = participants;
        Messages = messages;
    }

    public static class CRUD
    {
       static void Create(...)  {}
       static void Read(...)  {}
       static void Update(...)  {}
       static void Delete(...)  {}
    }
}

Message:

public class Message
{
    public uint Id { get; private set; }
    public Conversation Conversation { get; private set; }
    public Participant Sender { get; private set; }
    public DateTime SendingTime { get; private set; }
    public string Content { get; private set; }

    /* Constructor */
    /* CRUD */
}

Participant:

public class Participant
{
    public Conversation Conversation { get; private set; }
    public Employee Employee { get; private set; }

    /* Constructor */
    /* CRUD */
}

I thought Message.CRUD.Read(...) should get the messages from the database, and Participant.CRUD.Read(...) should get the participant from the database. But then What should Conversation.CRUD.Read(...) read and what should it return?

I mean, should it get from the database all the information about the conversation that has no class representation in the code (i.e. not Message and Participant), and then, should it return a non-complete object of Conversation with some nulls or should it do something else? What is the best practice here? Is there any?

1
  • I am sorry for my English. If my question is not clear please tell me so I could fix it. If you think you can improve it - PLEASE DO. Thanks. Commented Sep 13, 2016 at 8:13

1 Answer 1

3

Your problem is that your Conversation class does not correspond to one record in the database. The Message class and the Participants classes do.

If you do want to apply the same CRUD technique to conversations you should have a ConversationParticipant class (loading the list of participants from its table and the participant information via the Participant class) and ConversationMessage class (similarily loading via the Message class).

Then the Conversation class could use these two classes to load the participants and the messages and it could load the main information on the Conversation itself.

3
  • Why can't a Conversation class be a record in the database? For this example, it only has an ID, but if you want to store more properties like start date or owner or even give it a title, it would benefit from a record in the db.
    – JeffO
    Commented Sep 13, 2016 at 13:45
  • 1
    The specific Conversation class in the question is not one record in the database, it contains lists of messages and participants which I do not hope is stored in one record, and the question was how were the CRUD methods on that class supposed to work. The last paragraph in my answer mentions the main infromation on the conversation, which could be title, owner etc.
    – Bent
    Commented Sep 13, 2016 at 13:59
  • I see you're referring to the entire class, my mistake.
    – JeffO
    Commented Sep 17, 2016 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.