0

I have 3 object classes:

public class MessageData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
    public string EmployeeInfo { get; set; }
}

public class MultiMessageData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
    public byte MessageType { get; set; }
    public string MessageType { get; set; }
    public string Platform { get; set; }
    public string EmployeeInfo { get; set; }
}

public class SocialData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
    public bool IsReceived { get; set; }
    public byte MessageType { get; set; }
    public string MessageType { get; set; }
    public string Message { get; set; }
    public string UserName { get; set; }
}

Then I have a function right now that only implement the MessageData class:

public void SendData()
{
    List<MessageData> data = new List<MessageData>();

    // some work here with data variable...

    _sendRepository.Send(data);
}

Now what I need is to keep most of the logic but a way that I can instantiate a variable that can be of any class type for example:

public void SendData()
{
    var data;

    if(logic == 1){
        data = new List<MessageData>();
    } else if(logic == 2){
        data = new List<MultiMessageData>();
    }else {
        data = new List<SocialData>();
    }

    // some work here...

    // if I do that then this Send method I will change it to be a Generic T maybe.
    _sendRepository.Send(data);
}

Any clue?

7
  • inheritance I presume? Commented Sep 13, 2017 at 15:27
  • Do you can use generic method for it? I mean that you can swap definition of SendData to SendData<T> and invoke closed type by your logic Commented Sep 13, 2017 at 15:27
  • 6
    Use interfaces. Commented Sep 13, 2017 at 15:28
  • @GeorgeAlexandria The repository Send method will have a generic method but the SendData method cant be Generic Commented Sep 13, 2017 at 15:29
  • 1
    @Alander yes you can. Commented Sep 13, 2017 at 15:43

3 Answers 3

1

Inheritance is the way to go:

public class MessageDataBase
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
}
public class MessageData : MessageDataBase
{
    public string EmployeeInfo { get; set; }
}

public class MultiMessageData : MessageDataBase
{
    public byte MessageType { get; set; }
    public string MessageTypeString { get; set; }
    public string Platform { get; set; }
    public string EmployeeInfo { get; set; }
}

public class SocialData : MessageDataBase
{
    public bool IsReceived { get; set; }
    public byte MessageType { get; set; }
    public string MessageTypeString { get; set; }
    public string Message { get; set; }
    public string UserName { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But then in the funciton method how can I do my logic?
You would have the Send method take a parameter of MessageDataBase that way it can just take any Message that inherits from MessageDataBase.
1

Use an interface, but you will have to make common changes in the //..do some work here section

public interface IMessageData
{
    //Example required function for all MessageData classes
    void SetDate(DateTime date);
    void SetIsSent(bool date);
}
public abstract class BaseMessageData : IMessageData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }

    public BaseMessageData(string messageID)
    {
        MessageID=messageID;
    }
    public void SetDate(DateTime date)
    {
        Date = date.ToString("MM/dd/yyyy");
    }
    public void SetIsSent(bool sentStatus)
    {
        IsSent = sentStatus;
    }
}
public class MessageData : BaseMessageData, IMessageData
{

    public string EmployeeInfo { get; set; }

}

public class MultiMessageData : MessageData,IMessageData
{
    public byte MessageType { get; set; }
    public string MessageType { get; set; }

    public string Platform { get; set; }
}

public class SocialData : BaseMessageData, IMessageData
{
    public bool IsReceived { get; set; }

    public byte MessageType { get; set; }
    //Can't have to properties named the same. 
    //public string MessageType { get; set; }

    public string Message { get; set; }

    public string UserName { get; set; }
}

Then in your SendData

public void SendData()
{
    List<IMessageData> data;

    if (logic == 1)
    {
        data = new List<MessageData>();
    }
    else if (logic == 2)
    {
        data = new List<MultiMessageData>();
    }
    else
    {
        data = new List<SocialData>();
    }

    // some work here...
    foreach (var d in data)
    {
        //Only methods that will work are ones declared in the IMessageData interface
        d.SesDate(DateTime.Now);
        d.SetIsSent(true);
    }

    // if I do that then this Send method I will change it to be a Generic T maybe.
    _sendRepository.Send(data);
}

1 Comment

This is a good solution. Interfaces should be used in these kind of scenarios.
0

You can use my open source library, OneOf https://github.com/mcintyre321/OneOf

A OneOf is a special Type which can hold one value from a range of Types, like a Discriminated Union in F#. It provides an alternative to using inheritance/polymorphism.

You would write your method like this:

public void SendData()
{
    OneOf<List<MessageData>, List<MultiMessageData>, List<SocialData>> data = null;

    if(logic == 1){
        data = new List<MessageData>();
    } else if(logic == 2){
        data = new List<MultiMessageData>();
    }else {
        data = new List<SocialData>();
    }

    // some work here...


    _sendRepository.Send(data);
}

Or you could have var data = List<OneOf<MessageData, MultiMessageData, SocialData>>(); if you prefer, especially if you want to have different Types in the list.

install-package OneOf

It supports net451 and netstandard1.6

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.