zengcheng / codesmith

Automatically exported from code.google.com/p/codesmith
0 stars 0 forks source link

CSLA 3.8 Templates - Criteria #504

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When generating the criteria templates, the default constructor is assuming an 
entity object.  Should this not default to the criteria object itself instead.

For example the MaterialCriteria class has the following generated.

Public Sub New()
   MyBase.New(GetType(Mma.Business.Material))
End Sub

Instead, I think it should be:

Public Sub New()
   MyBase.New(GetType(Mma.Business.MaterialCriteria))
End Sub

Original issue reported on code.google.com by jodyches...@comcast.net on 20 Sep 2010 at 4:55

GoogleCodeExporter commented 9 years ago
Hello,

I believe that this is the correct behavior based on the following two 
documents(http://www.lhotka.net/Article.aspx?id=4be29839-f6d8-4caa-b1ae-13b85563
90dc and 
http://books.google.com/books?id=9Mbx3Svoc1EC&pg=PA444&lpg=PA444&dq=CSLA+Criteri
a+typeof&source=bl&ots=do7iUS1Xwy&sig=eB9WSkOs_D4TdaymWkUP_O4ehl0&hl=en&ei=4hqZT
NXrEtWMnQeDvMHLDw&sa=X&oi=book_result&ct=result&resnum=6&ved=0CDAQ6AEwBQ#v=onepa
ge&q=CSLA%20Criteria%20typeof&f=false). I remember researching this but can't 
remember. Could you please take a look or ask on Rocky's forums (he may be on 
vacation).

Thanks
-Blake Niemyjski

Original comment by bniemyjski on 21 Sep 2010 at 8:54

GoogleCodeExporter commented 9 years ago
In order to reuse the criteria objects for different business objects we have 
been doing the following:

<Serializable()> _
    Public Class MaterialCriteria
        Inherits Csla.CriteriaBase

        Private Shared MaterialIDProperty As PropertyInfo(Of Guid) = RegisterProperty(Of Guid)(GetType(MaterialCriteria), New PropertyInfo(Of Guid)("MaterialID"))
        Public ReadOnly Property MaterialID() As Guid
            Get
                Return ReadProperty(MaterialIDProperty)
            End Get
        End Property

        Public Sub New(ByVal type As Type, ByVal materialID As Guid)
            MyBase.New(type)
            LoadProperty(MaterialIDProperty, materialID)
        End Sub
        Public Sub New()
        End Sub
    End Class

Then within each business object we provide the type as part of the Criteria 
call, like the following:

DataPortal.Delete(Of Material)(New MaterialCriteria(GetType(Material), 
materialID))

or 
DataPortal.Delete(Of MaterialCatalog)(New 
MaterialCriteria(GetType(MaterialCatalog), materialID))

We are just look at the reusablity of these criteria objects. I know this is 
bad example since this criteria only has one parameter value and code have 
simply used the SingleCriteria class.  

Just a thought.  Let me know what you think.  Thanks.

Original comment by jodyches...@comcast.net on 23 Sep 2010 at 5:10

GoogleCodeExporter commented 9 years ago
Hello,

I could see where this could be useful if you have multiple entities from one 
table and you want to reuse. However, it would be nice not to have to call 
GetType. Do you know how this is supported in CSLA 4? Since this is supplied as 
part of the class definition.

Thanks
-Blake Niemyjski

Original comment by bniemyjski on 27 Sep 2010 at 12:10

GoogleCodeExporter commented 9 years ago
The current CodeSmith CSLA templates are generating the following for vb 4.0 
criteria objects:

Public Partial Class MaterialCriteria
        Inherits CriteriaBase(Of MaterialCriteria)
        Implements IGeneratedCriteria
....

#Region "Constructors"

        Public Sub New()

        End Sub

        Public Sub New(ByVal uMaterialID As System.Guid) 

            Me.UMaterialID = uMaterialID
        End Sub

#End Region

For the entity class, these templates are generating:  

Friend Shared Function GetByUMaterialID(ByVal uMaterialID As System.Guid) As 
Material
          Dim criteria As New MaterialCriteria()
          criteria.UMaterialID = uMaterialID

          Return DataPortal.FetchChild(Of Material)(criteria)
End Function

All of this appears to be in-line with the idea the criteria class can be 
independent from entities and thus re-usable as well.

Original comment by jodyches...@comcast.net on 30 Sep 2010 at 5:32

GoogleCodeExporter commented 9 years ago
Wondering what people think we should use for this. We had a request to move 
this to use a BusinessBase implementation. Would this work for you?

Original comment by bniemyjski on 5 Oct 2010 at 10:32

GoogleCodeExporter commented 9 years ago
Does this mean each BusinessBase would have their own nested (private) 
criteria? 

Original comment by jodyches...@comcast.net on 6 Oct 2010 at 2:16

GoogleCodeExporter commented 9 years ago
Hello,

No, the criteria class would just inherit from BusinessBase instead of 
ICriteria.

Thanks
-Blake Niemyjski

Original comment by bniemyjski on 17 Nov 2010 at 3:53

GoogleCodeExporter commented 9 years ago

Original comment by bniemyjski on 9 Mar 2012 at 1:34