Search code examples
c#dependency-injectionstructuremap3

Dependency Injection and Entities


I have two classes and have one to many relationship as shown below

public class User
{
    public string Name{get;set;}
    public IList<Address> Addresses{get;set}
}

public class Address
{
   public string StreetAddress{get;set;}
   public User User{get;set;}
}

To add address to user I need to initiate Addresses property in User constructor as

public User()
{
   this.Addresses=new List<Address>();

}

Is this scenario good candidate to use DI to initiate List or should I initiate the Address list in constructor as shown.


Solution

  • If the the address data itself is required to construct an instance of User, it may be a candidate for DI. But if's just a matter of making sure the list is instantiated, then your code is correct as-is.