Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Thrown By CreateDynamicTask in Glass.Mapper.Sc #25

Closed
mikeedwards83 opened this issue Sep 18, 2013 · 1 comment
Closed

Error Thrown By CreateDynamicTask in Glass.Mapper.Sc #25

mikeedwards83 opened this issue Sep 18, 2013 · 1 comment
Labels

Comments

@mikeedwards83
Copy link
Owner

I was using Glass.Mapper version 3.0.4.40 and Glass.Mapper.Sc version 3.0.5.52. Now i updated both assemblies to the lastest (3.0.5.49 and 3.0.6.60).

I have a class like that :

 [SitecoreType(AutoMap = true)]
 public class FredContainer : ContentBase
 {
    [SitecoreChildren(InferType = true)]
    public virtual IEnumerable<ContentBase> Children { get; set; }

    [SitecoreParent]
    public virtual ContentBase Parent { get; set; }
 }

This worked fine with the mentionned assembly but not in the new one. In sitecore, i have an Item that has children. I don't have any class that matches the children because I am only interested in the content inside the children. So I wrote this.MasterService.GetItem(id) to retrieve the parent, then I wrote a loop in all children and another loop for all children's children. The Mapper throws an exception : "Object reference not set to an instance of an object" for the children of the parent.
In the previous version, that same code returned the ContentBase properties and the content was correctly mapped.
I can rollback to the previous version but i'll prefer to stay up to date with the assemblies.

This is the stack trace
[NullReferenceException: Object reference not set to an instance of an object.]
Glass.Mapper.Sc.Pipelines.ObjectConstruction.CreateDynamicTask.Execute(ObjectConstructionArgs args) +37
Glass.Mapper.Pipelines.AbstractPipelineRunner2.Run(T args) +224 Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext) +988 Glass.Mapper.Sc.SitecoreService.CreateType(IEnumerable1 types, Item item, Boolean isLazy, Boolean inferType, Object[] constructorParameters) +264
Glass.Mapper.Sc.d__2.MoveNext() +322
System.Collections.Generic.List1..ctor(IEnumerable1 collection) +535
System.Linq.Enumerable.ToList(IEnumerable1 source) +79 Glass.Mapper.Sc.LazyItemEnumerable1.<.ctor>b__0() +69
System.Lazy1.CreateValue() +14442936 System.Lazy1.LazyInitValue() +476
Glass.Mapper.Sc.LazyItemEnumerable1.GetEnumerator() +26 System.Linq.Enumerable.Count(IEnumerable1 source) +224
ImportLogos() in MyProject

This is the method that I use to create the sitecore items.

 public void ImportLogos()
 {

            Container logoRepo = this.ProdService.GetItem<Container>(new Guid(LogosRepositoryId));

            Language english = LanguageManager.GetLanguage(LanguageEn);
            Language french = LanguageManager.GetLanguage(LanguageFr);
            Guid repositoryId = new Guid(LogoRepo);

            var repoEn = this.MasterService.GetItem<ContentBase>(repositoryId, english);
            var repoFr = this.MasterService.GetItem<ContentBase>(repositoryId, french);

            for (int i = 0; i < logoRepo.Children.Count(); ++i) // The logoRepo.Children is NULL.
            {
                FredContainer FredProductLogo = this.ProdService.GetItem<Container>(logoRepo.Children.ElementAt(i).Id);
                foreach (var child in FredProductLogo.Children)
                {
                    Logo myLogo = child as Logo;
                    if (myLogo == null)
                    {
                        continue;
                    }

                    var logoEn = this.CreateLogo(myLogo.Id, english, repoEn);

                    var logoFr = this.CreateLogo(myLogo.Id, french, repoFr);

                    var logo = logoEn ?? logoFr;

                    foreach (var logoFile in myLogo.LogoFiles)

                    {
                        this.CreateLogoFile(logoFile);
                        this.CreateLogoFile(logoFile);
                    }
                }
            }
 }

As you can see, I retrieve the children through the Container, then I use the children to get it's children.
The Container class I have put it in the last mail. This is the ContentBase

 [SitecoreType(AutoMap = true)]
public class ContentBase : IContentBase
{
    /// <summary>
    /// The Name of the Data section of the item.
    /// </summary>
    [SitecoreField(FieldName = "Name")]
    public virtual string DataName { get; set; }

    [SitecoreField]
    public virtual string Title { get; set; }

    [SitecoreId]
    public virtual Guid Id { get; set; }

    /// <summary>
    /// The Name of the Sitecore item.
    /// </summary>
    [SitecoreInfo(SitecoreInfoType.Name)]
    public virtual string ItemName { get; set; }

    [SitecoreInfo(SitecoreInfoType.DisplayName)]
    public virtual string DisplayName { get; set; }

    [SitecoreInfo(SitecoreInfoType.Language)]
    public virtual Language Language { get; set; }

    [SitecoreInfo(SitecoreInfoType.Url)]
    public virtual string Url { get; set; }

    [SitecoreInfo(SitecoreInfoType.Version)]
    public virtual int Version { get; set; }

    [SitecoreInfo(SitecoreInfoType.TemplateId)]
    public virtual Guid TemplateId { get; set; }

    #region Items Bucket

    [DoNotLog]
    [SitecoreField(FieldName = "IsBucket")]
    public virtual bool IsBucket { get; set; }

    /// <summary>
    /// Used to know if the item can be contained in a bucket.
    /// </summary>
    [DoNotLog]
    [SitecoreField(FieldName = "Bucketable", FieldId = FredRevampTemplateConstants.Bucketable)]
    public virtual bool Bucketable { get; set; }

    [DoNotLog]
    [SitecoreField(FieldName = "Lock Child Relationship")]
    public virtual bool LockChildRelationship { get; set; }

    #endregion

}
The real problem is when there's one item in the children that glass doesn't reconize, he will return null instead of only mapping the ContentBase which should be enough in my scenario.

Thank you

@mikeedwards83
Copy link
Owner Author

Issues has been solved and checked in to the develop branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant