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(); } }
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); } }
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); }
public static DtoType GetDtoType(this IDtoBase dto) { return(DtoType.GetDtoType(dto.GetType())); }