CodeSmith C# Templates on Codeplex

CodeSmith C# Templates on Codeplex

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


rasupit posted on Thursday, February 21, 2008

I have just posted a preview version of new CodeSmith templates to support CSLA 3.5.  Please give it a try and I'm looking forward to your feedback (good or bad).  I currently on have EditableRoot, EditableChildList, and EditableChild.  Other templates will be comming soon.

Some notable changes from the earlier version:

Support for Linq To SQL:
Generates CRUD operations for Linq to SQL.  Support optimistic concurrency through timestamp. 

Split Base no longer supported:
The new compiler trick with partial method provide much better implementation therefore I feel like the split base method is no longer needed.  I chosed to drop the Split with base class method to reduce the complexity of the templates.

N-level deep child support:
Although I've never needed, but if anybody needs it, thanks to Linq To SQL that make it possible, the templates will support it.

So please give it a try and let me know what do you think.  Hopefully I can beat Rocky with the release Big Smile [:D]
http://www.codeplex.com/CSLAcontrib/SourceControl/ListDownloadableCommits.aspx

Ricky

Patrick replied on Thursday, February 21, 2008

Very cool and good timing Ricky.

I will go and try them out Smile [:)]

Thanks,

Patrick

Ashraf Ali replied on Monday, February 25, 2008

Ricky,

you rock :)

 

Ash

GeorgeG replied on Wednesday, February 27, 2008

How do we use the new 3.5 templates? Do we need to have the commercial version of CodeSmith or we can use them with the project listed here http://www.cslagenwiki.com/~cslagenw/mediawiki001/index.php?title=Main_Page

Thanks

rasupit replied on Thursday, February 28, 2008

Like previous version.  This templates are built to work only with the free version of CodeSmith.  The too can be download http://www.codesmithtools.com/freeware.aspx.  However I have received feedback that the previous version would work just fine with the commercial version of CodeSmith.

Loved to hear feedback from others if they still work.

GeorgeG replied on Thursday, February 28, 2008

For csla version 2.xx I am using the open source project updated lastly by Andres Villanueva. I just click on CslaGenerator.exe and then point on a db, pick a table and business object type and then generated the code. I am not familiar of how to make your templates/project work with CodeSmith 2.6. I did download the free version of CodeSmith but could not figure out how to call the 3.5 templates. Can you please summarize the process of how to make them work? Probably I need to get more familiar with codesmith itself.

Thanks

rasupit replied on Thursday, February 28, 2008

I haven't have the time to update the readme file for this version.  However the readme file from previous version should probably help you as there are very little change from previous version.

You do need to be familiar on how to use CodeSmith.  Most people that use code generation usually make minor modification to suit their progamming need.

Other than the source control, you can also download the previous version here

HTH

GeorgeG replied on Friday, February 29, 2008

I am doing some progress on my end. I clicked on CodeGenTemplates\CodeSmith\CSLA35\CSharp\EditableRoot.cst
and CodeSmith launched.
Entered an ObjectName
Went to RootTable and created a new connection to our sql. From the table list I select a table.
Clicked on generate and get this error message

System.Exception: Unique Column(s) is required.
   at CodeSmith.Csla.TemplateBase.ObjectInfo.Validate()
   at CodeSmith.Csla.TemplateBase.ObjectInfo..ctor(CodeTemplate template)
   at _CodeSmith.EditableRoot_cst.__RenderMethod1(TextWriter writer, Control control)
   at CodeSmith.Engine.DelegateControl.Render(TextWriter writer)
   at CodeSmith.Engine.Control.RenderChildren(TextWriter writer)
   at CodeSmith.Engine.Control.Render(TextWriter writer)
   at CodeSmith.Engine.CodeTemplate.Render(TextWriter writer)
   at CodeSmith.Engine.CodeTemplate.RenderToString()
   at CodeSmith.Gui.CodeTemplateGenerator.f(Object A_0, EventArgs A_1)

ResultSetIndex=0.  What am I missing?

Thanks

 

JoeFallon1 replied on Friday, February 29, 2008

As I recall, a lot of template code was written assuming your tables have Primary Keys and maybe unique indexes. Does the table have a PK?

Joe

 

GeorgeG replied on Monday, March 03, 2008

You are right. I only had a unique index. After dropping the unique index and creating a primary key, the template works.

Thanks

 

Patrick replied on Saturday, May 03, 2008

rasupit:
Hopefully I can beat Rocky with the release Big Smile [:D]

Hi Ricky,
I was just wondering how you are going with the templates for CSLA.NET 3.5? Smile [:)]
I really like the ability to generate the code from XML with your templates.

Thanks for all your effort,
Patrick

rasupit replied on Tuesday, May 06, 2008

Patrick,
Thanks for the reminder.  I had been side tracked and turned my attention away from this.  I'll continue to work on the other stereotypes.  In the mean time... for anybody that have not already try with it.  please give it a try and keep the feedback coming.

