Beispiel #1
0
        /// <summary>
        /// Get an IAggregateFluent of parents matching a supplied IAggregateFluent of children for this relationship.
        /// </summary>
        /// <typeparam name="TParent">The type of the parent Entity</typeparam>
        /// <param name="children">An IAggregateFluent of children</param>
        public IAggregateFluent <TParent> ParentsFluent <TParent>(IAggregateFluent <TChild> children) where TParent : Entity
        {
            if (typeof(TParent) == typeof(TChild))
            {
                throw new InvalidOperationException("Both parent and child types cannot be the same");
            }

            if (inverse)
            {
                return(children
                       .Lookup <TChild, JoinRecord, Joined <JoinRecord> >(
                           JoinCollection,
                           c => c.ID,
                           r => r.ParentID,
                           j => j.Results)
                       .ReplaceRoot(j => j.Results[0])
                       .Lookup <JoinRecord, TParent, Joined <TParent> >(
                           DB.Collection <TParent>(),
                           r => r.ChildID,
                           p => p.ID,
                           j => j.Results)
                       .ReplaceRoot(j => j.Results[0])
                       .Distinct());
            }
            else
            {
                return(children
                       .Lookup <TChild, JoinRecord, Joined <JoinRecord> >(
                           JoinCollection,
                           c => c.ID,
                           r => r.ChildID,
                           j => j.Results)
                       .ReplaceRoot(j => j.Results[0])
                       .Lookup <JoinRecord, TParent, Joined <TParent> >(
                           DB.Collection <TParent>(),
                           r => r.ParentID,
                           p => p.ID,
                           j => j.Results)
                       .ReplaceRoot(j => j.Results[0])
                       .Distinct());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Get an IQueryable of parents matching a supplied IQueryable of children for this relationship.
        /// </summary>
        /// <typeparam name="TParent">The type of the parent Entity</typeparam>
        /// <param name="children">An IQueryable of children</param>
        /// <param name="options">An optional AggregateOptions object</param>
        public IMongoQueryable <TParent> ParentsQueryable <TParent>(IMongoQueryable <TChild> children, AggregateOptions options = null) where TParent : Entity
        {
            if (typeof(TParent) == typeof(TChild))
            {
                throw new InvalidOperationException("Both parent and child types cannot be the same");
            }

            if (inverse)
            {
                return(children
                       .Join(
                           JoinQueryable(options),
                           c => c.ID,
                           j => j.ParentID,
                           (c, j) => j)
                       .Join(
                           DB.Collection <TParent>(),
                           j => j.ChildID,
                           p => p.ID,
                           (j, p) => p)
                       .Distinct());
            }
            else
            {
                return(children
                       .Join(
                           JoinQueryable(options),
                           c => c.ID,
                           j => j.ChildID,
                           (c, j) => j)
                       .Join(
                           DB.Collection <TParent>(),
                           j => j.ParentID,
                           p => p.ID,
                           (j, p) => p)
                       .Distinct());
            }
        }
Beispiel #3
0
        /// <summary>
        /// Run the update command in MongoDB.
        /// </summary>
        /// <param name="cancellation">An optional cancellation token</param>
        public async Task <UpdateResult> ExecuteAsync(CancellationToken cancellation = default)
        {
            if (models.Count > 0)
            {
                var bulkWriteResult = await(
                    session == null
                    ? DB.Collection <T>().BulkWriteAsync(models, null, cancellation)
                    : DB.Collection <T>().BulkWriteAsync(session, models, null, cancellation)
                    ).ConfigureAwait(false);

                models.Clear();

                return(new UpdateResult.Acknowledged(bulkWriteResult.MatchedCount, bulkWriteResult.ModifiedCount, null));
            }
            else
            {
                if (filter == Builders <T> .Filter.Empty)
                {
                    throw new ArgumentException("Please use Match() method first!");
                }
                if (defs.Count == 0)
                {
                    throw new ArgumentException("Please use Modify() method first!");
                }
                if (stages.Count > 0)
                {
                    throw new ArgumentException("Regular updates and Pipeline updates cannot be used together!");
                }
                if (ShouldSetModDate())
                {
                    Modify(b => b.CurrentDate(Cache <T> .ModifiedOnPropName));
                }

                return(await UpdateAsync(filter, Builders <T> .Update.Combine(defs), options, session, cancellation).ConfigureAwait(false));
            }
        }
 /// <summary>
 /// Gets the IMongoCollection for a given IEntity type.
 /// <para>TIP: Try never to use this unless really neccessary.</para>
 /// </summary>
 /// <typeparam name="T">Any class that implements IEntity</typeparam>
 public static IMongoCollection <T> Collection <T>(this T _) where T : IEntity => DB.Collection <T>();
 /// <summary>
 /// Gets the IMongoCollection for a given IEntity type.
 /// <para>TIP: Try never to use this unless really neccessary.</para>
 /// </summary>
 /// <typeparam name="T">Any class that implements IEntity</typeparam>
 public static IMongoCollection <T> Collection <T>(this T entity) where T : IEntity
 {
     return(DB.Collection <T>(entity.Database()));
 }
 /// <summary>
 /// An IQueryable collection of Entities.
 /// </summary>
 public static IMongoQueryable <T> Collection <T>(this T entity) where T : Entity
 {
     return(DB.Collection <T>());
 }
Beispiel #7
0
 private Task <UpdateResult> UpdateAsync(FilterDefinition <T> filter, UpdateDefinition <T> definition, UpdateOptions options, IClientSessionHandle session = null, CancellationToken cancellation = default)
 {
     return(session == null
            ? DB.Collection <T>().UpdateManyAsync(filter, definition, options, cancellation)
            : DB.Collection <T>().UpdateManyAsync(session, filter, definition, options, cancellation));
 }
 /// <summary>
 /// Gets the IMongoCollection for a given IEntity type.
 /// <para>TIP: Try never to use this unless really neccessary.</para>
 /// </summary>
 /// <typeparam name="T">Any class that implements IEntity</typeparam>
 public static IMongoCollection <T> Collection <T>(this T _) where T : IEntity
 {
     return(DB.Collection <T>());
 }
Beispiel #9
0
 /// <summary>
 /// Fetches the actual entity this reference represents from the database.
 /// </summary>
 /// <returns>A Task containing the actual entity</returns>
 async public Task <T> ToEntityAsync()
 {
     return(await DB.Collection <T>().SingleOrDefaultAsync(e => e.ID.Equals(ID)));
 }
Beispiel #10
0
 private Task CreateAsync(CreateIndexModel <T> model, CancellationToken cancellation = default)
 {
     return(DB.Collection <T>().Indexes.CreateOneAsync(model, cancellationToken: cancellation));
 }
Beispiel #11
0
 /// <summary>
 /// Drops all indexes for this entity type
 /// </summary>
 /// <param name="cancellation">An optional cancellation token</param>
 public async Task DropAllAsync(CancellationToken cancellation = default)
 {
     await DB.Collection <T>().Indexes.DropAllAsync(cancellation).ConfigureAwait(false);
 }
Beispiel #12
0
 /// <summary>
 /// Drops an index by name for this entity type
 /// </summary>
 /// <param name="name">The name of the index to drop</param>
 /// <param name="cancellation">An optional cancellation token</param>
 public async Task DropAsync(string name, CancellationToken cancellation = default)
 {
     await DB.Collection <T>().Indexes.DropOneAsync(name, cancellation).ConfigureAwait(false);
 }