jeudi 4 juin 2015

Why DbContext instansiats repositories?

In the following code, DbContext instantiates repositories in derived class. I believe it does this somehow by reflection in its constructor. These are my questions :

  1. Why and for which purpose it does it?
  2. Is instantiating properties in self or derived classes a best practice?
  3. If it is a best practice which design pattern is it related to and what is the name of this behavior?

This is the only code in my project, I just add entityframework reference to a console application project :

class Program
{
    static void Main(string[] args)
    {
        var context = new DataContext();
        if(context.Products != null)
            Console.WriteLine("why repositories are not null?!");
    }
}

public class Product
{
}

public class DataContext : DbContext
{
    public IDbSet<Product> Products { get; set; }
} 

Aucun commentaire:

Enregistrer un commentaire