Ricky

Patrick replied on Wednesday, September 17, 2008

Patrick:
I really like the ability to generate the code from XML with your templates.
Thanks for all your effort,
Patrick

Hi Ricky,

the old release of the templates for CSLA.NET 2.0 contained these files
CslaXml.cst
Csla.cst
CslaProject.xsd
MySample.xml

The CslaXml seems to work with the sample file, the Csla.cst has an error
100,26): error CS0117: 'CodeSmith.Csla.TemplateBase.CodeGenerationMethod' does not contain a definition for 'SplitBase'

It might be worth porting them to the new templates as many new users might not even know they exist and then think they have to create each class by itself... and miss out on the option to define an xml file for code generation.

Thanks,
Patrick

rasupit replied on Wednesday, September 17, 2008

Patrick:
the old release of the templates for CSLA.NET 2.0 contained these files
CslaXml.cst
Csla.cst
CslaProject.xsd
MySample.xml


I went through some major rewrite to remove the rendering dependency from CodeSmith Table Schema to CSLA metadata so that these template can support other data source such as XML.

Thanks for the reminder. I'll work on upgrading these templates.  There are others I think still left behind: EditableSwithable and NameValueList comes to mind.

Patrick replied on Wednesday, September 24, 2008

Hi Ricky,

just a quick question.

Why did you name the partials methods with the class name embedded:
        partial void OnProjectRootInfoListFetching(ref bool cancel);
        partial void OnProjectRootInfoListFetched();

Instead of just:
        partial void OnFetching(ref bool cancel);
        partial void OnFetched();

Thanks,
Patrick

zinovate replied on Wednesday, September 24, 2008

Nice call on XML Support. I'd love to Gen from a EDM or Linq2SQL model. Not a bad "CSLA" designer especially with the EF features.

pandelaras replied on Tuesday, May 06, 2008

is there any chance of similar csla 3.5 templates for VB .NET coming up?

RedmondScorpio replied on Tuesday, May 06, 2008

Hello,

First thanks for the work on the templates, they're nice!Smile [:)]

One question I had concerns the updating code in the DataPortal_Update() method.  It seems that if I have a BO created and update a property, but before I submit submit the changes, another user happens to be working with the same record and submits his changes first. Won't the first change wipe out the changes from the second? 

i.e.  if the original DB record has firstname=tom and lastname=smith and user #1 changes firstname to mike, but before he submits his change, user #2 submits lastname=jones.  After user #1 submits, I would think it should be tom jones, but it seems like it will be tom smith.  Is this something that is taken care of at a different layer or should there be an error thrown or is this just a design decision?

Thanks!

Mike

 

rasupit replied on Tuesday, May 06, 2008

pandelaras,
Like the previous version, hopefully somebody will volunteer and enhance the existing vb version.  It shouldn't be too much work.

Mike,
The answer is optimistic concurrency.   The book and sample project explain how to implement this type of concurrency to address this kind of scenario.  The template also support optimistic concurrency.

czuvich replied on Thursday, June 26, 2008

Great work on the templates; however, I'm wondering you didn't incorporate the private backing field to all properties?

rasupit replied on Friday, June 27, 2008

czuvich,
There is no specific reason why I chose to use manage field over private backing field.  Do you see an advantage one over the other?

Ricky

JoeFallon1 replied on Friday, June 27, 2008

For regular CSLA I am not sure it makes a difference. Need to read the book when it comes out though.

But for CSLA Lite - (Silverlight) - I think it works best with managed fields.

Joe

 

czuvich replied on Friday, June 27, 2008

It's not a big deal... If I am not mistaken there's a performance boost with using the backing field in regular CSLA. Thanks for the reply. Good work!

rasupit replied on Saturday, June 28, 2008

@Joe: Yes I remember Rocky mention about not having additional serialization code when using field manager.

@czvich:
To modify the template to use private backing is quite easy actually.
1. just need to add (in PropertiesMethods.inc) private field and use it in getter and setter. 
2. search and replace and LoadProperty and ReadProperty methods. I think all of them are in DataPortalxxx area

I'll leave that to anybody that want to squeeze a few extra juice out Cool [H]
Ricky


reagan123 replied on Wednesday, September 10, 2008

I'm about to make the jump to 3.5.  Great work on the C# templates.  Does anyone know if anyone did the VB ones... or atleast started them?

Thanks for any info :)

k2so replied on Wednesday, September 17, 2008

great work! let me try it out

thx

hampole replied on Wednesday, September 24, 2008

I have been using CodeSmith templates V2.0 for CSLA2.0. Now I have switched to CSLA3.5 and trying to use 3.5 templates. I successfully generated the objects for my tables but when I try to build, I get the following errors.
The type or namespace name 'Dal' could not be found.

