frontier icon indicating copy to clipboard operation
frontier copied to clipboard

Revisit redundancy in domain dependency to avoid unwanted issues

Open mabdh opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe. Shield is currently heavily using DefaultStringIfEmpty function. The function will return fallback string if input is empty. The reason why we do this is because for some domain entity (not all), there are redundant fields and the usage of them depend on whether we are writing a data to the DB or fetching the joined results from the DB.

type Policy struct {
	ID          string
	Role        role.Role
	RoleID      string `json:"role_id"`
	Namespace   namespace.Namespace
	NamespaceID string `json:"namespace_id"`
	Action      action.Action
	ActionID    string `json:"action_id"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

Policy has redundant dependency to Namespace and NamespaceID, Role and RoleID, Action and ActionID. When creating a policy (writing to DB), we only use RoleID, NamespaceID, ActionID and let the others empty. But when fetching data from DB we use both.

Apart from adding unnecessary complexity, this thing will cause confusion and can lead to unwanted issue. We could have a single source of truth field dependency to avoid this.

Describe the solution you'd like Remove redundancy from the domain entity and decouple the entity if it is necessary to have the different one and rovide adapter to transform between each entity.

mabdh avatar Jul 29 '22 13:07 mabdh

@krtkvrm is there any concern if we are doing this?

mabdh avatar Aug 01 '22 21:08 mabdh

Closing this as for now

mabdh avatar Jan 02 '23 04:01 mabdh