Ejemplo n.º 1
0
        /// <summary>
        /// Adds a new child reference.
        /// <para>WARNING: Make sure to save the parent and child Entities before calling this method.</para>
        /// </summary>
        /// <param name="child">The child IEntity to add.</param>
        /// <param name="session">An optional session if using within a transaction</param>
        public async Task AddAsync(TChild child, IClientSessionHandle session = null)
        {
            parent.ThrowIfUnsaved();
            child.ThrowIfUnsaved();

            JoinRecord join = null;

            if (inverse)
            {
                join = await(session == null ?
                             JoinCollection.Find(r => r.ChildID == parent.ID && r.ParentID == child.ID).SingleOrDefaultAsync() :
                             JoinCollection.Find(session, r => r.ChildID == parent.ID && r.ParentID == child.ID).SingleOrDefaultAsync());

                if (join == null)
                {
                    join = new JoinRecord()
                    {
                        ID         = ObjectId.GenerateNewId().ToString(),
                        ModifiedOn = DateTime.UtcNow,
                        ParentID   = child.ID,
                        ChildID    = parent.ID,
                    };
                }
                else
                {
                    return;
                }
            }
            else
            {
                join = await(session == null ?
                             JoinCollection.Find(r => r.ParentID == parent.ID && r.ChildID == child.ID).SingleOrDefaultAsync() :
                             JoinCollection.Find(session, r => r.ParentID == parent.ID && r.ChildID == child.ID).SingleOrDefaultAsync());

                if (join == null)
                {
                    join = new JoinRecord()
                    {
                        ID         = ObjectId.GenerateNewId().ToString(),
                        ModifiedOn = DateTime.UtcNow,
                        ParentID   = parent.ID,
                        ChildID    = child.ID,
                    };
                }
                else
                {
                    return;
                }
            }

            await(session == null
                   ? JoinCollection.InsertOneAsync(join)
                   : JoinCollection.InsertOneAsync(session, join));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a new child reference.
        /// <para>WARNING: Make sure to save the parent and child Entities before calling this method.</para>
        /// </summary>
        /// <param name="child">The child IEntity to add.</param>
        /// <param name="session">An optional session if using within a transaction</param>
        public async Task AddAsync(TChild child, IClientSessionHandle session = null)
        {
            parent.ThrowIfUnsaved();
            child.ThrowIfUnsaved();

            JoinRecord join = null;

            if (inverse)
            {
                join = await JoinQueryable()
                       .SingleOrDefaultAsync(r =>
                                             r.ChildID.Equals(parent.ID) &&
                                             r.ParentID.Equals(child.ID));

                if (join == null)
                {
                    join = new JoinRecord()
                    {
                        ID         = ObjectId.GenerateNewId().ToString(),
                        ModifiedOn = DateTime.UtcNow,
                        ParentID   = child.ID,
                        ChildID    = parent.ID,
                    };
                }
            }
            else
            {
                join = await JoinQueryable()
                       .SingleOrDefaultAsync(r =>
                                             r.ParentID.Equals(parent.ID) &&
                                             r.ChildID.Equals(child.ID));

                if (join == null)
                {
                    join = new JoinRecord()
                    {
                        ID         = ObjectId.GenerateNewId().ToString(),
                        ModifiedOn = DateTime.UtcNow,
                        ParentID   = parent.ID,
                        ChildID    = child.ID,
                    };
                }
            }

            await(session == null
                   ? JoinCollection.ReplaceOneAsync(x => x.ID.Equals(join.ID), join, new UpdateOptions()
            {
                IsUpsert = true
            })
                   : JoinCollection.ReplaceOneAsync(session, x => x.ID.Equals(join.ID), join, new UpdateOptions()
            {
                IsUpsert = true
            }));
        }