What should a Read (as in CRUD) method return for a class that uses another class with a Read method?

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

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

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *