Exemplo n.º 1
0
        /// <summary>
        ///     Creates a relation between two tables.
        /// </summary>
        /// <typeparam name="TPrimary">
        ///     The type of the entities of the primary table.
        /// </typeparam>
        /// <typeparam name="TPrimaryKey">
        ///     The type of the primary key of the entities of the primary table.
        /// </typeparam>
        /// <typeparam name="TForeign">
        ///     The type of the entities of the foreign table.
        /// </typeparam>
        /// <typeparam name="TForeignKey">
        ///     Type type of the foreign key of the foreign table.
        /// </typeparam>
        /// <param name="primaryIndex">
        ///     An IIndex that specifies the primary key.
        /// </param>
        /// <param name="foreignIndex">
        ///     An IIndex that specifies the foreign key.
        /// </param>
        /// <param name="convertForeignToPrimary">
        ///     A function to convert a foreign key to the corresponding primary key.
        /// </param>
        /// <param name="convertPrimaryToForeign">
        ///     A function to convert a primary key to the corresponding foreign key.
        /// </param>
        /// <returns>
        ///     The relation.
        /// </returns>
        public Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey> CreateRelation <TPrimary, TPrimaryKey, TForeign, TForeignKey>(
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            Func <TForeignKey, TPrimaryKey> convertForeignToPrimary,
            Func <TPrimaryKey, TForeignKey> convertPrimaryToForeign,
            RelationOptions relationOptions
            )
            where TPrimary : class
            where TForeign : class
        {
            if (primaryIndex == null)
            {
                throw new ArgumentNullException("primaryIndex");
            }

            if (foreignIndex == null)
            {
                throw new ArgumentNullException("foreignIndex");
            }

            if (convertForeignToPrimary == null)
            {
                throw new ArgumentNullException("convertForeignToPrimary");
            }

            if (convertPrimaryToForeign == null)
            {
                throw new ArgumentNullException("convertPrimaryToForeign");
            }

            if (relationOptions == null)
            {
                // Use default relation options
                relationOptions = new RelationOptions();
            }

            var result = new Relation <TPrimary, TPrimaryKey, TForeign, TForeignKey>(
                primaryIndex,
                foreignIndex,
                convertForeignToPrimary,
                convertPrimaryToForeign,
                relationOptions);

            IRelationInternal relation = result;

            relation.ValidateAll();

            this.relationMapping[relation.PrimaryTable].Referring.Add(relation);
            this.relationMapping[relation.ForeignTable].Referred.Add(relation);
            this.relations.Add(relation);

            return(result);
        }
Exemplo n.º 2
0
        internal Relation(
            IUniqueIndex <TPrimary, TPrimaryKey> primaryIndex,
            IIndex <TForeign, TForeignKey> foreignIndex,
            Func <TForeignKey, TPrimaryKey> foreignToPrimary,
            Func <TPrimaryKey, TForeignKey> primaryToForeign,
            RelationOptions options)
        {
            this.isEnabled = true;

            this.primaryIndex = primaryIndex;
            this.foreignIndex = foreignIndex;

            this.convertForeignToPrimary = foreignToPrimary;
            this.convertPrimaryToForeign = primaryToForeign;

            this.options = options;
        }
Exemplo n.º 3
0
        public IRelation AddMemberGroupRelation(
            bool createMultiField = false, 
            bool useExpressionFactory = false,
            bool useTuple = false,
            bool cascadedDeletion = false)
        {
            RelationOptions options = new RelationOptions(
                cascadedDeletion: cascadedDeletion);

            if (createMultiField)
            {
                if (useTuple)
                {
                    var uniqueIndex = this.groups.CreateUniqueIndex(
                        new RedBlackTreeIndexFactory(), 
                        g => new Tuple<int, int>(g.Id, g.Id2));

                    var foreignIndex = this.members.CreateIndex(
                        new RedBlackTreeIndexFactory(),
                        m => new Tuple<int?, int>(m.GroupId, m.GroupId2));

                    if (useExpressionFactory)
                    {
                        return this.Tables.CreateRelation(
                            uniqueIndex, 
                            foreignIndex,
                            g => g.Id, m => m.GroupId,
                            g => g.Id2, m => m.GroupId2,
                            options);
                    }
                    else
                    {
                        return this.Tables.CreateRelation(
                            uniqueIndex, 
                            foreignIndex,
                            x => new Tuple<int, int>(x.Item1.Value, x.Item2),
                            x => new Tuple<int?, int>((int?)x.Item1, x.Item2),
                            options);
                    }
                }
                else
                {
                    var uniqueIndex = this.groups.CreateUniqueIndex(
                        new RedBlackTreeIndexFactory(), 
                        g => new { g.Id, g.Id2 });

                    var foreignIndex = this.members.CreateIndex(
                        new RedBlackTreeIndexFactory(), 
                        m => new { m.GroupId, m.GroupId2 });

                    if (useExpressionFactory)
                    {
                        return this.Tables.CreateRelation(
                            uniqueIndex, 
                            foreignIndex,
                            g => g.Id, m => m.GroupId,
                            g => g.Id2, m => m.GroupId2,
                            options);
                    }
                    else
                    {
                        return this.Tables.CreateRelation(
                            uniqueIndex, 
                            foreignIndex,
                            x => new { Id = x.GroupId.Value, Id2 = x.GroupId2 },
                            x => new { GroupId = (int?)x.Id, GroupId2 = x.Id2 }, 
                            options);
                    }
                }
            }
            else
            {
                var foreignIndex = this.members.CreateIndex(
                    new RedBlackTreeIndexFactory(),
                    m => m.GroupId);

                if (useExpressionFactory)
                {
                    return this.Tables.CreateRelation(
                        this.groups.PrimaryKeyIndex, 
                        foreignIndex, 
                        options,
                        new RelationConstraint<Group, Member, int?>(g => g.Id, m => m.GroupId));
                }
                else
                {
                    return this.Tables.CreateRelation(
                        this.groups.PrimaryKeyIndex,
                        foreignIndex,
                        x => x.Value,
                        x => x, 
                        options);
                }
            }

        }