Beispiel #1
0
        public TY Select <T, TY>(params WhereCondition <TY>[] conditions) where TY : Dwarf <TY>, new()
        {
            if (!typeof(TY).Implements <ICompositeId>())
            {
                throw new InvalidOperationException("Use Select for non-CompositeId types");
            }

            var command = new QueryBuilder()
                          .Select <TY>()
                          .From <TY>()
                          .Where(conditions).ToQuery();

            var uniqueKey = DwarfHelper.GetUniqueKeyForCompositeId(conditions);

            TY result = null;

            if (!typeof(TY).Implements <ICacheless>() && CacheManager.TryGetCache(uniqueKey, out result))
            {
                return(result);
            }

            var sdr = (SqlDataReader)ExecuteReader <T, TY>(command);

            try
            {
                result = sdr.Read() ? TupleToObject <TY>(sdr) : null;
            }
            catch (Exception e)
            {
                DwarfContext <T> .GetConfiguration().ErrorLogService.Logg(e);
            }
            finally
            {
                sdr.Close();
            }

            if (typeof(TY).Implements <ICacheless>())
            {
                return(result);
            }

            return(result != null
                       ? CacheManager.SetCache(DwarfHelper.GetUniqueKeyForCompositeId(result), result)
                       : CacheManager.SetCache(DwarfHelper.GetUniqueKeyForCompositeId(conditions), (TY)null));
        }