locked
Is it necessary to new the navigation property and the complex property in class constructor? RRS feed

  • Question

  • User185240422 posted
    public partial class Sort
        {
            public Sort()
            {
                this.Products = new Ilist<Product>();
                this.SortProperty = new SortProperty();
            }
        
            public int ID { get; set; }
            public string SortName { get; set; }
            public Property SortProperty { get; set; }
            public virtual IList<Product> Products { get; set; }
        }
    
    public partial class Product
    {
            public int ID { get; set; }
            public string ProductName { get; set; }
            public virtual Sort Sort { get; set; }
    }
    
    public partial class SortProperty
    {
    
       public string SortProperty1 { get; set; }
       public string SortProperty2 { get; set; }
          
    }

    Hello guys. Is it necessary to new the navigation property and the complex property in  class constructor? 

    Can you tell me  which solution is good for new or not , and why?

    Tuesday, April 7, 2015 5:06 AM

Answers

  • User-821857111 posted

    Is it necessary to new the navigation property and the complex property in  class constructor? 

    No.

    Can you tell me  which solution is good for new or not , and why?

    I instantiate collections in the class constructor so that I don't have to test for null. I only have to test for Any(). I don't tend to instantiate complex properties. If I don't have a value to pass to them, I usually want them to be null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2015 7:37 AM

All replies

  • User-821857111 posted

    Is it necessary to new the navigation property and the complex property in  class constructor? 

    No.

    Can you tell me  which solution is good for new or not , and why?

    I instantiate collections in the class constructor so that I don't have to test for null. I only have to test for Any(). I don't tend to instantiate complex properties. If I don't have a value to pass to them, I usually want them to be null.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2015 7:37 AM
  • User185240422 posted

    Thank you so much.

    Tuesday, April 7, 2015 11:03 AM