Generating DTO's, How can I prevent all TimeStamp fields from being generated?

Posts   
 
    
KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 30-May-2007 23:19:50   

Hey All,

I'm generating DTO's and in the ToEntity() methods i want to check to see if the field is a timestamp and if so then skip the setter. How can I accomplish this?

Here's what my DTO looks like now:


#region "Conversion Methods"
        /// <summary>
        /// Creates a new entity instance and copies over the values of this DTO
        /// </summary>
        public <[CurrentEntityName]>Entity ToEntity()
        {
            return ToEntity(new <[CurrentEntityName]>Entity());
        }

        
        /// <summary>
        /// Copies over the values of this DTO into the entity passed in.
        /// </summary>
        public <[CurrentEntityName]>Entity ToEntity(<[CurrentEntityName]>Entity toFill)
        {
<[Foreach EntityField CrLf]>            
            toFill.<[EntityFieldName]> = _<[CaseCamel EntityFieldName]>;
<[NextForeach]>
                
                
<[ If IsSubType ]>          base.ToEntity(toFill);<[ EndIf]>        
            return toFill;
        }
        
        #endregion

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-May-2007 04:24:52   

First you need a method that returns the Field DBType (coz the timestamp .netType is byte[]). You need to extend the DataAccessAdapter class:

namespace yourProject.DatabaseSpecific
{
    public class MyCustomAdapter : DataAccessAdapter
    {

        public IFieldPersistenceInfo getFieldDBType(IEntityField2 field)
        {
            return base.GetFieldPersistenceInfo(field);
        }

    }
}

Then your conversion code, would be (I think):

#region "Conversion Methods"
        /// <summary>
        /// Creates a new entity instance and copies over the values of this DTO
        /// </summary>
        public <[CurrentEntityName]>Entity ToEntity()
        {
            return ToEntity(new <[CurrentEntityName]>Entity());
        }

        
        /// <summary>
        /// Copies over the values of this DTO into the entity passed in.
        /// </summary>
        public <[CurrentEntityName]>Entity ToEntity(<[CurrentEntityName]>Entity toFill)
        {
<[Foreach EntityField CrLf]>        

             //--------------------------
             IFieldPersistenceInfo fieldInfo = new MyCustomAdapter().getFieldDBType(<[EntityField]>);

            // not timestamp (timestamp is enum 19 in SQLServer)
            if (fieldInfo.SourceColumnDbType  != 19)
            {
                 toFill.<[EntityFieldName]> = _<[CaseCamel EntityFieldName]>;
            }
            //--------------------------

<[NextForeach]>
                
                
<[ If IsSubType ]>          base.ToEntity(toFill);<[ EndIf]>        
            return toFill;
        }
        
        #endregion

No tested but should works. Let me know if you make it. wink

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39776
Joined: 17-Aug-2003
# Posted on: 31-May-2007 10:43:16   

You can also embed an if statement inside the foreach loop which checks for readonly fields. A timestamp field is a readonly field, similar to computed columns:

<[If Not IsReadOnly]> // your setter code <[/EndIf]>

Although be aware that you can't set the value then as well, so you have to have a way to initial set the value (e.g. via the constructor).

Frans Bouma | Lead developer LLBLGen Pro