mercredi 7 février 2018

How do you make an abstract or base class out of these classes

Consider that I have couple of classes which are parsing a text file and produce different objects. for example:

public MusicContract Parse(string line)
        {
            var separatedItems = line.Split(ContractConstants.LineItemSeparatorToken);

            var artist = separatedItems[0]; 
            var title = separatedItems[1];
            var usage = _usageParser.Parse(separatedItems[2]);
            var startDate = _dateParser.Parse(separatedItems[3]);
            DateTime? endDate = null;
            if (separatedItems.Length > 4 && !string.IsNullOrEmpty(separatedItems[4]))
                endDate = _dateParser.Parse(separatedItems[4]);

            return new MusicContract
            {
                Artist = artist,
                Title = title,
                Usages = usage,
                StartDate = startDate,
                EndDate = endDate
            };
        }

or another one:

 public DistributionContract Parse(string line)
        {
            var separatedItems = line.Split(ContractConstants.LineItemSeparatorToken);

            var partner = separatedItems[0];
            var usage = _usageParser.Parse(separatedItems[1]);
            return new DistributionContract
            {
                PartnerName = partner, 
                Usages = usage
            }; 
        }

and I have 10 more of these classes. Now I wanted to make an abstraction out of them what is the best way ? can be this a solution

all the Parse methods are returning a model like this

 public class model 
    {
        string Type; 
        string JsonContent; 
    }

and then when I have the return I know that I have to deserilize the content into which type. But does it worth to do this?

I mean shall we go toward abstraction at any price? my application is working but I feel not good about the code because the way currently we learn programming is so emphasises on the abstraction and generalization that if I make a code like above I have bad conscious that I am doing something wrong.

Aucun commentaire:

Enregistrer un commentaire