Default code for user code regions

Posts   
 
    
zulu
User
Posts: 50
Joined: 25-Aug-2008
# Posted on: 17-May-2009 00:18:10   

Hi, I am developing some lpt templates and I would like to provide user code regions that have default code in them.

The

DotNetTemplateEngine.GetUserCodeRegion

way of adding user code regions allows default content to be added, but it's a bit cumbersome, especially when default code contains indenting, line breaks etc.

I have developed my own code generator which uses codesmith templates and I have implemented a user code region feature like the one LLBLGen uses. My way of including user code regions in the template is as following:


' __USER_CODE_START CreateMessageForPersistenceRuleViolation
.... Default code
' __USER_CODE_END

instead of

DotNetTemplateEngine.GetUserCodeRegion("CreateMessageForPersistenceRuleViolation","'",".... Default code")

and then I use a merging strategy object to merge the new file output with the previous user code regions. It works perfectly.

Perhaps a future version of the lpt parser?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-May-2009 23:36:51   

But, if you generate a default code on USER_CODE_REGIONS, the code in there is not user-code anymore. Could you show us in which way this is helpful simple_smile ?

David Elizondo | LLBLGen Support Team
zulu
User
Posts: 50
Joined: 25-Aug-2008
# Posted on: 21-May-2009 18:08:45   

daelmo wrote:

But, if you generate a default code on USER_CODE_REGIONS, the code in there is not user-code anymore. Could you show us in which way this is helpful simple_smile ?

Well, if I generate default code in user code regions it means that the user can change that default code and keep the changes. Yes, I may accomplish the same thing by including an overridable method and the user could override it in a partial class but sometimes I don't see the necessity.

For example, my validator templates have a method CreateMessageForPersistenceRuleViolation, which contains a user code region with default code, which the user doesn't need to change, but he/she can and the default code implicity tells the user how to change it.

        Public Function CreateMessageForPersistenceRuleViolation(ByVal violatedRule As PersistenceRuleViolation, ByVal involvedEntity As Object) As String
            ' __USER_CODE_START CreateMessageForPersistenceRuleViolation
            Select Case violatedRule
                Case PersistenceRuleViolation.DuplicateKey
                    Return "Duplicate key"
                Case PersistenceRuleViolation.DeleteRelatedRecordsFirst
                    Return "The entity is used by other entities and so cannot be deleted"
                Case PersistenceRuleViolation.MissingRelatedRecords
                    Return "The entity requires other entities to be created first"
            End Select
            ' __USER_CODE_END
        End Function

Also, at times I may want to include comments in user code regions during first generation, telling the user what to put in there using commented code examples. I know, I could use other means to do this, but it's more intuitive this way I think.

Good enough reasons? :-)