В настоящее время я работаю над конвертером для преобразования компонентов из одной системы CMS в другую систему CMS.
компоненты в старых cms есть общий базовый класс с указанием имени компонента -
которое также используется в качестве дифференциатора для различных реализаций этого базового класса.
пример:
Код: Выделить всё
public abstract class ComponentBase
{
public string? ComponentName { get; set; }
}
public class RichTextElement : ComponentBase
{
public required string Title { get; set; }
public required string Content { get; set; }
}
Код: Выделить всё
public abstract class BaseConverter
{
protected readonly ConverterTools _converterTools; ´
protected BaseConverter(ConverterTools converterTools)
{
_converterTools = converterTools;
}
public abstract BlokComponent ConvertComponent(T component);
public virtual IEnumerable ConvertComponents(List components)
{
return components.Select(ConvertComponent);
}
}
Код: Выделить всё
namespace UniversalRobots.Web.MigrationJobs.StoryblokImport.Converters;
public class RichTextElementConverter : BaseConverter
{
public RichTextElementConverter(ConverterTools converterTools) : base(converterTools)
{
}
private BlokTextComponent CreateText(string? title, string text)
{
var textComponent = new TextComponent()
{
Uuid = Guid.NewGuid(),
Component = "text-v1",
Title = title ?? string.Empty,
Text = text),
};
return textComponent;
}
public override BlokComponent ConvertComponent(RichTextElement component)
{
return CreateText(component.Title, component.Content);
}
}
Я попробовал с это пока - но, похоже, возникают проблемы с тем, что BaseConverter слишком общий и должен быть конкретным?
Чего я не понимаю,
Код: Выделить всё
public class ConverterFactory
{
private readonly ConverterTools _converterTools;
private readonly Dictionary _converters;
public ConverterFactory(ConverterTools converterTools)
{
_converterTools = converterTools;
_converters = new Dictionary
{
{ typeof(RichTextElement), context => new RichTextElementConverter(_converterTools) },
};
}
public BaseConverter? GetConverter()
{
if (_converters.TryGetValue(typeof(T), out var converterFactory))
{
return converterFactory(_converterTools) as BaseConverter;
}
return null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -converter
Мобильная версия