The way you declared the product is using the C# Auto Property.
First of all, you should ask yourself do you need the product
to be visible as public
member, or you want to encapsulate the logic of setting the product
.
If the answer is that you want to want the Product
to be able to set outside, then there is no need to declare any additional method:
public class Brand
{
public int Id { get; set; }
public string Name { get; set; }
public IProduct Product { get; set; }
}
public static void Main(string[] args)
{
var brand = new Brand
{
Id = 1,
Name = "Name",
Product = new Product()
};
}
However, if you want to encapsulate the way you set the product
then consider using either Composition or Aggregation approaches:
public class Brand
{
private int _id;
public string _name;
private readonly IProduct _product;
public Brand(IProduct product, int id, string name )
{
_product = product;
_id = id;
_name = name;
}
}
public static void Main(string[] args)
{
var brand = new Brand(new Product(), 1, "prd");
}
Note: if you still want to be able to set the product
after object declaration, consider a different name for the method, like SetProduct
or something with close meaning, because AddProduct
means that you are dealing with the collection of Products.