πŸ“œ ⬆️ ⬇️

Understanding MVC in ASP.NET MVC and more

Have you ever refactored β€œfat” controllers? Have to create multi-storey models of representations? Add data to the view and rewrite the controller code? Did it seem to you that something was going wrong?


The reason is that many MVC frameworks do not quite follow the MVC pattern, and people who use them without even noticing themselves deviate further from it. It would seem that it is quite simple, and described in Wikipedia , but time after time there are problems in understanding it.


Take a look at this classic action of the fat controller:


public ActionResult AddComment(int articleId, string content)
{
    // -
    var article = ArticleRepository.Instance.GetById(articleId);

    if (article == null)
        return View("ArticleNotFound");

    var comment = new Comment
    {
        ArticleId = articleId,
        Content = content
    };

    CommentRepository.Instance.Add(comment);

    //  
    var viewModel = new ArticleViewModel
    {
        Article = article,
        Comments = CommentRepository.Instance.GetByArticleId(articleId)
    };

    return View("Article", viewModel);
}

:


@Model ArticleViewModel
<header><h1>@Model.Article.Title</h1></header>
<article>@Model.Article.Content</article>
<ul>
    @foreach (var comment in Model.Comments)
    {
        <li>@comment.Content</li>
    }
</ul>

?



ASP.NET MVC, MVC-. , .


MVC


Model β€” , ORM,


, . MVC . Model β€” , - . , , , . .


( ) , : API: , , . -, .


, ( , ) , , :


public interface IArticleService
{
    //  

    //         ,
    //     ,     .
    void AddComment(string commentContent);
}

View β€” ,


- View , , . . , ( ). - , ( ).


, , , %PageName%ViewModel ( ).


MVC , , , ! ( , , .)


, . , . , , , Model.


, , . "Helper"-. , , View.


Controller β€” -


, ( -) . , , :


  1. ()
  2. ( ), ( )
  3. ,

( MVC , -, , , .)


, , %PageName%ViewModelProvider, .


ASP.NET MVC


. , , . ASP.NET MVC? . Autofac Ninject, , .



:


  1. Autofac .
  2. , .

View


ASP.NET MVC , , . , WebViewPage c Execute β€” WebPageExecutingBase, .
WebViewPage, @inherits :


( - , )


using System.Web.Mvc;

namespace ASP
{
    //      ArticleViewModel
    //  int- 
    public abstract class ArticlePageBase: WebViewPage<int>
    {
        // Autofac      ,
        //     .

        // ,     
        public IArticleRepository ArticleRepository { get; set; }
        // ,       
        public ICommentRepository CommentRepository { get; set; }
        //  ,   ,
        //    
        public IArticleRecomendationService RecomendationService { get; set; }
    }
}

(Razor- )


@inherits ArticlePageBase
@{
    //      
    var article = ArticleRepository.GetById(Model);
    var comments = CommentRepository.GetByArticleId(Model);
    var recommendedArticles = RecomendationService.GetRecomendations(Model);
}
@*  *@

, , , . . Autofac , , .


, , , InitializePage , , :


public abstract class ArticlePageBase: WebViewPage<int>
{
    public IArticleRepository ArticleRepository { get; set; }

    // ,      
    protected Article Article;

    protected override void InitializePage()
    {
        Article = ArticleRepository.GetById(Model);
    }
}

@inherits ArticlePageBase
<h1>@Article.Title</h1>

C#- ( , ):


@inherits ArticlePageBase
@{
    //      
    var article = ArticleRepository.Get(Model);
    var comments = CommentRepository.GetByArticleId(Model);
    var recommendedArticles = RecomendationService.GetRecomendations(Model);
}
<h1>@ArticleService.Get(Model).Name</h1>

<header><h1>@article.Title</h1></header>
<article>@article.Content</article>
<ul>
    @foreach (var comment in comments)
    {
        <li>@comment.Content</li>
    }
</ul>
<ul>
    @foreach (var recommendedArticle in recommendedArticles)
    {
        //     
    }
</ul>

, .


View ASP.NET MVC Core


Razor ASP.NET MVC Core @inject . , , , :


@model int
@inject IArticleRepository ArticleRepository
@{
    var article = ArticleRepository.GetById(Model)
}
<h1>@article.Title</h1>

, ConfigureServices Startup:


public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    //  
    services.AddTransient<IArticleRepository, ArticleRepository>();
    //  
}


, :


public ActionResult AddComment(int articleId, string content)
{
    //    
    //   .   .
    try
    {
        ArticleService.AddComment(articleId, content);
    }
    catch (ArticleNotFoundException e)
    {
        return View("ArticleNotFound");
    }
    return View("Article", articleId);
}

. . - , , β€” .


MVC β€” ADR


MVC - . - -. , , , , , . SRP. .


. β€” ADR ( PHP), . MVC:



ADR- .NET.


!



MVC


(SRP)


Autofac


Castle Windsor



Autofac


ASP.NET MVC Core


Paul M. Jones ADR


ADR


ADR


')

Source: https://habr.com/ru/post/342748/


All Articles