I am a new bie to DDD. In our DDD project ,we have a requirement that our DBContext should only expose AggregateRoots.. Assuming that our DbContext is as shown below
public class ClassContext : DbContext
{
public DbSet<Class> Classes{ get; set; }
public DbSet<Students> Students{ get; set; }
}
and Class is the aggregate root . Is the following implementation the right way
public class ClassContext : DbContext
{
public DbSet<Class> Classes{ get; set; }
private DbSet<Students> Students{ get; set; }
}
Any comment is appreciated
It's certainly useful to think about aggregate roots in your application, but don't try to apply DDD concepts to an Entity Framework class model.
An Entity Framework class model is not a domain model. It's a data access layer. Any considerations regarding including or hiding entities and/or navigation properties should be motivated by facilitating smooth data access and nothing more.
It's highly unlikely that you're always going to read/create/update/delete students through classes only. That would make unnecessary clunky code. And who says that students will always be in a class?
But maybe this isn't the best example of an aggregate. A Student
doesn't have an identifying relationship with a Class
, because next time he'll be in another class. It would be different with the classic Order-OrderLine
relationship. I can imagine that in that case you might only expose a DbSet<Order>
.
So just expose the DbSet<Students>
as public class.