简体   繁体   中英

Architectural/best-practices question about generics

I'm working my way through 'head first design patterns' and want to use this in practice immediately. I'm writing a piece of code that connects an application with other applications. In fact, I need to generate an e-mail containing an XML file and send it via e-mail. But other stuff might be required in the future.

Thus, I identified 'the things that change': - The data for the transmission - The means of transmitting (could be e-mail, but could be FTP or webservice for another data-exchange)

So, I: - Created an abstract class DataObject - Created an interface ITransmissionMethod - Created an dataExchange abstract class:

  abstract class DataExchange<T,U>
    {
        private T DataObject;
        private U SendMethod;
    }

And SendViaMail is like

class SendViaMail : ISendMethod<System.Net.Mail.Attachment>
{
    public override void Send(System.Net.Mail.Attachment dataItem)
    {
        throw new NotImplementedException();
    }
}

Now - I can create classes like:

class MyExchange : DataExchange<MyDataObject,SendViaMail> { }

What do you think about this approach? Now what I would really like to do is Create an abstract method in DataExchange that should look something like

private abstract [the type of the T in ISendMethod<T>] PrepareObjectForSending(T dataObject) {
}

Visual Studio would force me to implement a method like:

private abstract System.Net.Mail.Attachment PrepareObjectForSendingMyDataObject dataObject) {
// Serialize XML file and make it into attachment object
}

Wouldn't that be sweet? But what do you guys think about this approach? In the future, people can create new dataObjects and new sendmethods and the code would still work. What I've been trying to do is: program against interface and extract changing parts. How about it?

That would work, but you could separate concerns even more. Here is just another version - make DataExchange to be very simple and delegate real work to workers:

class DataExchange<TDataObject, TTransmissionObject>
{
    IConverter<TDataObject, TTransmissionObject> conterver;
    ISendMethod<TTransmissionObject> sender;

    public Send(TDataObject dataObject)
    {
        TTransmissionObject tro = conterver.Convert(dataObject);
        sender.Send(tro);
    }
}

Converts would just convert data objects to objects suitable for transmission:

class DataToAttachmentConverter : IConverter<DataObject, Attachment>
{
    Attachment Convert(DataObject) { }
}
class DataToXmlConverter : IConverter<DataObject, XmlDocument>
{
    XmlDocument Convert(DataObject) { }
}

Senders would only send.

class MailSender : ISendMethod<Attachment>
{
    void Send(Attachment) {}
}
class FtpPublisher : ISendMethod<XmlDocument>
{
    void Send(XmlDocument) {}
}

Putting all together:

var exchanges = new [] {
      new DataExchange<DataObject, Attachment>( new DataToAttachmentConverter(), new MailSender()),
      new DataExchange<DataObject, XmlDocument>( new DataToXmlConverter(), new FtpPublisher())
};

foreach(var ex in exchanges)
    ex.Send(dataObject); //send as an attachent and put to ftp site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM