지난 번까지 간단히 사용자 configuration 을 구성하고 접근하는 것에 대해서 구현해 보았다.
이번부터는 조금씩 복잡한 사용자 Configuration 을 구성하는 것을 구현해 보고자 한다.
그렇다고 구현까지 복잡한 것은 아닐테니.. ㅋㅋ
기본저으로 사용자 Configuration Section 안에 정의된 모든 Configuration 은 전부 그런것은 아니지만 .config 파일 내에 Attribute 로 표현될 것이다.
그리고 elecment 와 attribute 를 사용하여 좀더 복잡한 XML 구조의 Configuration 을 구성할 수 있다.
이제부터 이전에 만들었던 Configuration Section 내에 Element 를 포함 시킬것이고 그 Elecment 내에 다시 Attribute 들을 포함시켜 Custom Configuration 을 구성할 것이다.
Custom Configuration Element 는 간단하게 ConfigurationElement Class 를 상속하기만 하면 만들 수 있다.
구현자체는 Custom Configuratoin Element 가 Custom Configuration Section 내에 포함되는 것 이외에는 Custom Configuration Section 을 구현할때와 많이 다르지 않다.
구성할 Configuration 의 내용을 보면 아래 그림과 같다.
먼저 woojjaConfigurationElement Class 를 추가하고 ConfigurationElement 로부터 상속을 받는다.
1 Imports System.Configuration
2
3 Public Class woojjaConfigurationElement
4 Inherits ConfigurationElement
5
6 Private Shared woojjaElementProperties As ConfigurationPropertyCollection
7
8 Private Shared phoneProperty As ConfigurationProperty
9 Private Shared emailProperty As ConfigurationProperty
10
11 #Region "생성자"
12 Shared Sub woojjaConfigurationElement()
13 phoneProperty = New ConfigurationProperty("phoneValue", GetType(String), String.Empty, ConfigurationPropertyOptions.IsRequired)
14 emailProperty = New ConfigurationProperty("emailValue", GetType(String), String.Empty, ConfigurationPropertyOptions.None)
15
16 woojjaElementProperties = New ConfigurationPropertyCollection()
17 woojjaElementProperties.Add(phoneProperty)
18 woojjaElementProperties.Add(emailProperty)
19
20 End Sub
21 #End Region
22
23 #Region "Properties"
24 <ConfigurationProperty("phone")> _
25 Public ReadOnly Property PhoneValue() As String
26 Get
27 Return CStr(MyBase.Item("phone"))
28 End Get
29 End Property
30
31 <ConfigurationProperty("email")> _
32 Public ReadOnly Property EmailValue() As String
33 Get
34 Return CStr(MyBase.Item("email"))
35 End Get
36 End Property
37 #End Region
38 End Class
위와 같이 Element 를 추가한 후에 이전에 만들었던 Section Class 내에 Element 로 추가되어야 하기 때문에 Property 로 추가해 주어야 한다.
아래와 같이 WoojjaConfigurationSection Class 를 수정해 준다.
1 Imports System
2 Imports System.Configuration
3
4 Public Class WoojjaConfigurationSection
5 Inherits ConfigurationSection
6
7 Private Shared woojjaProperties As ConfigurationPropertyCollection
8
9 Private Shared nameProperty As ConfigurationProperty
10 Private Shared isManProperty As ConfigurationProperty
11 Private Shared birthdayProperty As ConfigurationProperty
12 Private Shared woojjaElement As ConfigurationProperty '====> 추가
13
14 #Region "생성자"
15 Shared Sub WoojjaConfigurationSection()
16 nameProperty = New ConfigurationProperty("nameValue", GetType(String), String.Empty, ConfigurationPropertyOptions.IsRequired)
17 isManProperty = New ConfigurationProperty("isManValue", GetType(Boolean), False, ConfigurationPropertyOptions.None)
18 birthdayProperty = New ConfigurationProperty("birthdayValue", GetType(Date), Nothing, ConfigurationPropertyOptions.None)
19 woojjaElement = New ConfigurationProperty("woojjaElement", GetType(woojjaConfigurationElement), Nothing, ConfigurationPropertyOptions.IsRequired) '====>추가
20
21 woojjaProperties = New ConfigurationPropertyCollection()
22 woojjaProperties.Add(nameProperty)
23 woojjaProperties.Add(isManProperty)
24 woojjaProperties.Add(birthdayProperty)
25 woojjaProperties.Add(woojjaElement) '====>추가
26
27 End Sub
28 #End Region
29
30 #Region "Properties"
31 <ConfigurationProperty("nameValue", isrequired:=True)> _
32 Public ReadOnly Property NameValue() As String
33 Get
34 Return CStr(MyBase.Item("nameValue"))
35 End Get
36 End Property
37
38 <ConfigurationProperty("isManValue")> _
39 Public ReadOnly Property isManValue() As Boolean
40 Get
41 Return CBool(MyBase.Item("isManValue"))
42 End Get
43 End Property
44
45 <ConfigurationProperty("birthdayValue")> _
46 Public ReadOnly Property BirthdayValue() As Date
47 Get
48 Return DirectCast(MyBase.Item("birthdayValue"), Date)
49 End Get
50 End Property
51 '====>추가
52 <ConfigurationProperty("woojjaElement")> _
53 Public ReadOnly Property WoojjaElementValue() As woojjaConfigurationElement
54 Get
55 Return DirectCast(MyBase.Item("woojjaElement"), woojjaConfigurationElement)
56 End Get
57 End Property
58 #End Region
59 End Class
이제 우리가 원한 것은 모두 구현한 것 같다.
폼에 TextBox 두개를 추가하고 우리가 구현한 사항을 확인해 보자.
아래와 같이 Source 를 수정하고
1 Imports System.Configuration
2 Imports Woojja.Configuration
3
4 Public Class Form1
5
6 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
7 Me.TextBox1.Text = ConfigurationManager.AppSettings.Item("woojja").ToString()
8 End Sub
9
10 Private Sub btnGetinfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetinfo.Click
11 Dim woojjaSection As WoojjaConfigurationSection = DirectCast(ConfigurationManager.GetSection("woojja"), WoojjaConfigurationSection)
12 Dim strName As String = woojjaSection.NameValue
13 Dim isMan As Boolean = woojjaSection.isManValue
14 Dim datBirthday As Date = woojjaSection.BirthdayValue
15 Dim strPhone As String = woojjaSection.WoojjaElementValue.PhoneValue
16 Dim strEmail As String = woojjaSection.WoojjaElementValue.EmailValue
17
18 txtName.Text = strName
19 If isMan Then
20 rdoMan.Checked = True
21 Else
22 rdoWoman.Checked = True
23 End If
24 txtBirthday.Text = datBirthday.ToString("yyyy-MM-dd")
25
26 txtPhone.Text = strPhone'====>추가
27 txtEmail.Text = strEmail'====>추가
28
29 End Sub
30 End Class
그리고 F5를 꾹 눌러보자
다음은 이번에 추가한 Configuration Element 가 여러개 포함된 Section 을 구현할 것이다.
즉... Element Collection 에 관해...
그럼 모두 행복한 고수되셔요..
woojja ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\