예제 #1
0
파일: DtoBase.cs 프로젝트: radtek/BootFX
        protected DtoBase()
        {
            this.Type = DtoType.GetDtoType(this.GetType());

            // walk...
            this.Values = new Dictionary <DtoField, DtoValue>();
            foreach (var field in this.Type.Fields)
            {
                if (field is DtoEntityMemberField)
                {
                    this.Values[field] = new DtoStoredValue(field.DefaultValue);
                }
                else if (field is DtoAdHocField)
                {
                    this.Values[field] = new DtoRedirectedValue((DtoAdHocField)field);
                }
                else
                {
                    throw new NotSupportedException(string.Format("Cannot handle '{0}'.", field.GetType()));
                }
            }

            // also...
            this.Links = new Dictionary <DtoLink, DtoLinkWrapper>();
            foreach (var link in this.Type.Links)
            {
                this.Links[link] = new DtoLinkWrapper();
            }
        }
예제 #2
0
        public static IDtoCapable GetConcrete(this IDtoBase dto)
        {
            var type = DtoType.GetDtoType(dto.GetType());

            if (dto.IsNew())
            {
                return(type.CreateConcreteInstance());
            }
            else
            {
                // load...
                var item = (IDtoCapable)type.ConcreteEntityType.Persistence.GetById(new object[] { dto.Id }, OnNotFound.ReturnNull);
                return(item);
            }
        }
예제 #3
0
        public static IDtoBase ToDto(this IDtoCapable item)
        {
            var type = DtoType.GetDtoType(item.GetType());
            var dto  = (IDtoBase)Activator.CreateInstance(type.Type);

            // raise...
            dto.OnInitializingFromConcrete(item);

            // walk...
            foreach (var field in type.Fields)
            {
                if (field.CanGetValueFromConcreteItem)
                {
                    var value = field.GetValueFromConcreteItem(item);
                    dto[field] = value;
                }
            }

            // end...
            dto.OnInitializedFromConcrete(item);

            // return...
            return(dto);
        }
예제 #4
0
        //public static PagedDataResult<T> ToDtos<T>(this IEnumerable<IDtoCapable> items, PagedDataRequest request)
        //    where T : IDtoBase
        //{
        //    throw new NotImplementedException("This operation has not been implemented.");
        //}

        public static List <T> ToDtos <T>(this IEnumerable <IDtoCapable> items)
            where T : IDtoBase
        {
            var dtoType = DtoType.InferDtoType(items);

            // get...
            using (var conn = Database.CreateConnection(dtoType.ConcreteEntityType))
            {
                conn.BeginTransaction();
                try
                {
                    var dtos = new List <T>();
                    foreach (var item in items)
                    {
                        dtos.Add(item.ToDto <T>());
                    }

                    // next -- preload any linked data that we can find...
                    foreach (var link in dtoType.Links)
                    {
                        var map = new Dictionary <long, IDtoBase>();
                        foreach (var dto in dtos)
                        {
                            // get...
                            var parentId = dto.GetValue <long>(link.ReferenceField);
                            if (parentId != 0 && !(map.ContainsKey(parentId)))
                            {
                                map[parentId] = null;
                            }
                        }

                        // load everything up...
                        var pages = Runtime.Current.GetPages(map.Keys, 500);
                        foreach (var page in pages)
                        {
                            var filter   = new SqlFilter(link.Link.ParentEntityType);
                            var keyField = link.Link.ParentEntityType.GetKeyFields()[0];
                            filter.Constraints.AddValueListConstraint(keyField, page);

                            // add...
                            var gots = filter.ExecuteEntityCollection();
                            foreach (IDtoCapable got in gots)
                            {
                                var id = ConversionHelper.ToInt64(filter.EntityType.Storage.GetValue(got, keyField));
                                if (map.ContainsKey(id))
                                {
                                    map[id] = got.ToDto();
                                }
                            }
                        }

                        // go back and fill them in...
                        foreach (var dto in dtos)
                        {
                            var parentId = dto.GetValue <long>(link.ReferenceField);
                            if (parentId != 0)
                            {
                                dto.SetLink(link, map[parentId]);
                            }
                        }
                    }

                    // ok...
                    conn.Commit();

                    // return...
                    return(dtos);
                }
                catch (Exception ex)
                {
                    conn.Rollback();
                    throw new InvalidOperationException("The operation failed", ex);
                }
            }
        }
예제 #5
0
 public static DtoType GetDtoType(this IDtoBase dto)
 {
     return(DtoType.GetDtoType(dto.GetType()));
 }