Reuse TodoItem class
nbarbettini opened this issue · comments
Is the NewTodoItem
POCO an unnecessary duplication of TodoItem
?
My initial thought was keeping everything super clean and separate, but that might be a little too much.
Probably you can reuse TodoItem
but with Bind
attribute (to selectively choose properties to be bound) when explaining the model binding concept. NewTodoItem
with just one property Title
of type string
seems to be overkill in general but a good and simpler example for explaining model binding that populates a class object without Bind
property.
As far as I can tell, the NewTodoItem is a good example of a Dto (aka view model, resource model, etc). I really like that design pattern, so I looked seeing it in this book. But I can see how that would have seemed a bit unclear to beginner programmers.
@Welkie Yeah, that's exactly what I was trying to illustrate (DTOs). This example didn't come out as clean as I wanted because the properties are identical to TodoItem
, but I'll see if it makes sense to keep it.
@sadqiang I agree, it would be cool to show model binding - feels like it might be outside the scope for now though. In a more complex application model binding could be very useful.
@nbarbettini I really like DTOs, I use them for add, edit, delete, command use cases, etc. But they do add a lot of code that beginners (like me, before I took that class in school) can have trouble understanding. It feels like fluff to them, layers in between without purpose. I think that this book would be best without them, just using the model classes for changing data in the database.
I think DTOs aren't really an ASP.NET Core thing, they're a web development in general thing, and that they would be a great entry in that "extra" category we discussed adding in previous issues, along with subjects like AJAX, SPAs, etc.
Might you be interested in a pull request for a chapter on them now that my school is dying down and I have some time to make meaningful contributions to the book? :)
In my perspective if NewTodoItem
is still kept there for the next release of the book, it can also be regarded as a good introduction to model binding. You don't need to say much about it if it is beyond the scope, but saying something like
Hey, look there is an interesting feature, the form field
Title
submitted to the server gets wrapped byNewTodoItem
as an argument ofpublic async Task<IActionResult> AddItem(NewTodoItem newItem)
.Title
of type string is converted or serialized automatically by the framework model binding toNewTodoItem
. This feature is good if the form has many fields submitted to the server because a single action method argument suffices to accept a bunch of incoming form fields.
Of course DTO is another good aspect!
What I decided to do in fbddcae was:
- Get rid of
NewTodoItem
and reuseTodoItem
- Add a paragraph explaining the DTO concept as an alternative
- Talk a little bit more about model binding
Thanks to both of you for the feedback!