Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introdução ao ASP .NET MVC

Introdução ao ASP .NET MVC

Apresentação de Introdução ao ASP .NET MVC na primeira reunião presencial da comunidade NetPonto, a comunidade portuguesa de arquitectura e desenvolvimento de software.

C. Augusto Proiete

May 13, 2012
Tweet

More Decks by C. Augusto Proiete

Other Decks in Programming

Transcript

  1. Caio Proiete +9 anos de experiência profissional em TI Consultor

    da Novabase Formador da Ciclo Microsoft MVP, MCT http://caioproiete.net
  2. Agenda • Introdução – ASP .NET: WebForms e MVC •

    O que é MVC? • Funcionamento do ASP .NET MVC • WebForms: Vantagens e Desvantagens • Demonstração – Criação de Controllers, Views e Rotas
  3. Introdução: ASP .NET e WebForms • Não é a mesma

    coisa! • WebForms, é um framework do ASP .NET - A plataforma para desenvolvimento de aplicações web
  4. Agora temos o ASP .NET MVC • Que é um

    outro framework para o ASP .NET • E agora ficamos com duas opções: –ASP .NET WebForms –ASP .NET MVC
  5. ASP .NET MVC: Características • Total controlo do HTML gerado

    • Total controlo das URLs (Clean URLs) • Permite automatizar testes na interface gráfica com facilidade • Extremamente extensível
  6. ASP .NET MVC: URLs simplificadas • No ASP .NET WebForms:

    • /Produtos.aspx?Categoria={dF7504E0-4F80- 1173-9A0C-0305482C3301} • No ASP .NET MVC: –/Produtos/Livros
  7. O que é o ASP .NET MVC? • Actualmente, é

    um framework instalado à parte • Não vem com o Visual Studio 2008, nem com o .NET Framework 3.5. Nem mesmo no SP1 destes • Estará incluído no Visual Studio 2010
  8. O ASP .NET MVC inclui 1 (um) novo assembly e

    namespace – System.Web.Mvc Que depende de outros assemblies – System.Web.dll (2.0) – System.Core (3.5) – System.Web.Extensions (3.5) – System.Web.Abstractions (3.5 SP1) – System.Web.Routing (3.5 SP1)
  9. OK, mas o que é esse MVC? • MVC =

    Model-View-Controller • É um padrão de arquitectura aplicacional, design pattern • Permite separar as responsabilidades em uma aplicação
  10. Separar responsabilidades? • Sim. Por exemplo, a apresentação dos dados

    para o utilizador deve ser responsabilidade de uma parte “X” da aplicação. • Já a parte de obter os dados da base de dados, responsabilidade de uma parte “Y” da aplicação
  11. Funcionamento do ASP .NET MVC Request ViewData Controller View A

    View cria uma representação visual desses dados
  12. Funcionamento do ASP .NET MVC Request ViewData Controller Response View

    E essa representação é enviada de volta para o navegador
  13. WebForms: Vantagens • Desenvolvimento rápido (RAD) • Controlos que “trabalham”

    por você • Conceito semelhante a desenvolver para Windows Forms. Aproveita conhecimento prévio
  14. WebForms: Desvantagens • Pouco controlo sobre o HTML gerado •

    Muito difícil para automatizar testes nas páginas • Abstração do funcionamento real das aplicações web (PostBacks, ViewState), que pode induzir a erros com facilidade
  15. ~113 Kb só de ViewState (!) /wEPDwULLTE3NDAyNzI0MzMPZBYCZg9kFgICAw9kFgYCAw8PFgIeCEltYWdlVXJsBSsvdXBsb2FkL0FuYU1hcmlhQnJhZ2EuanBnLjM0MHgzNDAudGh1bWIuYXhkZGQCBQ9kFgYCBQ8WAh4LXyFJdGVtQ291bnQC/////w9 kAgcPZBYCZg9kFgJmD2QWAgIBD2QWAgIVDxAPFgIeC18hRGF0YUJvdW5kZ2QPFhsCAQICAgMCBAIFAgYCBwIIAgkCCgILAgwCDQIOAg8CEAIRAhICEwIUAhUCFgIXAhgCGQIaAhsWGxAFBEFjcmUFAkFDZxAFB0FsYWdvYXMFAkFMZxAFCEFtYXpvbmFzB QJBTWcQBQZBbWFww6EFAkFQZxAFBUJhaGlhBQJCQWcQBQZDZWFyw6EFAkNFZxAFEERpc3RyaXRvIEZlZGVyYWwFAkRGZxAFD0VzcMOtcml0byBTYW50bwUCRVNnEAUGR29pw6FzBQJHT2cQBQlNYXJhbmjDo28FAk1BZxAFDE1pbmFzIEdlcmFpcw UCTUdnEAUSTWF0byBHcm9zc28gZG8gU3VsBQJNU2cQBQtNYXRvIEdyb3NzbwUCTVRnEAUFUGFyw6EFAlBBZxAFCFBhcmHDrWJhBQJQQmcQBQpQZXJuYW1idWNvBQJQRWcQBQZQaWF1w60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mtfX0JhY2 tpbmdGaWVsZB08UmV ... (continua) ~153 Kb 153 Kb - 113 Kb = 40 Kb Multiplique por este texto por 6
  16. Referências Site Oficial – http://asp.net/mvc Blog do Scott Guthrie (Microsoft)

    – http://weblogs.asp.net/scottgu Blog do Phil Haack (Microsoft) – http://haacked.com NerdDinner ASP.NET MVC Tutorial – http://shrinkster.com/18m9
  17. Referências MSDN Virtual Lab: Introduction to ASP.NET MVC New! –

    http://go.microsoft.com/?linkid=9631340 MSDN Virtual Lab: Advanced ASP.NET MVC New! – http://go.microsoft.com/?linkid=9640183 MSDN Virtual Lab: Building an Application with ASP.NET MVC New! – http://go.microsoft.com/?linkid=9636146 NerdDinner ASP.NET MVC Tutorial – http://shrinkster.com/18m9