开发者

Getting a country code - Service or Utility method?

开发者 https://www.devze.com 2023-03-25 23:16 出处:网络
Getting a country code or internal id might require accessing a database or simply loading a properties file.

Getting a country code or internal id might require accessing a database or simply loading a properties file.

How would such a method be placed in an application though? As a utility开发者_C百科 method it would look out of place so would it fit in an Application Service?


In DDD this type of method would be placed in a repository, which is basically an abstraction of a data access layer. For something like country codes you could consider caching the data since it is static.


In my experience, countries do not have internal ids. (Your domain may be different.) So, if it doesn't exist in your domain, and you're doing domain-driven design, you're going to want to avoid an artificial concept like country id.

Let's assume you've got an Address table in the database. Instead of storing CountryId, store the CountryAbbreviation instead. This is a natural key.

Now, how do you make sure that your Address only had valid country_id? If you are in control of all the clients (e.g.) then you do something like this:

<select>
  <option value="US">United States</option
  <option value="UK">United Kingdom</option
</select>

Above, you can see that the value attribute is the country abbreviation.

Unless reports are being based off country abbreviations, you don't really need to make sure they're super-consistent (unless it's super important to your domain).

0

精彩评论

暂无评论...
验证码 换一张
取 消