[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (1)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (2)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (3)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (4)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (5)

지난번까지는 ASP.NET MVC2 Application 구성에 대해서 간단하게 살펴보았습니다.
그리고 MVC Application 을 생성했을때 기본적으로 만들어지는 파일들로 Build 까지 해 보았습니다.

이번 부터는 본격적으로 Application 을 만들어 나가려고 하는데요.
지난 아티클에서 만든 기본적인 Web Application 을 바탕으로 이어 나가려고 합니다.

고객들의 List 를 뿌려보고요. 고객 정보를 조회하고 고객의 주소를 생성, 수정, 삭제하는 코드를 작성해 보도록 하겠습니다. 

다음 세 개의 Controller 를 작성할 것입니다. 고객 리스트, 고객정보를 다루는 Controller, 주소를 생성, 수정, 삭제하는 Address Controller 와 마지막으로 Application 의 환영 Page View를 다루는 Home Controller 입니다. 

음 그럼 먼저 master 파일을 아래의 Source 로 변경합니다.

    1 <%@ Master Language="VB" Inherits="System.Web.Mvc.ViewMasterPage" %>

    2 

    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    4 <html xmlns="http://www.w3.org/1999/xhtml">

    5 <head runat="server">

    6     <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

    7     <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />

    8 </head>

    9 

   10 

   11 <body>

   12     <div class="page">

   13 

   14         <div id="header">

   15             <div id="logo">

   16                     <h1>My MVC Application</h1>

   17             </div>           

   18             <ul id="menu">

   19                 <li>

   20                     <%= Html.ActionLink("Home", "Index", "Home")%>

   21                 </li>

   22                 <li>

   23                     <%= Html.ActionLink("About Us", "About", "Home")%>

   24                 </li>

   25                 <li>

   26                     <%= Html.ActionLink("Customers", "Index", "Customer") %>

   27                 </li>

   28             </ul>

   29         </div>

   30 

   31         <div id="main">

   32             <div id="content">

   33 

   34                 <asp:ContentPlaceHolder ID="MainContent" runat="server" />

   35 

   36             </div>

   37             <div id="footer">

   38                 <p>

   39                     My Sample MVC Application &copy; Copyright 2009

   40                 </p>

   41             </div>

   42         </div>

   43 

   44     </div>

   45 </body>

   46 </html>


그럼 EDM (Entity Data Model) 을 사용하는 것으로부터 시작하겠습니다.

AdventureWorks Entity Data Model 을 추가하도록 하겠습니다.
Models 폴더를 오른쪽 클릭해서 Entity Data Model 을 생성합니다.

파일명은 AdventureWorks.edmx 입니다. 

선택을 하게되면 Entity Data Model Wizard 가 열립니다.
"Generate from database" 항목을 선택합니다.
Connection 정보가 없군요.
그럼 생성을 해 줘야겠죠. "New Connection..." 버튼을 클릭해 줍니다.

SQL Server 를 선택해 주고요. "Continue" 버튼을 클릭합니다.

서버를 선택할 수 있는 창이 뜹니다.
서버를 선택하고 Database 를 선택한 후 연결상태를 테스트 합니다.
그리고 "OK" 버튼을 꾹 눌러주면...

선택한 사항에 대한 내용이 채워져 있습니다.
Connection String 에 대해서는 한번 살펴보셔도 좋을 듯 합니다.
Wizard 를 사용해서 뿐만이 아니라 직접 코딩할때도 쓸수 있을 테니 말이죠.
그리고 Checkbox 의 내용을 보면 Textbox 에 적혀진 이름으로 Web.config 파일에 ConnectionString 정보를 저장하겠느냐 라는 옵션을 달아 놨습니다.
Web.config 파일을 확인해 보셔도 좋을 듯 합니다.

Database 를 선택했으니 사용하려는 Table 을 선택해야겠지요?

과정을 모두 마치게 되면 아래와 같이 Entity Model 이 생성됩니다.
이 파일을 직접 별도의 Text Editor 로 열어보십시요. Address, CustomerAddress, Customer Class 가 만들어 진것을 확인 하실수 있습니다.

이번엔 AdventureWorksRepository 라는 Repository Class 를 생성할 텐데요... 이 Repository 라는 녀석은 방금 생성한 Model 의 Entity 들을 검색하기위한 Mehtod 를 노출하는 역할을 합니다.


아래는 AdventureWorksRepository.vb 파일의 코드입니다.

    1 Public Class AdventureWorksRepository

    2     Private context As New AdventureWorksLT2008R2Entities()

    3 

    4     Public Function GetCustomers(ByVal page As Integer, ByVal size As Integer) As IEnumerable(Of Customer)

    5         Return context.Customer.OrderBy(Function(c) c.CustomerID).Skip(page * size).Take(size)

    6     End Function

    7 

    8     Public Function GetCustomerById(ByVal customerId As Integer) As Customer

    9         Return context.Customer.Include("CustomerAddress.Address").Where(Function(c) c.CustomerID = customerId).First()

   10     End Function

   11 

   12     Public Sub AddAddress(ByVal address As Address, ByVal customerId As Integer)

   13         address.rowguid = Guid.NewGuid()

   14         address.ModifiedDate = DateTime.Now

   15         context.AddObject("Address", address)

   16 

   17         Dim customerAddress As New CustomerAddress()

   18         customerAddress.Address = address

   19         customerAddress.Customer = GetCustomerById(customerId)

   20         customerAddress.rowguid = Guid.NewGuid()

   21         customerAddress.AddressType = "Shipping"

   22         customerAddress.ModifiedDate = DateTime.Today

   23         context.AddObject("CustomerAddress", customerAddress)

   24         context.SaveChanges()

   25     End Sub

   26 

   27     Public Sub UpdateAddress()

   28         context.SaveChanges()

   29     End Sub

   30 

   31     Public Sub DeleteAddress(ByVal address As Address, ByVal customerId As Integer)

   32         Dim customerAddress As CustomerAddress = GetCustomerAddressById(address.AddressID, customerId)

   33         context.DeleteObject(address)

   34         context.DeleteObject(customerAddress)

   35         context.SaveChanges()

   36     End Sub

   37 

   38     Public Function GetAddressById(ByVal addressId As Integer) As Address

   39         Return context.Address.Where(Function(a) a.AddressID = addressId).First()

   40     End Function

   41 

   42     Public Function GetCustomerAddressById(ByVal addressId As Integer, ByVal customerId As Integer) As CustomerAddress

   43         Return context.CustomerAddress.Where(Function(a) a.AddressID = addressId AndAlso a.CustomerID = customerId).First()

   44     End Function

   45 End Class


다음으로는 CustomerController 가 고객 List 를 표시하는데 사용할 ViewData 를 생성할 것입니다.
ViewData 파일들을 저장할 ViewData 폴더를 생성해 줍니다.


CustomerViewData Class 를 생성해줍니다.


다음은 CustomerViewData Class 의 내용입니다.

    1 Public Class CustomerViewData

    2     Public Property Customers As IEnumerable(Of Customer)

    3     Public Property PreviousPage As Integer

    4     Public Property NextPage As Integer

    5 End Class


다음은 AddressController 가 사용할 ViewData를 생성할 것입니다.

다음은 AddressViewData Class 의 내용입니다.

    1 Public Class AddressViewData

    2     Public Property Address As Address

    3     Public Property CustomerId As Integer

    4 End Class


생각외로 이미지를 Capture 를 하면서 설명을 하다보니 내용은 별개 없는데 엄청 기네요...
이렇게 해야하나 싶기도 한데... ㅡㅡ'
그래도 한번은 끝까지 해보려 합니다.

오늘은 Entity Data Model 을 생성하는 방법을 살펴봤구요.
Entity Data Model 이 Entity 를 노출하기위한 Repository Class 를 생성했습니다.
그리고 Controller 가 사용할 ViewData 까지 생성해 보았습니다.

다음은 오늘 생성한 것들을 바탕으로 본격적으루다가 Controller 들과 View 들을 생성해 보겠습니다.


행복한 고수되십시요...

woojja ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\
반응형

+ Recent posts