The code snippet is as follows.
var mgr = ContextManager<Dal.RMDBDataContext>

I am not too familiar with the new version. Please let me know how to resolve this. I appreciate your help.

Ravi

ozitraveller replied on Wednesday, September 24, 2008

Hi hampole

Have a look at the PTracker example. Look in the ProjectTracker.DalLinq project in the solution.

From project.cs in Library

      using (var ctx = ContextManager<ProjectTracker.DalLinq.PTrackerDataContext>.GetManager(ProjectTracker.DalLinq.Database.PTracker))
      {
        // get project data
        var data = (from p in ctx.DataContext.Projects <--
                    where p.Id == criteria.Value
                    select p).Single();
        LoadProperty<Guid>(IdProperty, data.Id);
        LoadProperty<string>(NameProperty, data.Name);
        LoadProperty<SmartDate, System.DateTime?>(StartedProperty, data.Started);
        LoadProperty<SmartDate, System.DateTime?>(EndedProperty, data.Ended);
        LoadProperty<string>(DescriptionProperty, data.Description);
        mTimestamp = data.LastChanged.ToArray();

        // get child data
        LoadProperty<ProjectResources>(ResourcesProperty, ProjectResources.GetProjectResources(data.Assignments.ToArray()));
      }

This links to PTracker.dbml in DalLinq

database markup language (.dbml) metadata file.

 

hampole replied on Thursday, September 25, 2008

Thanks for your response. I looked at the PTracker project and still I am confused. I have all the tables and the CRUD procedures in SQL server. Do I need to create dbml file with all the meta data. Is there any tool to do that? Please help.

Thanks,
Ravi

ozitraveller replied on Thursday, September 25, 2008

Once you have your Data Sources connected, you should be able to drag everything you need from the Data Sources window.

Yes you will need to add a new dbml object to your project.

 

hampole replied on Thursday, September 25, 2008

Thanks. I have little bit progressed now. I used the Microsoft tool SqlMetal.exe to generate the dbml file. Once I added to the project, DataContext class was created. I am having some other problems. The CodeSmith generated code snippet for fetch is as follows. I don't see any method by name Single in DataContext class. Do I need to do anything to generate that? please help.

        private void DataPortal_Fetch(SingleCriteria<StateProvince, int> criteria)
        {
            using (var mgr = ContextManager<Dal.RMDBDataContext>
                        .GetManager(Database.RMDB))
            {
                var data = mgr.DataContext.RMStateProvinces
                        .Single(r => r.StateProvinceID == criteria.Value);

                StateProvinceID = data.StateProvinceID;
                StateProvinceName = data.StateProvinceName;
                StateProvinceCode = data.StateProvinceCode;
                CountryRegionID = data.CountryRegionID;
                CreatedDate = data.CreatedDate;
                UpdatedDate = data.UpdatedDate;
                CreatedUserID = data.CreatedUserID;
                UpdatedUserID = data.UpdatedUserID;
            }//using
        }

zinovate replied on Monday, September 29, 2008

There is a project type called "Linq to SQL Classes" in VS 2008.

It provides a visual designer and drag and drop support from the data sources window. That's your best path for Linq 2 SQL development.

ajj3085 replied on Tuesday, September 30, 2008

SqlMetal is perfectly valid as well, and it's use is recommended if you want a bit more control over the generated classes.

To answer the OPs question, it may be that System.Linq isn't being referenced via a using clause.

zinovate replied on Monday, September 29, 2008

Single is part of the Linq Extension methods.

Here's a good source of info on Linq.

 http://msdn.microsoft.com/en-us/netframework/aa904594.aspx 

ebaytok replied on Sunday, November 02, 2008

I haven't been able to locate the codesmith templates for net3.5.  Can you post a link? --Thanks.

jnapoli replied on Friday, December 12, 2008

I have to admit, i am new to the templates.  And I've just tried playing with the templates EditableRootList and EditableRoot for 3.5 version.  Not having any luck. 

Have you gotten any other feedback? 

 

 

 

 

 

 

 

bniemyjski replied on Tuesday, February 16, 2010

Hello,

If you are working with CSLA 3.8, we have a set of templates that really goes beyond the CSLA Contrib templates. I've tried to make the migration from CSLA Contrib to our templates as easily as possible. Please check out a nightly build, we have added support for SQL Stored procedures as well as object factory.

Thanks

-Blake Niemyjski

bniemyjski replied on Tuesday, August 03, 2010

Hello,

We recently released our CSLA v3.0.0 templates and it is easy to upgrade from the CSLA Contrib templates. They support CSLA 3.8.4,CSLA 4.0 and Silverlight 4. These are 100% current with Visual Basic and C# support!!! We are in the process of writing documentation on the templates. This blog post is current and helpful when upgrading.

Thanks

-Blake Niemyjski

Copyright (c) Marimer LLC