Ejemplo n.º 1
0
        /// <summary>Convert from LookupProperty entity to DTO</summary>
        /// <param name="dbContext">DB Context to use for setting DTO state</param>
        /// <param name="dto">DTO to use if already created instead of creating new one (can be inherited class instead as opposed to base class)</param>
        /// <param name="entityDtos">Used internally to track which entities have been converted to DTO's already (to avoid re-converting when circularly referenced)</param>
        /// <returns>Resultant LookupProperty DTO</returns>
        public LookupPropertyDto ToDtoDeep(FACTS.Framework.DAL.DbContext dbContext, LookupPropertyDto dto = null, Dictionary <BaseEntity, FACTS.Framework.Dto.BaseDto> entityDtos = null)
        {
            entityDtos = entityDtos ?? new Dictionary <BaseEntity, FACTS.Framework.Dto.BaseDto>();
            if (entityDtos.ContainsKey(this))
            {
                return((LookupPropertyDto)entityDtos[this]);
            }

            dto = ToDto(dto);
            entityDtos.Add(this, dto);

            System.Data.Entity.Infrastructure.DbEntityEntry <LookupProperty> entry = dbContext?.Entry(this);
            dto.IsNew     = (entry?.State == EntityState.Added);
            dto.IsDeleted = (entry?.State == EntityState.Deleted);

            if (entry?.Reference(x => x.LookupName)?.IsLoaded == true)
            {
                dto.LookupName = LookupName?.ToDtoDeep(dbContext, entityDtos: entityDtos);
            }
            if (entry?.Collection(x => x.LookupValues)?.IsLoaded == true)
            {
                foreach (LookupValue lookupValue in LookupValues)
                {
                    dto.LookupValues.Add(lookupValue.ToDtoDeep(dbContext, entityDtos: entityDtos));
                }
            }

            return(dto);
        }