CodeSmith Community
Your Code. Your Way. Faster!

CSLA 2.0 Templates for C#

Latest post 02-21-2008 8:26 AM by ender. 16 replies.
  • 04-06-2006 10:05 PM

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    CSLA 2.0 Templates for C#

    Hi,

    I have created a collection of templates for Rocky's new book Expert C# 2005 Business Objects.  If you have used my previous template collections, you'll find that some major rewrite was done on this version. The rewrite is neccessary in order to give more flexibility and to support multiple data source. These templates now support Table, View, and Xml data source.  

    Enjoy them and don't forget to read the ReadMe file before you use it. There are also some known issues you need to be aware of.

    Ricky Supit.

    Note: As of 6/1/2006, I'll no longer post any new release on this post.  Any new releases will be posted on GotDotNet at http://workspaces.gotdotnet.com/cslacstemplate

    History:

    0.9.3 5/30/06 (Beta)
    - ENH(RAS): Add hooks to support Split-Partial.
    - Fix(RAS): ReadOnlyChildList is missing IsReadOnly set to false and true in Fetch method.
    - Fix(RAS): EditableChild is missing MarkOld() in Fetch method.
    - Fix(RAS): Missing ResultSetIndex property in EditableSwitchable. (reported by Joe Egan)
    - Fix(RAS): Missing CanXXXObject authorization-rule methods in EditableSwitchable. (reported by Joe Egan)
    - Fix(RAS): Duplicate FetchObject, FetchChildren, and UpdateChildren on EditableSwitchable. (reported by Joe Egan)
    - ENH(RAS): Improve split class support, add Execute[Fetch|Insert|Update|Delete] methods.
    - Fix(TEC): Missing Convert null to string on SmartDate property set.
    - Fix(TEC): EmptyIsMin value was incorectly assigned during declaration when column start with active, start, begin, from.
    - ENH(TEC): Guid value is automaticaly generated in business object.
    0.9.2      4/30/06
    - Enh(TEC): Provide support for CommmandSchema object allowing for a stored procedure
                to be used as a data source.
    - Fix(RAS): Add override on AddNewCore (ERL, ECL) to allow adding new collection item.
    - Fix(RAS): Pass Options properties to subtemplate on Csla.cst.
    - Fix(RAS): Make MinimizeStackTraceUse defaulted to true instead of false.
    - Enh(RAS): Add ChildNameAndSuffix, ParentNameAndSuffix.
    - Enh(RAS): refactor loading ObjectInfo.
    0.9.1
    - Fix: Constructor in ECL contain pk assignment. (reported by Tom Harris)
    - Fix: Constructor exist in base class of ECL and EC.
    - Enh: 'using' on SqlCommand to ensure proper object dispose
    - New: EditableSwitchable template
    - Fix: RO generate override IsValid/IsDirty when child col obj exist. (reported by ChrisD)
    - Fix: Add criteria object in ROR (reported by ChrisD)

    0.9.0   4/6/06   By:Ricky Supit
    - Initial release (Alpha)

    Post was edited: 4/16/2006 6:27 PM ET.

    • Post Points: 105
  • 04-08-2006 8:16 AM In reply to

    • chrisdenslow
    • Not Ranked
    • Joined on 08-13-2003
    • Aberdeen , Scotland, United Kingdom
    • Posts 7
    • Points 205

    Re: CSLA 2.0 Templates for C#

     Hi Ricky,
     
    Well I have two bits of really good news for you and then one very small bit of bad
     
    A) Your templates for Codesmith function perfectly in version 3.2 of Codesmith i.e. the new one that Eric J Smith recompiled against VS 2005 - so that's brilliant.
     
    B) I just generated every type of object you have provided a template for without any errors in generation at all - even better!!
     
    Now for the exceedingly minor bit of bad news..
     
    C) When I compile the generated classes in my C# project everything complies perfectly EXCEPT the ReadOnlyBase object as it is looking for a criteria class that isn't there.  The error I get reads
     
    "Error 1 The type or namespace name 'Criteria' could not be found (are you missing a using directive or an assembly reference?) C:\CSLA20\ObjectBasicsCS\ObjectBasicsLibrary\ROPerson.cs 130 39 ObjectBasicsLibrary
    "   
    All in all I think you should feel very proud of what you have come up with here.  I'm very impressed so far and I'll let you know about any other errors I come across as I wire a UI onto the front of these objects and try to get my demonstration application for One Little Victory sorted out.
     
    Give yourself a hefty pat on the back and feel very , very smug.
     
    Cheers
    Chris  
     
    "We're only immortal for a limited time" - Neil Peart
    • Post Points: 35
  • 04-09-2006 10:06 AM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    Chris,

    Thanks for your feedback.  It's good to know that these templates also work with CodeSmith version 3.2

    C) ReadOnlyRoot template is missing criteria object.  I'll have this fixed on the next release.  In the mean time you can add this include statement at line 42 in ReadOnlyRoot.cst to make it look like this:

    <% if(objInfo.IsGeneratedClass) {%>

    <!-- #INCLUDE FILE="ObjectCriteria.inc" -->

    <!-- #INCLUDE FILE="DataPortalFetch.inc" -->

    <%}%>

    Keep them coming,

    Ricky

    • Post Points: 5
  • 04-16-2006 5:31 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    New version 0.9.1 available at the top of this thread.
    • Post Points: 35
  • 04-21-2006 10:15 AM In reply to

    • Thorsten
    • Not Ranked
    • Joined on 04-21-2006
    • Posts 1
    • Points 35

    Re: CSLA 2.0 Templates for C#

    Hi!

    First of all I've to say your templates are great.
    Thanx.

    But I discovered a problem.
    Maybe it's my fault.

    I tried to use the GenerationMethod "SplitBase".
    In some cases an exception accurs:
    -----------------------------------------------------------------------------------------------------
    Error Message:
    Test method Vera.Test.StammblattTraegerBOTest.UpdateStammblattTraegerBO threw exception:  Csla.DataPortalException: DataPortal.Fetch failed --->  Csla.Server.CallMethodException: DataPortal_Fetch method call failed --->  System.NotSupportedException: Invalid operation - fetch not allowed.

    Stack Trace:
    at Csla.Core.BusinessBase.DataPortal_Fetch(Object criteria) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\Core\BusinessBase.cs:line 874
     --- End of inner exception stack trace ---
        at Csla.Core.BusinessBase.DataPortal_Fetch(Object criteria) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\Core\BusinessBase.cs:line 874
       at Csla.MethodCaller.CallMethod(Object obj, MethodInfo info, Object[] parameters) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\MethodCaller.cs:line 56
       at Csla.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\MethodCaller.cs:line 37
       at Csla.Server.SimpleDataPortal.Fetch(Object criteria, DataPortalContext context) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\Server\SimpleDataPortal.cs:line 101
     --- End of inner exception stack trace ---
        at Csla.Core.BusinessBase.DataPortal_Fetch(Object criteria) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\Core\BusinessBase.cs:line 874
       at Csla.MethodCaller.CallMethod(Object obj, MethodInfo info, Object[] parameters) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\MethodCaller.cs:line 56
       at Csla.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\MethodCaller.cs:line 37
       at Csla.Server.SimpleDataPortal.Fetch(Object criteria, DataPortalContext context) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\Server\SimpleDataPortal.cs:line 101
       at Csla.DataPortal.Fetch(Object criteria) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\Client\DataPortal.cs:line 173
       at Csla.DataPortal.Fetch[T](Object criteria) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\csla20cs\Csla\DataPortal\Client\DataPortal.cs:line 138
       at Vera.Library.StammblattTraegerBO.GetStammblattTraegerBO(Int32 sPTID) in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\Vera\Vera.Library\StammblattTraegerBO.cs:line 166
       at Vera.Test.StammblattTraegerBOTest.UpdateStammblattTraegerBO() in C:\Dokumente und Einstellungen\thil\Eigene Dateien\Visual Studio 2005\Projects\Vera\Vera.Test\StammblattTraegerBOTest.cs:line 121
    ------------------------------------------------------------------------------------------------

    Maybe reflection is not working correctly with a dirived class.
    Do anybody know what went wrong?

    Greetings!
    Thorsten
    • Post Points: 35
  • 04-21-2006 6:01 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    Thorsten,

    I'm glad you like them.  This is actually one of known issues in the readme file.  I'm afraid split class by using base class does not work quite right yet.  You're right the reflection is not working correctly.  I have submitted this issue but don't know if Rocky will address this in the future. 

    I have tested several work around to address this problem.

    • Put all DataPortal_xxx method in the user class
    • Override DataPortal_xxx instead of creating strongly typed overload method
    • Use closed construct
    • Modify GetMethod so it will work with open construct.

    I don't like any of those and prefer the last approach which require fixing the GetMethod.  Let me know if you find that this was not actually the framework issue but my implementation of the base class was incorrect.

    Ricky

    • Post Points: 50
  • 04-30-2006 8:04 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    I have just realeased version 0.9.2.  Check it out, is available on top of this thread.

    Tom Cooley contributed by adding support to stored procedure data source. 

    I have created a workspace on gotdotnet for these templates.  http://workspaces.gotdotnet.com/cslacstemplate This workspace is open to public so everybody is welcomed to join the workspace.  If you have idea and would like to contribute, please drop me a line.

    Enjoy,

    Ricky

    • Post Points: 35
  • 05-01-2006 3:01 PM In reply to

    • TomCooley
    • Not Ranked
    • Joined on 05-01-2006
    • Posts 7
    • Points 95

    Re: CSLA 2.0 Templates for C#

    I'm not sure if this is warranted, but since using a stored procedure as the basis for generating the class is a bit of a departure from using a table, I thought I'd touch on a few usage notes.


    Unlike the RootTable or RootView approach, a single stored procedure can (and in many cases within CSLA does) return multiple result sets meaning you can use that same stored procedure to define all of the objects hydrated from that stored procedure.  And since it is often the case that get/fetch-type stored procedures do not look identical to the underlying tables, it can generate a more accurate class with less need for modification.


    Therefore, I've added two properties to each of the templates, RootCommand and ResultSetIndex. Given this approach, a single stored procedure is used to define not only the root object, but all of it's children. For EditableRoot, ReadOnlyRoot, EditableRootList & ReadOnlyRootList, the default index is zero (the first result returned). Typically you would not change this. When using this result set, it is assumed that the stored procedure's parameters provide either a unique index (for objects) or filter values (for collections) meaning there is no need to enter values in the UniqueColumnNames StringCollection.


    However, when generating the child classes, the ResultSetIndex may need to be changed to match the returned result from the stored procedure (it is set to 1 by default for child templates). Also, since the parameters used to call the stored procedure are unlikely to qualify the child objects primary key value(s), you must specify at least one UniqueColumnName in this case.


    Similarly, it is expected that the parameters for a NameValueCollection fetch stored procedure more likely indicate filter values than primary keys, you must identify which field is the UniqueColumnName for these templates.


    Other than that, generation is similar to using RootTable or RootView.


    I have to say that because Ricky did such a nice job with the templates and the design of the TemplateBase class, adding this support was really easy. He's really done a nice job with this. I had looked into adding this support to the version 1.X templates and it would have meant a great deal of ripping and tearing, but it fit very nicely in this version.

     

    Tom

    • Post Points: 5
  • 05-16-2006 10:16 AM In reply to

    • ErebusBat
    • Not Ranked
    • Joined on 05-16-2006
    • Posts 1
    • Points 35

    Re: CSLA 2.0 Templates for C#

    I don't know if I am doing something wrong or there is a bug in the template.

    I am atempting to generate based off the EditableSwitchable template and I am running into a whole bunch of errors (Template compile errors, duplicated code, etc..)

    I pulled the latest copy from source control, could that be the problem? Is there a known issue with the EditableSwitchable template? I don't mind fixing the errors, but if it is a known issue or there is a specific way to fix the templates then I would rather do that.

    Thanks,
    Andrew
    • Post Points: 35
  • 05-16-2006 7:59 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    Andrew,

    Thanks for your report.  I have a fix for this and will be available on the next release.  You currently can get this from source control on gotdotnet

    Ricky.

    • Post Points: 5
  • 06-01-2006 6:51 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    Version 0.9.3 is available for download at http://workspaces.gotdotnet.com/cslacstemplate

    • Post Points: 35
  • 06-03-2006 5:21 AM In reply to

    • cdshearer
    • Not Ranked
    • Joined on 06-03-2006
    • Auckland, New Zealand
    • Posts 1
    • Points 35

    Re: CSLA 2.0 Templates for C#

    Hi Ricky

    Great set of templates, but I'm having a little trouble with the code generated. I'm using the CslaXml template, and using the MySample.xml file, it generates the following code (which does not compile):

    Project.Generated.cs:

    //

    // Fetch

    //

    [NonSerialized()]

    private predicateEx<SqlConnection, Criteria> executeFetchDelegate = null;

    private bool onExecuteFetch(SqlConnection cn, Criteria criteria)

    {

    if (executeFetchDelegate != null)

    return executeFetchDelegate(cn, criteria);

    return true;

    }

    [NonSerialized()]

    private actionEx<SqlCommand, Criteria> executeFetchCompleteDelegate = null;

    private void onExecuteFetchComplete(SqlCommand cm, Criteria criteria)

    {

    if (executeFetchCompleteDelegate != null)

    executeFetchCompleteDelegate(cm, criteria);

    }

     

     

    There seems to be an error in the template.

    Craig

    • Post Points: 35
  • 06-05-2006 9:46 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    Craig,  Glad you like the templates

    Replace line 3 in DelegationMethod.inc with the following would fix it.

    <% if(!(objInfo.IsChild && objInfo.ParentType.Length==0)) {%>

    I have also updated the file in source control.

    Ricky.

    • Post Points: 5
  • 06-06-2006 9:46 AM In reply to

    • JLuc
    • Not Ranked
    • Joined on 06-06-2006
    • Posts 2
    • Points 40

    Re: CSLA 2.0 Templates for C#

    Hi Ricky,

    I got 2 small problems.

    One is that ReadOnlyRootList generated wants to call the ReadOnlyChild with a get method

    having a datareader parameter ex: this.Add(ContactInfo.GetContactInfo(dr));

    The get method generated in the info object wants to have an integer. Am I using the templates correctly?

    The other problem is that I can't generate code when targeting a table with a bigint primary key. Is it normal behaviour because it is not implemented?

    Here's the error:

    System.OverflowException: Value was either too large or too small for an Int32.
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
       at System.Convert.ToInt32(Object value)
       at SchemaExplorer.SqlSchemaProvider.GetTableColumns(String connectionString, TableSchema table)
       at SchemaExplorer.TableSchema.get_Columns()
       at CodeSmith.Csla.ObjectInfo.LoadProperties(TableSchema table)
       at CodeSmith.Csla.ObjectInfo.LoadFromSchema(CodeTemplate template)
       at CodeSmith.Csla.ObjectInfo..ctor(CodeTemplate template)
       at _CodeSmithTemplate.ReadOnlyRoot_cst.__RenderMethod1(TextWriter writer, Control control)
       at CodeSmith.Engine.DelegateControl.Render(TextWriter writer)
       at CodeSmith.Engine.Control.RenderChildren(TextWriter writer)
       at CodeSmith.Engine.CodeTemplate.Render(TextWriter writer)
       at CodeSmith.Engine.CodeTemplate.RenderToString()
       at CodeSmith.Gui.CodeTemplateGenerator.b(Object , EventArgs )

    Thanks,

    -JLuc

     

     

    • Post Points: 35
  • 06-06-2006 6:52 PM In reply to

    • rasupit
    • Top 100 Contributor
    • Joined on 11-17-2003
    • Posts 44
    • Points 1,035

    Re: CSLA 2.0 Templates for C#

    JLuc,

    If you generate ContactInfo using ReadOnlyChild.cst you should get GetContactInfo(SafeDataReader dr)

    What database are you using create table with big int column as primary key.  I tested using sql 2000 and sql express 2005, they both work correctly. The variable is generated as long.

    Ricky

    • Post Points: 35
Page 1 of 2 (17 items) 1 2 Next > | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems