CSLA .NET

From Rockford Lhotka's Expert C# 2008 and VB 2008 Business Objects books

Problems with CodeSmith C# Template

rated by 0 users
This post has 6 Replies | 1 Follower

Not Ranked
Posts 6
Rizshe Posted: Wed, Jan 14 2009 4:20 AM
Hi,
I'm working with CSLA2.0 for sometime.
In a recent project we tried to use CodeSmith to generate C# templates and I'm having problems.
Maybe its my understanding OR my lack of knowledge, either way I need some serious help.

This is the actual code which I wrote a while back.

namespace BDD.MyImpact.Library.BO
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using BDD.MyImpact.Library.Data;

///
/// Readonlylist of a Person Publications
///
[Serializable]
public class PersonPublicationList : ReadOnlyList
{
String _user = String.Empty;
public String User
{
get
{
return this._user;
}
}

#region Factory Methods
private PersonPublicationList()
{

}

public static PersonPublicationList GetPublicationList(String user)
{
return DataPortal.Fetch(new Criteria(user));
}
#endregion

#region Criteria
[Serializable()]
private class Criteria
{
private String _user;

public String User
{
get
{
return this._user;
}
}

public Criteria(String login)
{
this._user = login;
}

}

#endregion

#region Data Access

private void DataPortal_Fetch(Criteria criteria)
{
using (SqlConnection cn = new SqlConnection(Database.MyImpactConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "miapp.pc_get_PersonPublicationList";
cm.Parameters.AddWithValue("@user", criteria.User);
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
IsReadOnly = false;
while (dr.Read())
{
PublicationInfo info = new PublicationInfo(dr.GetInt32("pub_id"),
dr.GetString("Title").ToString(),
dr.GetInt16("Year"),
dr.GetString("web_html"),
dr.GetString("journal"));
this.Add(info);

}
IsReadOnly = true;
}
}
}

}
#endregion
}
}
---------------------------------------
I already have a child object for this class which is already in use by another parent class that is why I don't want to generate the child.

---------------------------------------------
I tried to do SplitPartial but disregard it after I find it a bit rigid in terms of modification as I regarded the generated classes not to be touched and user classes are the ones to be modifiable.
I then sort of go towards the SplitBase as I can easily override the methods but here again I'm having problems.
Here is the code the XML template above generated.

using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using BDD.MyImpact.Library.Data;

namespace BDD.MyImpact.Library.BO
{
[Serializable()]
public abstract class PersonPublicationListBase : Csla.ReadOnlyListBase
where T : PersonPublicationListBase
where C : PublicationInfoBase
{

#region Factory Methods
public static T GetPersonPublicationList()
{
return DataPortal.Fetch(new FilterCriteria());
}
#endregion //Factory Methods

#region Data Access

#region Filter Criteria
[Serializable()]
protected class FilterCriteria : Csla.CriteriaBase
{

public FilterCriteria() : base(typeof(T))
{

}
}
#endregion //Filter Criteria

#region Data Access - Fetch
[Transactional(TransactionalTypes.TransactionScope)]
private void DataPortal_Fetch(FilterCriteria criteria)
{
RaiseListChangedEvents = false;
IsReadOnly = false;

using (SqlConnection cn = new SqlConnection(Database.MyImpactConnection))
{
cn.Open();

ExecuteFetch(cn, criteria);
}//using

IsReadOnly = true;
RaiseListChangedEvents = true;
}

protected virtual void ExecuteFetch(SqlConnection cn, FilterCriteria criteria)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "pc_get_PersonPublicationList";


using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
while (dr.Read())
this.Add(PublicationInfoBase.GetPublicationInfo(dr));
}
}//using
}
#endregion //Data Access - Fetch
#endregion //Data Access
}
}

------------------------------------
Now If you take a look at the code which I wrote and the code which is generated there are differences and I couldn't find a way to sort of get rid of those differences which is essential to let me work?
Any ideas?

Thanks
Not Ranked
Posts 6
Rizshe replied on Fri, Jan 16 2009 9:53 AM
Isn't there anyone who explain as all I need is an explanation :)

Regards,
Rizwan
Top 75 Contributor
Posts 78
Hello,

To make the changes in the generated code you would need to open up the CodeSmith Template in CodeSmith Studio or notepad and make the changes and regenerate. For the current CodeSmith CSLA templates, I would log all bugs on the CSLA CodePlex Site.

Just a heads up that we are currently creating new CSLA 3.6.x templates for CodeSmith. If you have any suggestions please let me know.

Thanks
-Blake Niemyjski

 

Thanks

-Blake Niemyjski (Author of the CodeSmith CSLA templates).

 

Not Ranked
Posts 9
Gort replied on Thu, Feb 25 2010 9:17 AM

Blake-

I have grabbed your templates from the nightly build and am attempting to use them.  What I am not seeing is the child properties being created for an object.  For example, I have a 'Patient' table and am creating a ReadOnlyRoot for it.  The Patient table has a couple of tables that have PatientId as their foreign key and are set up to be 1 to many with Patient.  So, a Patient can have several addresses, i.e. PatientAddress.  But, the PatientAddress property is not getting created in my PatientInfo class.  Should it?

Tks.

Top 75 Contributor
Posts 78

Hello,

Currently read only stereo types do not have any child properties generated. I wasn't sure if this was standard practice or not. My thoughts were that read only stereo types were usually used one deep. What are your thoughts on this?

Thanks

-Blake Niemyjski

 

Thanks

-Blake Niemyjski (Author of the CodeSmith CSLA templates).

 

Top 500 Contributor
Posts 31

Rizshe,

I'm using the latest nightly build of Codemiths CSLA templates and have this working.

If you are using the Entities.cst and the resulting Entity.csp file in Visual Studio you need to do the following.

  1. Right click on the Entity.csp file in Visual Studio.
  2. Select Manage menu option.
  3. Goto 1.Data Source
  4. Edit the "IgnoreExpression" list.
  5. Add all the related tables you want "ignored" or skipped when generating the templates. Codesmiths templates will then not generate the properties relationships  for those tables you defined in IgnoreExpression.

I've found I create a series of entries in the Entity.csp Codesmith project file pointing to the Entity.cst template and use these options so codesmith generates my CSLA business objects to suit my requirements.  Often the Codesmith templates are too aggressive creating the relationships, especially in highly normalized databases (which isn't codesmiths templates fault, more the database relationships your working with).

Jamie Clayton

Senior Application Developer

Jenasys Design

Top 500 Contributor
Posts 31

Gort,

I'm working with the nightly builds of Codesmiths CSLA.  I must admit to have only generated one readonly list at this stage and had to do some manual code changes to the generated output to make it compile.

When I get to migrating my existing Readonly CSLA objects to the new Codesmith templates I'll report any errors to http://code.google.com/p/codesmith/issues/list. Blake and Codesmith have been outstanding at template changes to fix issues identified.

From a design perspective I've only ever design parent readonly lists and readonly child items, so I can populate combo boxes or search lists.  I'm assuming your suggesting that the Codesmith templates should also generate Grandchildren + any other database relationships when you create a readonly stereotype via codesmiths templates. Is that what you are suggesting?

Jamie Clayton

Senior Application Developer

Jenasys Design

Page 1 of 1 (7 items) | RSS

Please contact Magenic for your .NET consulting
and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems