c# - Is using an id as a business value an anti-pattern -
with risk of being labeled off-topic going ask way :-)
i joined new development team , have habit of doing following. while have considered anti pattern find out can't explain why. so, curious opinions.
let consider following situation: have invoice application, when new invoice needs created, has new unique invoice number (like inv0001). of course, stored in db, in table having auto increment field 'id'. so, generate number id.
class invoice{ [key] int id { get; set;} string number => string.concat("inv", this.id.tostring().padleft(4, '0')); } i have (blindly) obeyed rule "don't use id business value". can't motivate it.
don't use id business value
you can apply same explanation used motivating of separation of concerns.
if in business logic code using features of implementation details of id, example generated database, mean business logic depend on database implementation.
id have 1 responsibility - provide unique value, can identify entity , link other related entities. in business logic can use id value equality conditions.
for example if have id of type integer generated database.
, have somewhere in business logic condition
if (id == 0) { return "new"; } your business logic depend on id implementation (type). mean can use id of value integer , can not change guid example.
Comments
Post a Comment