Criteria error in template?

Feb 9, 2012 at 6:43 PM

Hi

using latest templates  and using Criteria base for Criteria the generated code does not create a public parameterless constructor (and so fails when running). The code underlined bel0w is missing.

        public GetByUserName() { }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="GetByUserName"/> class.
        /// </summary>
        /// <param name="userName">The UserName.</param>
        /// <param name="applicationName">The ApplicationName.</param>
        public GetByUserName(string userName, string applicationName)
        {
            UserName = userName;
            ApplicationName = applicationName;
        }

 Also on the Fetch for criteria... crit.userName should just be userName

  /// <summary>
        /// Loads a SchoolInfo object from the database.
        /// </summary>
        /// <param name="userName">The User Name.</param>
        /// <param name="applicationName">The Application Name.</param>
        /// <returns>A data reader to the SchoolInfo.</returns>
        public IDataReader Fetch(string userName, string applicationName)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("Abraca"))
            {
                using (var cmd = new SqlCommand("usp_SchoolByUserName_Get", ctx.Connection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserName", crit.userName).DbType = DbType.String;
                   // cmd.Parameters.AddWithValue("@ApplicationName", applicationName).DbType = DbType.String;
                    return cmd.ExecuteReader();
                }
            }
        }
Hope this helps
Richard

 

 

Coordinator
Feb 10, 2012 at 11:51 PM
Edited Feb 11, 2012 at 8:30 AM

Hi Richard,

If you take a look at ProjectTracker you will notice that CriteriaBase classes in that project don't have any constructor at all. CslaGenFork uses a constructor with parameters just because it's the way CslaGen did it and it was simpler to keep that code (if it works, no need to break it).

Take ProjectTracker's ResourceGetter

[Serializable]
public class Criteria : CriteriaBase<Criteria>
{
    public static readonly PropertyInfo<intResourceIdProperty = RegisterProperty<int>(c => c.ResourceId);
 
    public int ResourceId
    {
        get { return ReadProperty(ResourceIdProperty); }
        set { LoadProperty(ResourceIdPropertyvalue); }
    }
 
    public static readonly PropertyInfo<boolGetRolesProperty = RegisterProperty<bool>(c => c.GetRoles);
 
    public bool GetRoles
    {
        get { return ReadProperty(GetRolesProperty); }
        set { LoadProperty(GetRolesPropertyvalue); }
    }
}

This class is used like this (for example in CreateNewResource)

new Criteria { ResourceId = -1, GetRoles = !RoleList.IsCached }

Code generated by CslaGenFork would use a parametrized constructor like this 

new Criteria (-1, !RoleList.IsCached)

The difference is that CslaGenFork generated code needs to pass all the parameter in the constructor's call while code that uses no constructor at all just needs to assign the properties that aren't null.

Cheers

Tiago Freitas Leal

Coordinator
Feb 11, 2012 at 12:30 AM
zen8019 wrote:

Also on the Fetch for criteria... crit.userName should just be userName

  /// <summary>
        /// Loads a SchoolInfo object from the database.
        /// </summary>
        /// <param name="userName">The User Name.</param>
        /// <param name="applicationName">The Application Name.</param>
        /// <returns>A data reader to the SchoolInfo.</returns>
        public IDataReader Fetch(string userName, string applicationName)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("Abraca"))
            {
                using (var cmd = new SqlCommand("usp_SchoolByUserName_Get", ctx.Connection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserName", crit.userName).DbType = DbType.String;
                   // cmd.Parameters.AddWithValue("@ApplicationName", applicationName).DbType = DbType.String;
                    return cmd.ExecuteReader();
                }
            }
        } 

Concerning this issue, I can't reproduce it. I guess it only shows on specific cases (like first criteria property is a string).

Coordinator
Feb 11, 2012 at 12:33 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 11, 2012 at 9:19 AM

Hi

The only issue I have is that I get this error

"No parameterless constructor defined for this object." when I don't have the  public GetByUserName() { } and no data is returned and when I add that line data is returned okay.

Have I misunderstood this post?

http://forums.lhotka.net/forums/p/7645/36438.aspx

Either way, I get an error without the parameterless constructor and not with.  Is it something to do with my SILVERLIGHT directives not in the right place?

Thanks

Richard

Coordinator
Feb 11, 2012 at 9:31 AM
Edited Feb 11, 2012 at 9:31 AM

Referring to the above forum thread

Quoting Rocky:

The MobileFormatter requires a parameterless ctor. On the Silverlight side it must be public (due to reflection limitations) and on the .NET side it can be non-public - but either way it must be there.
In C# and VB, if you define a parameterized ctor the compiler won't automatically generate a parameterless ctor on your behalf. It sounds like you may have a paramterized ctor defined in your class, and on the .NET side that probably prevented the compiler from generating the parameterless ctor.

This is an issue when you are using the MobileFormatter. So you are right, something must be changed in the templates, either:

  • do not generate any constructor at all

or

  • generate a parameterless contructor (private or public with #if SILVERLIGHT directives)

Thanks for the persistence. ;)

Coordinator
Feb 11, 2012 at 9:32 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Feb 13, 2012 at 11:31 PM
Edited Feb 13, 2012 at 11:33 PM
zen8019 wrote:

Hi

Also on the Fetch for criteria... crit.userName should just be userName

  /// <summary>
        /// Loads a SchoolInfo object from the database.
        /// </summary>
        /// <param name="userName">The User Name.</param>
        /// <param name="applicationName">The Application Name.</param>
        /// <returns>A data reader to the SchoolInfo.</returns>
        public IDataReader Fetch(string userName, string applicationName)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("Abraca"))
            {
                using (var cmd = new SqlCommand("usp_SchoolByUserName_Get", ctx.Connection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserName", crit.userName).DbType = DbType.String;
                   // cmd.Parameters.AddWithValue("@ApplicationName", applicationName).DbType = DbType.String;
                    return cmd.ExecuteReader();
                }
            }
        }
Hope this helps
Richard

 

Hi Richard,

Can't reproduce this one. I tried with two string parameters, both non nullable.

Can you send the project file so I can see what's different?