Specifying criteria when using codesmith?

Specifying criteria when using codesmith?

Old forum URL: forums.lhotka.net/forums/t/5612.aspx


reagan123 posted on Thursday, October 16, 2008

Hello,
I've finally had a few minutes to mess around with some of the CSLA templates for CodeSmith.  I have run into an issue that I was hoping someone could help me with.

Is there a way to specify the criteria used for our fetch for classes generated from  ReadOnlyRootList.cst.  It looks like the criteria is just an empty class so the fetch will get all of the records.  I'd like to be able to fetch a list based on a guid and also fetch one based on a string.

Hopefully i'm just overlooking something.

Thanks as usual.

I'm using the CSLAcontrib-41428 templates
VB
CSLA 2.0

reagan123 replied on Thursday, October 16, 2008

Here is the generated code so far....

=================================================
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data

Namespace BusinessObjects.Person

    <Serializable()> _
    Public Class PersonInfoList
        Inherits  Csla.ReadOnlyListBase(Of PersonInfoList, PersonInfo)

#Region " Authorization Rules "

        Public Shared Function CanGetObject() As Boolean
            'TODO: Define CanGetObject permission in PersonInfoList
            Return True
            'If Csla.ApplicationContext.User.IsInRole("PersonInfoListViewGroup") Then
            '    Return True
            'End If
            'Return False
        End Function

#End Region ' Authorization Rules

#Region " Factory Methods "

        Private Sub New()
            'require use of factory method
        End Sub

        Public Shared Function GetPersonInfoList() As PersonInfoList
            If Not CanGetObject() Then
                throw new System.Security.SecurityException("User not authorized to view a PersonInfoList")
            End If
            Return DataPortal.Fetch(Of PersonInfoList)(New FilterCriteria())
        End Function

#End Region ' Factory Methods

#Region " Data Access "

#Region " Filter Criteria "

        <Serializable()> _
        Private Class FilterCriteria

            Public Sub New()

            End Sub
        End Class

#End Region


#Region " Data Access - Fetch "

        <Transactional(TransactionalTypes.TransactionScope)> _
        Private Overloads Sub DataPortal_Fetch(ByVal criteria As FilterCriteria)
            RaiseListChangedEvents = False
            IsReadOnly = False
            Using cn As SqlConnection = New SqlConnection(Database.UAS_WBConnection)
                cn.Open()
                ExecuteFetch(cn, criteria)
            End Using
            IsReadOnly = True
            RaiseListChangedEvents = True
        End Sub

        Private Sub ExecuteFetch(ByVal cn As SqlConnection, ByVal criteria As FilterCriteria)
            Using cm As SqlCommand = cn.CreateCommand()
                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "esp_personinfolist_select"

                Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
                    While dr.Read()
                        Me.Add(PersonInfo.GetPersonInfo(dr))
                End While
                End Using
            End Using
        End Sub

#End Region ' Data Access - Fetch
#End Region ' Data Access

    End Class

End Namespace

JoeFallon1 replied on Friday, October 17, 2008

I have not worked with the community version of the templates in a long time.

A key concept is that they are a "starting point" for you. Most developers then enhance them for their specific environment and coding styles.

So if you DB has a PK of GUID and UniqueIndex of String then you can point to a table and have the template add the extra Criteria classes and factory methods to your code. (Of course you have to write the extra template code to do this and then it will work for all tables that have those constraints.)

Joe

 

reagan123 replied on Friday, October 17, 2008

Thanks for the reply... I assumed I would have to modify the existing template to get something like this to work, but wasn't 100% how to to actually do this.  :)

Atleast I was on the right track.  I'll start messing around with the template to see if I can get it.

On another note, would anyone here be willing to share what their custom templates look like?  I'd be curious to see what types of things people are doing with their CSLA classes.

Or maybe just a description of some of the customizations you have done?

Thanks!

Copyright (c) Marimer LLC