Loading a Value object in a List or DropdownList, DDD

I need to clarify something.

Does the person have Aggreagate, 2 VO (Country, StateProvince).

I want to load the whole country in my view layer (I use mvc)

Evan says that you use the repository (IPersonRepository) to work with the root entity (it should always return only a reference to the shared array)

public interface IPersonRepository() { void savePerson(Person p); void removePerson(Person p); Ilist<Person> getPerson(); } 

what I usually do to solve this problem:

Add this method to IPersonRepository

 IList<Country> LookupCountrysOfPerson(); 

At the Infra level, implement domain interfaces, for example:

 public IList<Person> LookupCountrysOfPerson() { return Session.CreateQuery("from Countrys").List<Person>()); } 

My partner says it’s wrong.

Sometimes you have to sacrifice your domain model to complete some task.

What is the best way to do this?

with the code please! :)

+4
source share
4 answers

Evans also says (pg 170) "An entity as basic, like Location, can be used by many objects for many reasons ..."

I would also think about making the country an entity for the reasons stated above. Perhaps more importantly, this is a low-level object. You probably even deliver the country by configuration, and not through any actions in the real domain. Therefore, I would remove it from the Person and make it an autonomous entity.

Also, for this type of object, you may not need a dedicated repository, consider creating a single search service that provides access to a query for a group of similar objects of this kind.

+4
source

I would say that it is unlikely that you need a country to be an entity. I suspect that the country is nothing more than reference data, like a person’s title. Is there any behavior related to the country in your domain? I suspect that this is exactly what is printed on letters / envelopes.

This question is somewhat similar to the one I answered some time ago:

Simple aggregated root and repository question

My suggestion is that you implement a Lookup service that your client can use and which is cached. Ignore the DDD rules and anything related to aggregates or repositories for this. As someone else has already mentioned, it is here that the ideology of CQRS comes into play; the client does not need to go through the domain to receive data. The domain is purely transactional, not intended for requests.

This article explains how to create a common search service for reference data for things that usually populate drop-down lists in the user interface (e.g. Title, Country, etc.)

http://wtfperminute.blogspot.com/2011/02/working-with-reference-data-lookups.html

+4
source

If your country actually has a VO domain (you do not want to support the identification thread in the name of the country, etc.), which is the most common scenario, I would add a specialized class to the data access to return a list of all countries as an OO . I would also add caching (second level cache in NHibernate) to the entity of the country and list all the requests of countries so that I do not have to hit DB every time.

In fact, this is where the CQRS really shines. CQRS recognizes that you do not need to go through the domain level to get some data for presentation purposes. In CQRS, you just get some data.

+2
source

It seems that countries are not objects of value here; they have a clear identity and are important for business purposes outside of your Person objects. They must become entities, and they should be considered in a form suitable for them.

Think of it this way: let them say that some troubled country has overthrown its current dictator and changed his name. The reference to the Person object on Country must be valid, because Country not determined by its attributes (i.e., the String denoting its name), but by its identifier.

+1
source

Source: https://habr.com/ru/post/1345780/


All Articles