samedi 13 juin 2020

Trying to apply builder design pattern with the below methods

I have class like this as below for design project where i am using this one to generate word document. the process is we are using ReactJS UI and .Net core and when the user click on button in UI we will be sending the request to API where we can download the word document.

public class DesignProject : PatchEntityProperties
{
    [Key, GraphQLNonNullType]
    public string ProjectNumber { get; set; }
    public string Name { get; set; }
    [Column(TypeName = "jsonb")]
    public ProjectSectionStatus SectionStatuses { get; set; }
    [ForeignKey("AshraeClimateZone"), GraphQLIgnore]
    public Guid? AshraeClimateZoneId { get; set; }
    public virtual AshraeClimateZone AshraeClimateZone { get; set; }
    public virtual ProjectPhase ProjectPhase { get; set; }
    [Column(TypeName = "jsonb")]
    public List<ProjectObject<LocalCode>> LocalCodes { get; set; } = new List<ProjectObject<LocalCode>>();
    .........
    ......
}

and project object is looks like as below

public class ProjectObject<T>
{
    public Guid? Id { get; set; }
    public T OriginalObject { get; set; }
    public T ModifiedObject { get; set; }

    [GraphQLIgnore, JsonIgnore]
    public T TargetObject
    {
        get
        {
            return ModifiedObject ?? OriginalObject;
        }
    }

}

and then i do have individual classes as mentioned in DesignProject class are like as below

public class LocalCode : AEIMaster
{
    public string Edition { get; set; }
    public string City { get; set; }
    public State State { get; set; }
    public Country Country { get; set; }        
}

i have few more classes and all are implementing AEIMaster interface used in word document generation and code is like as below

this is controller method where i am calling from UI

[Route("api/[controller]")]
[ApiController]
public class DesignProjectsController : ControllerBase
{
    private readonly APIDbContext _context;

    public DesignProjectsController(APIDbContext context)
    {
        _context = context;
    }

    // GET: api/DesignHubProjects/00000-00
    [Authorize, HttpGet("{id}")]
    public async Task<ActionResult<DesignProject>> GetDesignProject(string id)
    {
        var designProject = await _context.DesignProjects.FindAsync(id);
        MemoryStream document = new DocumentGeneration().GenerateBasisOfDesign(designProject);

        return new InlineFileContentResult(document.ToArray(), "application/docx") { FileDownloadName = fileName };
    }
}

and then below is the code for DocumentGeneration class

    public MemoryStream GenerateBasisOfDesign(DesignProject designProject)
    {
        DesignProj = designProject;
        MemoryStream mem = new MemoryStream();

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document))
        {
            var mainDocumentPart = wordDoc.AddMainDocumentPart();
            Document doc = new Document();
            mainDocumentPart.Document = doc;
            doc.Body = new Body();

            Body body = wordDoc.MainDocumentPart.Document.Body;
            if (Condition1)
            {
                body.AppendChild(new Paragraph(new Run(new Text())));
                if ((designProject.LocalCodes?.Count ?? 0) > 0)
                {
                    body.AppendChild(new Paragraph(new Run(new Text())));
                    body.AppendChild(BuildSubHeaderPart("Applicable Local Codes", 2));
                    body.Append(RenderBulletedList(wordDoc, RenderRunList(designProject.LocalCodes.Select(s => s.TargetObject).Select(selector).ToList())));
                }
            }
            if(condition 2)
            {
                // use other classes which are derived from AEIMaster
                // append those to body 
            }
            mainDocumentPart.Document.Body = body;
            mainDocumentPart.Document.Save();
            ApplyHeader(wordDoc);
        }
        return mem;
    }

i am looking to apply builder design pattern for this method and classes and i know it will separates the construction of complex object from its representation and i am not sure where to start with the above classes and methods.

Could any please suggest ideas or suggestions on this, many thanks in advance.

Aucun commentaire:

Enregistrer un commentaire