Пример #1
0
 /// <summary>
 /// Initializes a new instance of IdentityMap.
 /// </summary>
 /// <param name="owner"><see cref="T:FieldMap"/> this identity belongs to.</param>
 /// <param name="format"><see cref="T:IdentityFormat"/> of the identity.</param>
 /// <param name="increment"><see cref="T:IdentityIncrement"/> of the identity.</param>
 public IdentityMap( FieldMap owner, IdentityFormat format, IdentityIncrement increment )
 {
     this.Owner = owner;
     this.Format = format;
     this.Increment = increment;
     if ( this.Format == IdentityFormat.Guid ) { this.UnsavedValue = new Guid(); }
     else if ( this.Format == IdentityFormat.Integer ) { this.UnsavedValue = 0; }
     else if ( this.Format == IdentityFormat.String ) { this.UnsavedValue = String.Empty; }
 }
        private void HandleField( PropertyInfo f, IFieldAttribute a, Type t )
        {
            if( a is PersistentFieldAttribute )
                                                {
                                                                PersistentFieldAttribute attribute = a as PersistentFieldAttribute;
                                                                string tablename = attribute.Name;

                                                                this.Context.AddChild( new PersistentFieldMap()
                                                                {
                                                                                Field = f,
                                                                                ChildReturnType = attribute.ChildReturnType ?? f.PropertyType,
                                                                                Cascade = attribute.Cascade,
                                                                                Lazy = ( attribute.Loading == Loading.Active ? false : true ),
                                                                                Linked = attribute.Linked
                                                                }
                                                                .WithBehavior( attribute.Behavior )
                                                                .WithLinkedTable( attribute.LinkName )
                                                                .WithParentKeyColumn( attribute.LinkLeft )
                                                                .WithChildKeyColumn( attribute.LinkRight )
                                                                .WithTable( tablename ) );
                                                }
                                                else if( a is FieldAttribute )
                                                {
                                                                FieldAttribute attribute = a as FieldAttribute;

                                                                FieldMap fm = new FieldMap()
                                                                {
                                                                                Field = f,
                                                                                Behavior = attribute.Behavior,
                                                                                Cascade = attribute.Cascade,
                                                                                DefaultValue = attribute.DefaultValue,
                                                                                MappingType = attribute.MappingType,
                                                                                Name = attribute.Name,
                                                                                Parent = attribute.Parent,
                                                                                Static = attribute.IsStatic,
                                                                                AllowNull = attribute.AllowNull
                                                                };

                                                                switch( attribute.MappingType )
                                                                {
                                                                                case FieldMappingType.ForeignKey: { this.HandleForeignKey( fm ); break; }
                                                                                case FieldMappingType.ManagedPrimaryKey:
                                                                                case FieldMappingType.PrimaryKey: { this.Context.SetPrimaryKey( fm, ( ( PrimaryKeyAttribute )attribute ).UnsavedValue ); break; }
                                                                                case FieldMappingType.PrimaryForeignKey: { this.HandlePrimaryForeignKey( fm ); break; }
                                                                                case FieldMappingType.Reference: { this.Context.AddReferencedField( fm ); break; }
                                                                                case FieldMappingType.Value: { this.Context.AddField( fm ); break; }
                                                                }

                                                                EncryptedAttribute encrypted = Utility.GetCustomFieldAttribute<EncryptedAttribute>( f );
                                                                if( encrypted.NotNull() ) fm.WithEncryption( encrypted.PrivateKey, encrypted.Algorithm );

                                                }
        }
 private void HandlePrimaryForeignKey( FieldMap mapping )
 {
     this.Context.SetPrimaryKey( mapping );
                                         this.Context.AddForeignKey( mapping );
                                         this.Context.AddField( mapping );
 }
 private void HandleForeignKey( FieldMap mapping )
 {
     if( this.CanMapByType( mapping.FieldReturnType ) || mapping.FieldReturnType.IsIdentity() )
                                                         this.Context.AddForeignKey( mapping );
                                         else throw new MappingException( String.Format( System.Globalization.CultureInfo.InvariantCulture,
                                             @"Unable to map {0} as a foreign key in type {1}.  Type is either not maked as Persistent or the return type of the member is not a compatible identity type (Int, Guid, String).", mapping.Name, this.Context.Type ) );
 }
Пример #5
0
        /// <summary>
        /// Sets this TypeMap's <see cref="P:PrimaryKey"/>.
        /// </summary>
        /// <param name="fm"><see cref="T:FieldMap"/> that represents the primary key.</param>
        /// <param name="unsavedValue">Value for Persist to use as a compare value to determine if the primary key is new or already set.</param>
        /// <returns><see cref="T:IdentityMap"/> for fluent coding.</returns>
        public IdentityMap SetPrimaryKey( FieldMap fm, object unsavedValue )
        {
            fm.Map = this;
            IdentityIncrement increment = ( fm.MappingType == FieldMappingType.ManagedPrimaryKey
                ? IdentityIncrement.Manual
                : IdentityIncrement.Auto );

            fm.Identity = new IdentityMap( fm, Utility.GetIdentityFormat( fm.FieldReturnType ), increment );
            if ( !unsavedValue.Null() ) { fm.Identity.UnsavedValue = unsavedValue; }

            this.m_primarykey = fm;

            return ( this.PrimaryKey.Identity );
        }
Пример #6
0
 /// <summary>
 /// Sets this TypeMap's <see cref="P:PrimaryKey"/>.
 /// </summary>
 /// <param name="fm"><see cref="T:FieldMap"/> that represents the primary key.</param>
 /// <returns><see cref="T:IdentityMap"/> for fluent coding.</returns>
 public IdentityMap SetPrimaryKey( FieldMap fm )
 {
     return ( this.SetPrimaryKey( fm, null ) );
 }
Пример #7
0
 /// <summary>
 /// Adds a reference field to the TypeMap.
 /// </summary>
 /// <param name="fm"><see cref="T:FieldMap"/> to add.</param>
 public void AddReferencedField( FieldMap fm )
 {
     fm.Map = this; this.m_referenced.Add( fm );
 }
Пример #8
0
 /// <summary>
 /// Adds a foreign key to the TypeMap.
 /// </summary>
 /// <param name="fm"><see cref="T:FieldMap"/> to add.</param>
 public void AddForeignKey( FieldMap fm )
 {
     fm.Map = this; this.m_foreignkeys.Add( fm );
 }
Пример #9
0
 /// <summary>
 /// Adds a field to the TypeMap.
 /// </summary>
 /// <param name="fm"><see cref="T:FieldMap"/> to add.</param>
 public void AddField( FieldMap fm )
 {
     fm.Map = this; this.m_fields.Add( fm );
 }