'Storing multiple fields of address in entity class
I need to store multiple addresses, like address1 , address2 and zipcode etc.
Should I declare all the fields individually?
for instance:
class A{
// many fields
Address1:string;
Address2:string;
zipCode:number;
city:String;
}
or is there any better way to declare them in entity class? I have been told creating entity classes is the best practice.
Solution 1:[1]
Have you tried something like this?
class Address {
//...
zipCode: string;
city: string;
// ...
}
class A {
addresses: Address[]
}
Solution 2:[2]
I would rather create a specific class for those fields, something like:
class A {
// many fields
address: AddressData
}
class AddressData {
address1: string;
address2: string;
zipCode: string;
city: string;
}
IMO it's better to keep data that belong to the same group in a separated class. Also I wouldn't store the two addresses in an array because you would lose the information about the main address and the secondary address.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | androidavid |
| Solution 2 | jashinhidan |

