Exemplo n.º 1
0
		// --------------------------------------------------
		public static KMap<Region> CreateMapRegion( IKLink link )
		{
			var map = new KMap<Region>( link, x => x.Regions );
			map.UnManagedColumns( "ParentId" );

			map.CreateInstance = () => { return new Region(); };
			map.ClearInstance = obj => { obj.Clear(); };

			map.WriteRecord = ( obj, record ) => {
				DEBUG.IndentLine( "\n-- Map<Region>.WriteRecord() Instance = {0}", obj );
				map.BaseWriteRecord( obj, record );
				record.OnSet( "ParentId", () => { return obj.Parent == null ? null : obj.Parent.Id; } );
				DEBUG.Unindent();
			};
			map.LoadRecord = ( record, obj ) => {
				DEBUG.IndentLine( "\n-- Map<Region>.LoadRecord() Record = {0}", record );
				map.BaseLoadRecord( record, obj );
				record.OnGet( "ParentId", val => { obj.Parent = val == null ? null : map.Find( x => x.Id == val ); } );
				DEBUG.Unindent();
			};
			map.OnRefresh = obj => {
				DEBUG.IndentLine( "\n-- Map<Region>.OnRefresh() Instance = {0}", obj );
				obj = map.BaseOnRefresh( obj );
				obj.ChildRegions.Clear(); obj.ChildRegions.AddRange( link.Query<Region>().Where( x => x.ParentId == obj.Id ).ToList() );
				obj.Countries.Clear(); obj.Countries.AddRange( link.Query<Country>().Where( x => x.RegionId == obj.Id ).ToList() );
				DEBUG.Unindent(); return obj;
			};

			map.OnInsert = obj => {
				DEBUG.IndentLine( "\n-- Map<Region>.OnInsert() Instance = {0} ...", obj );
				obj.Parent = link.Sync( obj.Parent, KMaps.SyncReason.Insert );
				obj = map.BaseOnInsert( obj );
				link.Sync<Region>( obj.ChildRegions, KMaps.SyncReason.Insert );
				link.Sync<Country>( obj.Countries, KMaps.SyncReason.Insert );
				DEBUG.Unindent(); return obj;
			};
			map.OnUpdate = obj => {
				DEBUG.IndentLine( "\n-- Map<Region>.OnUpdate() Instance = {0} ...", obj );
				obj.Parent = link.Sync( obj.Parent, KMaps.SyncReason.Update );
				obj = map.BaseOnUpdate( obj );
				link.Sync<Region>( obj.ChildRegions, KMaps.SyncReason.Update );
				link.Sync<Country>( obj.Countries, KMaps.SyncReason.Update );
				DEBUG.Unindent(); return obj;
			};
			map.OnDelete = obj => {
				DEBUG.IndentLine( "\n-- Map<Region>.OnDelete() Instance = {0} ...", obj );
				link.Sync<Region>( obj.ChildRegions, KMaps.SyncReason.Delete );
				link.Sync<Country>( obj.Countries, KMaps.SyncReason.Delete );
				obj = map.BaseOnDelete( obj );
				DEBUG.Unindent(); return obj;
			};

			map.Validate(); return map;
		}