Answer the question
In order to leave comments, you need to log in
What is the easiest way to master dependency injection, code-first, TDD and patterns?
Good afternoon, dear colleagues!
I've been playing C# for two years, it all started with Web Forms, after some time I realized that I needed MVC. I immediately realized that I need to study patterns, dependency injection, code-first, test-driven development, etc. fashionable things (as far as I understood from the texts of vacancies fashionable).
I am currently reading a book by Adam Freeman, doing SportsStore. Here in his book everything is smooth there: this means a model, here is an interface, but here there is no need for an interface, here we introduce a dependency, but here we don’t. Somehow everything goes by itself, and why he decided to do it one way or another does not write.
Let's say an ideal situation - I decided to make a website, how can I understand what logic to put in the controller, which in the model, what methods - where to stick it, why exactly and not otherwise? I understand that patterns are needed here? That is, is there any information on how best to do this or that thing and why?
In general, all this is somehow difficult for me, apparently because there is no understanding, if you read a specific code, everything seems to be clear, but if you look at it as a whole, I don’t understand why this is done.
How would I solve this problem, please advise what to read and see?
Answer the question
In order to leave comments, you need to log in
Good evening! Thanks for the invitation.
In my opinion, you should adhere to the following study priorities:
Now for MVC.
1. Model - here, in principle, everything is simple: a data model. It can be argued here what is meant by Model: a model of the data itself or a presentation model . Personally, after working with the MVVM pattern in WPF, I understand the data model in terms of ASP.NET MVC as the view model , and the term "data model" means the domain model itself (EF code-first, for example). Some might say that it's "extra work" to pack the data model from EF objects into view model objects. Yes, I partially agree. But on the other hand, this gives a certain security guarantee that the user will not accidentally change an important part of the data model (for example, ID).
2. Controller. The main task of the controller is to generate data for display and pass this data to the view. Those. you should strive to ensure that the code of the action method in the controller contains a minimum of code. Ideally, calling a method from the business logic layer and passing the resulting data to the view. If you see that the action method in the controller contains some business logic, then this is a signal for refactoring: Your action method knows too much. From experience, I can add that, on average, the code of an action method contains from 2 to 20-30 lines of code (taking into account the fact that the { and } brackets are on separate lines).
3. Submission. Here, too, everything is simple: display data (from the view model). In no case should you write logic for working with the data itself in the view, for example, you can’t do this:
<div id="account">
@{
using(var db = new MyEfDbContext()
{
var userAccount = db.Accounts.FirstOrDefault(e => e.Username == User.Identity.Username);
if(userAccount != null)
{
@:Имя: @userAccount.Name
@:Фамилия: @userAccount.LastName
}
}
}
</div>
<div id="account">
@{
var accountService = new MyApp.BL.AccountService();
var userAccount = accountService.GetUserAccountByUsername(User.Identity.Name);
if(userAccount != null)
{
@:Имя: @userAccount.Name
@:Фамилия: @userAccount.LastName
}
}
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question