0

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.

2

I am not 100% sure what you are trying to achieve but if you want to map a model after it has been instantiated then you can use the Map method:

https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/SitecoreService.cs#L1511

This was originally designed for working with Sitecore search. Your model will need ID and Language properties that already have values so that Glass can work out which item to map onto your model.

0

You could potentially use the CreateType(Type type) overload, this would allow you to do Type.FromName(string typeName) to allow you to map at runtime

https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/ISitecoreService.cs#L102

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.