Class constructor with required properties

5 posts, 1 answers
  1. Craig
    Craig avatar
    112 posts
    Member since:
    Nov 2011

    Posted 21 Mar 2014 Link to this post

    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
  2. Answer
    Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 24 Mar 2014 Link to this post

    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.
     
  3. DevCraft banner
  4. Craig
    Craig avatar
    112 posts
    Member since:
    Nov 2011

    Posted 31 Mar 2014 Link to this post

    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
  5. Craig
    Craig avatar
    112 posts
    Member since:
    Nov 2011

    Posted 31 Mar 2014 Link to this post

    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
  6. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 02 Apr 2014 Link to this post

    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.
     
Back to Top
DevCraft banner