[.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 ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\
반응형

[.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)

이번엔 ASP.NET Routing 에 대해서 살펴볼까 합니다.

ASP.NET MVC Framework 에서는 Controller Class와 Action 에 URL을 연결하기 위해서 ASP.NET Routing 을 사용합니다. ASP.NET Routing 은 여러분이 먼저 정의해 놓은 패턴에 따라 URL 내의 parameter 를 파싱합니다. 그리고 자동적으로 parameter argument 로 Controller Action 에 변수로 전달합니다. 이 과정에서 Web site 내의 특정파일을 연결할 필요는 없습니다.

기본적으로 ASP.NET MVC Project 는 미리 설정된 URL Routing Rule Set 을 가집니다.
새로운 ASP.NET MVC project template 에 의해서 생성되는 Global.asax 의 ASP.NET Application Class 내에 선언된 기본적인 이름 기반(name-based) URL mapping 규칙 set 을 사용하여 개발을 시작할 수 있습니다.
기본적으로 [Controller]/[Action]/[id] 의 패턴을 가지고 있습니다.

우리가 만든 Web Site 를 호출해 보겠습니다. 50000번 포트를 사용하고 있었죠?
http://localhost:50000 를 날립니다. 
안뜬다면... ASP.NET Development Web Server 가 호출되어있나 확인하시고...
아래와 같이 뜹니다.

 우리가 Web Server 에 http://localhost:50000 라는 Request 를 날리면 ASP.NET Routing Engin 은 이 Request 를 가로 채서 기본적으로 등록된 [Controller]/[Action]/[id] 패턴의 Route 를 적용할 것입니다.
그런데 URL 에는 Pattern 에 적용할 내용이 포함되어 있지 않죠?
그럼 Routing Engin은 기본 Controller 와 기본 Action 으로 instance를 생성합니다.
기본 값들은 어떻게 알 수 있냐고요?

Global.asax 파일의 Application Class 를 보면 알 수 있습니다.

    7     Shared Sub RegisterRoutes(ByVal routes As RouteCollection)

    8         routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

    9 

   10         ' MapRoute takes the following parameters, in order:

   11         ' (1) Route name

   12         ' (2) URL with parameters

   13         ' (3) Parameter defaults

   14         routes.MapRoute( _

   15             "Default", _

   16             "{controller}/{action}/{id}", _

   17             New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _

   18         )

   19 

   20     End Sub


17 Line 을 보시면 아시겠죠?

음... 또한가지 알아 두셔야 할 점은 이 RegisterRoutes Method 를 수정해서
 [Controller]/[Action]/[id] 패턴의 Route Rule 을 새롭게 수정하실수 있다는 것입니다.

이번 아티클까지 MVC Framework 을 이용해서 Site 를 제작하는데 필요한 내용을 간략하게 살펴보았고 다음에는 Site를 제작해 보도록 하겠습니다.

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


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

제목과 같이 근래에 나온 Samsung Galaxy Phone 의 Emulator 를 추가해보기로 하겠습니다. 정확하게는 Skin을 추가하는 것이지요...

그닥 복잡하지 않습니다. (뭐 이런걸 아티클로 쓰느냐고 할 정도로... ^^')

먼저 아래사이트에 가서 다운 받습니다.
http://rexxars.com/android/android-emulator-skin-samsung-galaxy/

Feel free to get it 이라고 반전시킨부분이 보이시죠?
클릭하시면 다운받으실 수 있습니다.

아래 파일탐색기의 주소란에 보이시는 폴더 경로에 저장하십시요.
그리고 압축을 푸셔요..
이때 주의하실 점은 압축풀려진 폴더 내부에 Galaxy 라는 폴더가 존재하면 안됩니다. ^^

위까지의 순서가 답니다.

그러시면 Eclipse 메뉴의 Window>Android SDK and AVD Manager 클릭하셔서 New 버튼을 클릭하시어 새로운 AVD 를 추가합니다.
이때... Skin 을 선택하게 되는데... Built-in을 선택하시고 오른쪽의 Select 박스를 클릭하시면 Galaxy 를 보실수 있습니다.

Launch 합니다.

짜라잔... Galaxy 가 뜨는 군요...

System 에 따라 다르지만 Loading 되는데 시간이 좀 걸리죠...

드디어 나타났습니다. ^^

여러 에뮬레이터 Device를 등록했다면 Android App를 실행시켰을때 기본적으로 띄울 Emulator Device 를 미리 설정해 놓을 수 있습니다.
아래 탐색기의 폴더로 이동하셔서 config.ini 를 열어보시면 skin.name 과 skin.path 라는 항목을 찾으실 수 있을 것입니다.
이 부분을 default 로 사용할 emulator device 의 정보로 변경하시면...
android application 을 실행했을 때 기본적으로 실행되는 Skin을 선택하실 수 있습니다.

아래 이미지는 위 config.ini 파일에 설정되어 있는 HVGA skin 의 모습입니다.
반응형

'ETC > Android' 카테고리의 다른 글

[Android] Deploy Application  (1) 2010.06.04
[Android] 구글 TV  (0) 2010.05.25
[Android] Android 개발환경 구축  (2) 2010.05.16

+ Recent posts