// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; using AutoMapper; using osu.Game.Input.Bindings; using Realms; namespace osu.Game.Database { public static class RealmObjectExtensions { private static readonly IMapper mapper = new MapperConfiguration(c => { c.ShouldMapField = fi => false; c.ShouldMapProperty = pi => pi.SetMethod != null && pi.SetMethod.IsPublic; c.CreateMap(); }).CreateMapper(); /// /// Create a detached copy of the each item in the collection. /// /// A list of managed s to detach. /// The type of object. /// A list containing non-managed copies of provided items. public static List Detach(this IEnumerable items) where T : RealmObject { var list = new List(); foreach (var obj in items) list.Add(obj.Detach()); return list; } /// /// Create a detached copy of the item. /// /// The managed to detach. /// The type of object. /// A non-managed copy of provided item. Will return the provided item if already detached. public static T Detach(this T item) where T : RealmObject { if (!item.IsManaged) return item; return mapper.Map(item); } } }