-1

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

Sabarish Sathasivan
  • 1,196
  • 2
  • 19
  • 42
  • 1
    You need to read more about DDD and the [Repository pattern](http://blog.sapiensworks.com/post/2014/06/02/The-Repository-Pattern-For-Dummies.aspx) as a concept,only then you should tackle implementations. If you're looking for a quick solution, that's not how it's done. Btw, the ORM is an [implementation detail](http://blog.sapiensworks.com/post/2012/04/15/The-Repository-Pattern-Vs-ORM.aspx/) of a repository . If you don't understand the concept, the code won't help you. – MikeSW Jul 03 '15 at 18:39
  • @MikeSW FYI, OP wasn't even asking about the Repository pattern. – Ash Mar 06 '19 at 05:19

2 Answers2

2

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.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
0

I don't think the private declaration for Students is needed. Presumably the Class object contains something like

 public virtual List<Student> Students { get; set; }  

so, given the requirement that you are only exposing aggregate roots,your code to find a student will always need to find a class and pull the student from it.

Kirsten
  • 15,730
  • 41
  • 179
  • 318