using System; using System.Collections.Generic; namespace ZeroLevel.MsSql { public static class DbMapperFactory { private static readonly Dictionary _mapperPool = new Dictionary(); private static readonly object _poolLocker = new object(); /// /// Создание маппера /// /// Тип представляющий модель данных /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember /// Маппер public static IDbMapper Create(Type entityType, bool mapOnlyMarkedMembers = false) { if (null == entityType) throw new ArgumentNullException(nameof(entityType)); lock (_poolLocker) { if (false == _mapperPool.ContainsKey(entityType)) { var gt = typeof(IDbMapper<>); var rt = gt.MakeGenericType(new Type[] { entityType }); _mapperPool.Add(entityType, new DbMapper(rt, mapOnlyMarkedMembers)); } } return _mapperPool[entityType]; } /// /// Создание маппера /// /// В случае задания в true, все поля класса считаются данными модели, в т.ч. не отвеченные аттрибутом DbMember /// Маппер public static IDbMapper Create(bool mapOnlyMarkedMembers = false) { var entityType = typeof(T); lock (_poolLocker) { if (false == _mapperPool.ContainsKey(entityType)) { _mapperPool.Add(entityType, new DbMapper(mapOnlyMarkedMembers)); } } return (IDbMapper)_mapperPool[entityType]; } } }