This is a migrated thread and some comments may be shown as answers.

Class constructor with required properties

4 Answers 44 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Craig
Top achievements
Rank 1
Craig asked on 21 Mar 2014, 12:04 PM
Hi
In my ORM model I have domain class that has a number of string properties that are not nullable.
When I create a new instance of the generated class these properties are null.
Is there anything I can do to have the class generated so that required string properties are initialized to string.Empty?

Regards
Craig

4 Answers, 1 is accepted

Sort by
0
Accepted
Kristian Nikolov
Telerik team
answered on 24 Mar 2014, 01:52 PM
Hello Craig,

There are two approaches which you could use in order to initialize the string properties of your persistent classes. One is to modify our code generation templates, the other is to extend the persistent classes with a customized parameterless constructor. In my answer I will detail both of these approaches.

Customizing the code generation templates:
From Code Generation Settings window of both  the Model Settings dialog or the New Domain Model wizard you can copy the default templates locally to your project in order to modify them. Use the Copy Default To... button to do so. After that, under the Includes_ver.2 folder you can find the PropertiesGenerator template. In it, you can modify the GenerateFieldForProperty method to auto-initialize the string properties for your domain classes. The following code snippet is an example:
protected void GenerateFieldForProperty(Telerik.OpenAccess.CodeGeneration.CodeProperty property)
{
    string fieldModifier = "private";
    if(property.IsFieldProtected)
    {
        fieldModifier = "protected";
    }
 
    string propertyType = property.ToTypeString();
 
    string initialValue = string.Empty;
    if(string.Equals(propertyType, "string", StringComparison.CurrentCultureIgnoreCase))
    {
        initialValue = " = String.Empty";
    }
 
    if (property.IsIEnumerable)
    {
        initialValue = " = new List<"+ property.Type +">()";
    }
#>
<#= fieldModifier #> <#= propertyType #> <#= property.FieldName #><#= initialValue #>;
<#+
}
This approach is applicable when you need the string properties of multiple domain classes to be auto-initialized with the same value.

Using custom parameterless constructor:
The persistent classes of your model are generated as partial so their definition can be done over multiple code files. You could create a new code file for each class where you would like to have the string properties initialized and define a constructor with no parameters which initializes the string properties. This approach requires more manual modifications but it does provide more control over which of the string properties should be initialized and how.

I have attaches a sample project which illustrates both approaches.

I hope this helps. Should you have additional questions or need assistance, feel free to contact us again via our Ticket System or post in our forums again.

Regards,
Kristian Nikolov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Craig
Top achievements
Rank 1
answered on 31 Mar 2014, 10:34 AM
Hi Kristian,
Thanks for your reply.
I'm already using the code generation templates and I would like to extend them as you suggest. However, I only want the strings initialized for required properties e.g.

private string _site;
[Column("Site", OpenAccessType = OpenAccessType.StringVariableLength, Length = 5, Scale = 0, SqlType = "varchar")]
[Storage("_site")]
[StringLength(5)]
[Required()]
public virtual string Site
{ ...
 
At the moment I am trying to scan the attribute collection to find the attribute but it is not going so well.

Perhaps there is an easier way?

Regards,
Craig
0
Craig
Top achievements
Rank 1
answered on 31 Mar 2014, 11:26 AM
Hi
I've a working template. I modified your sample code slightly to do what I need.
bool isReqd = false;
if(property.Attributes == null || property.Attributes.Count() == 0)
{
    isReqd = false;
}
else
{
    foreach(var attribute in property.Attributes)
    {
        if (attribute.ToCode().ToLower().Contains("required") == true)
            isReqd = true;
    }
    if (isReqd == true)
    {
        if(string.Equals(propertyType, "string", StringComparison.CurrentCultureIgnoreCase))
        {
            initialValue = " = String.Empty";
        }
    }
}

Thanks
Craig
0
Kristian Nikolov
Telerik team
answered on 02 Apr 2014, 12:53 PM
Hello Craig,

Indeed this is the modification which allows to auto initialize only the fields for the required properties. We are glad that you have found a solution for your scenario.

Do not hesitate to get back to us in case you have additional questions or need help.

Regards,
Kristian Nikolov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
General Discussions
Asked by
Craig
Top achievements
Rank 1
Answers by
Kristian Nikolov
Telerik team
Craig
Top achievements
Rank 1
Share this question
or