我如何制作通用pdf生成器

问题描述 投票:0回答:0

我正在渲染 html 页面并使用 dinktopdf 创建 pdf。我在控制器中有 3 个方法。这些是我用来渲染 html 页面的 RenderViewToStringAsync、我获取发送到 html 页面的 dto 模型的 GetModel,以及我转换为 pdf 的GeneratePDF。在这里,在我的 GetModel 方法中,我采用我的 dto 模型并将其映射如下。

Certificate_Dto model = new Certificate_Dto();
var applicationVW = await _serviceManager.VW_Applications.GetEntityAsync(x => x.GUID == GUID);
model= new()
{
applicationGUID = applicationVW.GUID,
ApplicationNo = applicationVW.ApplicationNo,
MemberGUID = applicationVW.MemberGUID,
NameSurname = applicationVW.NameSurname,
...
}

然后返回模型。在这里,我想让我的 dto 和我通过 _serviceManager 达到的值 (VW_Applications) 通用。在这一点上,我认为它看到了名字的体现和表达。我还没有弄清楚如何准确地做到这一点。我有一个尚未完成的 GetModel 方法,您可以在下面找到它。此时我应该如何进行?你能回顾并指导我的方法吗?

public async Task<T> GetModel<T>(Guid GUID) where T : new()
{
    if (GUID == Guid.Empty || !Guid.TryParse(GUID.ToString(), out Guid parseGuid))
    {
        throw new Exception("Hatalı guid değeri");
    }

    T model = new T();
    
    // Get name of the T up to '_'
    var name = typeof(T).Name.Split('_')[0];

    // Get the VW service dynamically based on the type T
    var vwServiceType = _serviceManager.GetType().GetProperty("VW_" + name)?.GetValue(_serviceManager, null);
    if(vwServiceType != null)
    {
        var method = vwServiceType.GetType().GetMethod("GetEntityAsync");
        if(method != null)
        {
            var applicationVW = await (Task<VW_Application>)method.Invoke(vwServiceType, new object[] { (Expression<Func<VW_Application, bool>>)(x => x.GUID == GUID) });
            if (applicationVW != null)
            {
                string CourseMakersJSON = _serviceManager.Courses.GetValue(a => a.CourseID == applicationVW.CourseID, "CourseMakersJSON");

                List<int> CourseTypeMakerIDs = new();

                if (!string.IsNullOrEmpty(CourseMakersJSON))
                {
                    CourseTypeMakerIDs = CourseMakersJSON.Split(',').Select(int.Parse).ToList();
                }

                // Get properties of model
                var properties = typeof(T).GetProperties();
                foreach(var property in properties)
                {
                    // This assumes your model and applicationVW have the same property names
                    var value = applicationVW.GetType().GetProperty(property.Name)?.GetValue(applicationVW, null);
                    property.SetValue(model, value);
                }

                // Assuming CourseTypeMakers is a property of your model
                var courseTypeMakersProperty = properties.FirstOrDefault(p => p.Name == "CourseTypeMakers");
                if(courseTypeMakersProperty != null)
                {
                    var courseTypeMakers = await _serviceManager.VW_CourseTypeItems.GetAllAsync(a => CourseTypeMakerIDs.Contains(a.GroupID));
                    courseTypeMakersProperty.SetValue(model, courseTypeMakers);
                }
            }
        }
    }
    return model;
}
asp.net-mvc asp.net-core generics reflection expression
© www.soinside.com 2019 - 2024. All rights reserved.