Using interfaces to refactor some duplicated code

Posts   
 
    
Posts: 254
Joined: 16-Nov-2006
# Posted on: 03-Apr-2007 13:33:12   

Guys,

We have a lot of code in some ASP.NET user controls such as this

/// <summary>
    /// Saves the SI head attachment.
    /// </summary>
    private void SaveSIHeadAttachment() {
        
        // Save record in Attachment table
        AttachmentEntity attachment = GetAttachmentObjectFromFormData();
        if (attachment.Save()) {
            // save record in link table
            SiheadAttachmentEntity siHeadAttachment = new SiheadAttachmentEntity();
            siHeadAttachment.AttachmentId = attachment.AttachmentId;
            siHeadAttachment.SiheadId = AttachmentEntityKey;
            siHeadAttachment.Save();
        }
    }

    /// <summary>
    /// Saves the transaction line attachment for PO and SI Line.
    /// </summary>
    private void SaveTransactionLineAttachment() {

        // Save record in Attachment table
        AttachmentEntity attachment = GetAttachmentObjectFromFormData();
        if (attachment.Save()) {
            // save record in link table
            TransactionLineAttachmentEntity lineAttachment = new TransactionLineAttachmentEntity();
            lineAttachment.AttachmentId = attachment.AttachmentId;
            lineAttachment.TransactionLineId = AttachmentEntityKey;
            lineAttachment.Save();
        }
    }

This is in attachment UserControl which attaches files to documents e.g. invoices / purchase orders.

The code is the part which links the actual document to the attachment entity.

Can anyone think of a useful way to refactor using the LLBLGen code? I was thinking interfaces would be a useful approach e.g. IDocumentAttachmentLink interface containing two properties

AttachmentId DocumentId

Each entity could implement the interface and the UserControl could write code based on the interface.

Could we generate these interfaces or add them through the LLBLGen generated code? If so what are the best approaches to do this, we use SelfServicing and additional partial classes to extension the base entities.

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 04-Apr-2007 11:29:29   

You can modify the code generation templates to include the new interface. Please consult the LLBLGen Pro SDK manual, for how to deal with templates.