I have a factory of sorts that gets the proper instance of an object at runtime.
public AbstractPrompt GetPrompt(object prompt, EducationAwardsModel model)
{
var apiType = prompt.GetType();
var type = Type.GetType("TI.Web.Models.Education.AwardPrompts." + apiType.Name)
?? Type.GetType("TI.Web.Models.Education.AwardPrompts.LegacyAwardPrompts." + apiType.Name)
?? apiType;
return Activator.CreateInstance(type, prompt, model) as AbstractPrompt;
}
I'd like to turn those models into glass mapper models with Sitecore fields on them. Is it possible to do that after the instance has been created? Something like educationAwards.GlassCast(myInstance)?
The alternative I've come up with is to package each model with a SitecoreFieldCollection class and then each model will have a property of that class which is then glasscast in the constructor. Example:
Folder Structure: Models > Education > AwardPrompts > LegacyAwardPrompts > RedundantAwardPrompt > RedundantAwardPrompt.cs // SitecoreFieldCollection.cs
public class RedundantAwardPrompt : AbstractPrompt
{
public RedundantAwardPrompt() {
Fields = item.GlassCast<SitecoreFieldCollection>();
}
public RedundantAwardPrompt(object prompt, EducationAwardsModel model)
{
RedundantAwards = ((EducationGateway.RedundantAwardPrompt)prompt).RedundantAwards;
using (var g = GatewayManager.Provider.Create<ClubDataGatewayClient>())
{
ClubNames = g.GetClubByClubId(AuthorizationUtility.ApplicationKey,
RedundantAwards.Select(c => c.ClubAwarded).Distinct().ToArray())
.ToDictionary(x => x.Id, x => x.Name);
}
}
public IEnumerable<LegacyEducationProgramAward> RedundantAwards { get; set; }
public IDictionary<ClubID, string> ClubNames { get; set; }
public SitecoreFieldCollection Fields { get; set; }
public override IEnumerable<object> GetResponse()
{
return new[] { new RedundantAwardPromptResponse() };
}
}
And I would do that for each prompt, but that seems redundant.