For the API /v1/users
, the data shape of the user is:
{
id: 1,
name: 'teresa teng'
}
For the API /v1/user/:id
, the data shape of the user has some extra fields:
{
id: 1,
name: 'teresa teng',
role: 'admin',
email: 'example@gmail.com'
}
I'm looking for good names for the interfaces. The names I've used are:
Option 1:
interface BaseUserEntity {
id: number;
name: string;
}
interface UserEntity extends BaseUserEntity {
role: string;
email: string;
}
I don't really like Base
prefix for the interface name.
Option 2:
interface UserEntity {
id: number;
name: string;
}
interface UserDetailEntity extends UserEntity {
role: string;
email: string;
}
I think User
and UserDetail
are the same thing, introducing excess words - Detail
. Do you have any suggestions for the naming convension? Thanks.
Top comments (6)
I have an other thought on the topic, And I think this is an approach that is promoted/adopted by the golang community(correct me if i am wrong).
What about having only one global (maybe in a types directory)
user
type/interface and other versions with only partial set of fields, they are only defined local in the module that uses them.I have this thought as well. Ideally, we should have interfaces corresponding to the data model represented by the database tables, and I remember that there are ORM and Code Generation tools that can do this.
In addition, for the front end, we should define the types and interfaces for the view model.
The model(type, interface) conversion process is as follows:
API => Frontend representation: Domain Model Interface => View model interface
Frontend representation => API: View model interface => Domain Model interface.
We will convert the domain model interface that contain all fields to target types through TS's Utility Types or Map Type rather than repeated definitions.
The naming convention for the derived types just like my above comment. The key point of the naming convention should be readability, scalability, reusability and maintainability.
More naming convention examples:
The previous advice given is great, here is an alternative way to model your domain:
That's a very good question.
Today you have two versions of your User interface and tomorrow you'll have 3 then 4. It's not scalable this way. It's ok if the number of interfaces representing a part of an Entity is small (I would say 2 max).
In your case, I would pick 'DetailedUser', 'DecoratedUser' etc.. for the second interface. But for the first, it's not an User Entity, because it's lacking of some data. It would have been ok if the UserEntity interface defines all the properties a User has.
For such issues, for the sake of my applications, I prefer to not defined multiple interface and stay with only 'full' interfaces version of my entities.
Then, every : -function, -component, -class, etc... take some input that they defines and output something that they defines.
For instance, if you have a function that take a user with its roles and only uses its name and its roles. The function shouldn't take the whole user, instead it should only take name and roles properties.
I've seen others using full user interface all the time (or full class instance), it's also ok, it really depends of what you think it's better in your case.
If you want, you can provide more context of the exiting app behind these interfaces so we can help you better!
Have a nice day,
aaaand, Happy Coding!
Thanks for your advice! The reason why I use
Entity
word is because the user data has aid
field.So, based on your advice. I can name the interface like this:
But if I don't create types like
T0
,T1
andT2
to hold the target type, I have to pick the fields repeatly in the consumer side. It violates the DRY principle. I am looking for a general naming convension forT0
,T1
andT2
types. Maybe like this:Or
type UserFromGetUserById {}
andtype UserFromGetUsers {}
, the name consists byUser
+From
+ method name of the API serviceI would do
or,
Why would you include
name
, when you don't want to includerule
?