0
public record Currency
{
   public string Code {get; init;}
   public HashSet<Country> Countries {get; init;}
}
public record Country
{
   public string Code {get; init;}
   public HashSet<Currency> Currencies {get; init;}
}

Assuming that:

  1. A country accepts one to many currencies.

  2. A currency is used in one to many countries.

After some research, records seem to be a reasonable choice for storing countries and currencies, because once they are loaded from the database, I do not need to change them anymore.

However, I do not know how I can construct a hashset of currency records and a hashset of country records, containing the complete information of each other.

In the database I do have a CurrencyCountryRelationship pivot table.

With that table, I can create a list of currency, and then a list of country, and then construct a list of relationship to achieve the result, but I do not want to use them for object models, if possible.

//I just want to cache a one-off configuration from the database for later usage. So if records are not the right choice please let me know. Any solutions will be great. Thanks!

Josh
  • 575
  • 7
  • 17
  • 2
    Are you using an ORM to access the database e.g. EntityFramework or some other technology? – phuzi Jul 07 '21 at 15:15
  • If you use Entity Framework, and if you have an intermediary / join table, and if that join table contains only `CurencyId` and `CountryId` fields, the objects will be generated in C# with appropriate relationships and 'just work'. You will be able to do, for example, `myCountry.Currencies` and get a `List` of currencies, and `myCurrency.Countries` and get a `List` of countries. – Jonathan Jul 07 '21 at 15:29
  • Hi, I am using EF Core. I understand that when I grab data from EF core, it will 'just' work. However, I also need to do Entity Model => DTO => Domain Model as I need to use some existing functions in the domain model. So whilst EF handles the navigation properties for the entity models, when I mapped it into a domain model I do not know how I should be constructing them. – Josh Jul 08 '21 at 10:21

1 Answers1

0

As you know, records in typical relational database would be something like following (its not fully normalized maybe):

Table Country

Country Code| Country Name | Currency code

-- Primary Key would be composite of all columns

Table Currency

Currency Code | Currency Name | Country Code

-- Primary key would be Currency Code

Foreign Key for Table would be Currency Code to Currency table.

If you want you could represent it using lot of ways. But your solution would depend on how you want to use it now and for later.

if you want to use frameworks, for example entity, you can see How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC This is very close to what you are doing.

If you want to use records, you can do When to use record vs class vs struct

If you have use case in mind, you could let us know and I would try to give fuller answer with code.