jeudi 6 décembre 2018

Whats Pattern use for lazy loading data?

Can I ask you here?

I currently have a code that fetches data from the database and using DocX (C#) I do replace information to generate a word document

My sample:

public class DocumentGenerateService
{
    public WordDocument DocWord { get; set; }
    public ResultDTO Data { get; set; }
    public AppDbContext _db { get; set; }

    public DocumentGenerateService(AppDbContext db)
    {
        DocWord = new WordDocument(@"C:\Base.docx");
        Data = new ResultDTO();
        _db = db;
    }

    public MemoryStream Generate(Guid id)
    {
        LoadBase(Id);
        LoadFields(Id);
        Compile();

        return DocWord;
    }
    public void LoadBase(Guid id)
    {
        Data.AnyProperty = _db.Xpto.AsNoTracking().Where(x => x.Id == id).Select(x => new AnyDTO...).FirstOrDefault();

    }
    public void LoadFields(Guid Id)
    {
        Data.Fields = _db.OtherData.Where(x => x.AnyPropertyId == Id).Select(x => new OtherDTO ... ).ToList()
    }
    public static void Compile()
    {
        DocWord.Replace("<<AnyProperty>>", Data.Date.ToString("dd/MM/yyyy"), true, true);
        DocWord.Replace("<<PropertyExample>>", Data.Company, true, true);
        DocWord.Replace("<<PropertyExample>>", Data.CompanyDocument, true, true);
        DocWord.Replace("<<PropertyExample>>", Data.CompanyAddress ?? "", true, true);
        DocWord.Replace("<<PropertyExample>>", Data.CompanyAddressNumber ?? "", true, true);

        DocWord.CompileList = Data.Fields;
    }
}

What design pattern should I use in this case?

I want to leave my code clean

Aucun commentaire:

Enregistrer un commentaire