-3

which approach is better and more correct.

class Project
int Id
string Name
int CategoryId

OR

class Project
int Id,
string Name
Category CategoryId
hashtag
  • 63
  • 3
  • 12
  • Should your second example read `Category Category` at the end? Is the `Category` object an instance of a Category class or a value object that represents a category id? – Adrian Thompson Phillips Mar 20 '15 at 09:04
  • It should read Category.CategoryID. In example I've posted it is Category class not value object. CategoryID is foreign key in Project table. – hashtag Mar 20 '15 at 11:42
  • In that case it depends completely on your domain. If you have `Categories` as part of your `Project` aggregate root, then you'd have to accept all the rules that come with that decision (there's way too many to list here). The main ones being that you cannot create or modify a `Category` directly or outside of a `Project`. You would have to modify the `Category` via a method on the `Project` aggregate root. Although I don't know your domain, I'd imagine you'd want to edit/create `Categories` elsewhere. If this is the case then example 1 is more correct (for DDD). – Adrian Thompson Phillips Mar 20 '15 at 11:49
  • currently I use mapping 1:1 stored procedure and domain object. Is this approach good? In future I would like to use repository pattern with classic ADO.NET. – hashtag Mar 20 '15 at 13:49
  • Persistence details are usually outside of the realm of DDD. Ideally when practising DDD you want to design an application/domain-centric model that models your domain. Because of this, nothing should creep upwards from your chosen persistence layer, into your domain model. So in other words, persistence can be done any way you like. – Adrian Thompson Phillips Mar 20 '15 at 15:17

2 Answers2

0

IMHO the domain classes, and not only, moreover, all classes should be declared at singular as they represent a type.

Daniel Stancu
  • 52
  • 2
  • 8
  • Logically, one should use entity types in entities which are related (one to one, one to many, etc), if you use JPA, usually Project has one or many Categories, not CategoryIDs. – Daniel Stancu Mar 20 '15 at 01:03
  • No,I used native Ado.Net with DataReader object – hashtag Mar 20 '15 at 01:05
-1

Project, rather than Projects, as each instance will represent a single project.

L. Blanc
  • 2,150
  • 2
  • 21
  • 31
  • Sorry, both represent single Project. Focus is on Category attribute. – hashtag Mar 20 '15 at 00:52
  • 1
    It depends somewhat on the implementation (ie are you using Hibernate or another ORM, that has its own style?). You probably don't want to call the ID "Category" unless some ORM magic makes that compelling. An ID is an ID and not the thing. – L. Blanc Mar 20 '15 at 00:55
  • I used native ADO.NET with DataReader object. Until now,we mapped stored procedure with domain object 1:1. And I want to optimize domain model. In database we have Projects table and Category codebook. Project can have only one Category – hashtag Mar 20 '15 at 00:58
  • If, when you retrieve a project from the database, the category object is fully constructed (and not just an integer), then you may want to call it category. otherwise category id. – L. Blanc Mar 20 '15 at 01:05