protected override object SetValue(ref bool setValue, SmartValueInfo info) { //return base.SetValue(ref setValue, info); var source_value = info.SourcePropValue; if (CheckIsUnitializedProxy(source_value)) return null; // Nulls don't have to be taken cared of if (source_value == null) return null; // If we already have the object completely worked out, return the previous result. if (CheckIfAlreadyResolved(source_value)) return GetAlreadyResolved(source_value); // If the object is currently being worked out, return null in the meanwhile and put your trust in the lazy setters if (CheckIfCurrentlyResolving(source_value)) { RequestForLazySetting(source_value, info.Target, info.TargetProp); return null; } SetCurrentlyResolving(source_value); // Collections do not appear as proxies, even if they are not initialized. My convention is to return null here. var source_value_type = source_value.GetType(); if (source_value_type.GetInterfaces().Contains(typeof(IPersistentCollection))) { var collection_result = SetValueForPersistentCollection(info); SetAlreadyResolved(source_value, collection_result); return collection_result; } // Not a proxy, collection, null or previously resolved. Resolve using regular methods: // NOTE: What about proxy=>regular object=>proxy? Maybe I should rebuild here too to use recursion? //var simple_result = base.SetValue(ref setValue, info); object simple_result; if (source_value_type.IsPrimitive || source_value_type == typeof(string) || source_value_type == typeof(DateTime) || source_value_type == typeof(Guid)) simple_result = base.SetValue(ref setValue, info); else simple_result = Resolve(source_value_type, source_value); SetAlreadyResolved(source_value, simple_result); return simple_result; }
protected virtual object SetValue(ref bool setValue, SmartValueInfo info) { return info.SourcePropValue; }
private object SetValueForPersistentCollection(SmartValueInfo info) { var persistent_collection = (IPersistentCollection)info.SourcePropValue; if (!persistent_collection.WasInitialized) return null; // Initialized collection, return a new and populated IList var source_as_obj_list = (IList)info.SourcePropValue; if (source_as_obj_list.Count == 0) return null; var source_list_item_type = source_as_obj_list[0].GetType(); Type list_type = typeof(List<>).MakeGenericType(source_list_item_type); var source_as_list = (IList)info.SourcePropValue; var target_as_list = (IList)Activator.CreateInstance(list_type); foreach (var source_item in source_as_list) { //bool setValue = true; if (source_item == null) continue; //var target_item = SetValue(ref setValue, info); // Temp: Perform pre-existance checks here too if (CheckIsUnitializedProxy(source_item)) return null; if (CheckIfAlreadyResolved(source_item)) { target_as_list.Add(GetAlreadyResolved(source_item)); continue; } if (CheckIfCurrentlyResolving(source_item)) { RequestForLazySetting(source_item, target_as_list); continue; } // TODO: Change to a non-instance creating function, as this defeats the references var target_item = Resolve( source_item.GetType(), source_item ); target_as_list.Add(target_item); } return target_as_list; }