예제 #1
0
        internal static TResult Aggregate <T, TResult>(AggregateNames aggregateName, Expression <Func <T, TResult> > projection, Expression <Func <T, bool> > predicate = null, string connectionName = null, DbConnection connection = null, IConfiguration config = null)
            where T : class
            where TResult : struct
        {
            string providerName = null;

            if (connection == null)
            {
                providerName = DbFactory.GetProviderInvariantName(connectionName, typeof(T), config);
                connection   = DbFactory.CreateConnection(connectionName, typeof(T), config);
            }
            var sql = SqlBuilder.GetSelectAggregationStatement(aggregateName.ToString(), projection, predicate, DialectFactory.GetProvider(connection, providerName));

            return(RetrieveScalar <TResult>(sql, connection: connection, config: config));
        }
예제 #2
0
        public static AggregateName GetAggregateName(this Type aggregateType)
        {
            return(AggregateNames.GetOrAdd(
                       aggregateType,
                       t =>
            {
                if (!typeof(IAggregateRoot).GetTypeInfo().IsAssignableFrom(aggregateType) &&
                    !typeof(IIdentity).GetTypeInfo().IsAssignableFrom(aggregateType))
                {
                    throw new ArgumentException($"Type '{aggregateType.PrettyPrint()}' is not an aggregate root or identity");
                }

                return new AggregateName(
                    t.GetTypeInfo().GetCustomAttributes <AggregateNameAttribute>().SingleOrDefault()?.Name ??
                    t.Name);
            }));
        }