|
Imports System.Security.Permissions
Imports System.Configuration
Imports System.Collections.Specialized
<PermissionSetAttribute(SecurityAction.InheritanceDemand, Name:="FullTrust")> _
<PermissionSetAttribute(SecurityAction.LinkDemand, Name:="FullTrust")> _
Public Class LinqSettingsProvider
Inherits SettingsProvider
Private conn As String
Public Sub New()
'Sets the connection string to use MDF file included with this demo (TestConsole/bin/debug)
'Change to a connection string for your database.
Dim dbpath = AppDomain.CurrentDomain.BaseDirectory & "Netrudder.mdf"
conn = "Data Source=.\SQLEXPRESS;AttachDbFilename=""" & dbpath & _
""";Integrated Security=True;User Instance=True"
End Sub
Public Overrides Sub Initialize(ByVal name As String, ByVal col As NameValueCollection)
MyBase.Initialize(Me.ApplicationName, col)
End Sub
Public Overrides Property ApplicationName() As String
Get
ApplicationName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
End Get
Set(ByVal value As String)
' Do nothing.
End Set
End Property
''' <summary>
''' This method retrieves settings values from the data source.
''' The return object, SettingsPropertyValueCollection, is consumable by any class
''' implementing ApplicationSettingsBase, for example NetrudderSettings
''' </summary>
''' <param name="context">Provides some additional contextual information</param>
''' <param name="collection">Collection of settings properties supplied by the settings class (e.g. NetrudderSettings)</param>
''' <returns></returns>
''' <remarks></remarks>
Public Overrides Function GetPropertyValues(ByVal context As SettingsContext, _
ByVal collection As SettingsPropertyCollection) As SettingsPropertyValueCollection
Dim settings = New SettingsPropertyValueCollection
Try
Using db As New NetrudderDataContext(conn)
For Each propDef As SettingsProperty In collection
'Compiler will issue a warning if we attempt to use the iteration variable
'in a lambda expression, so we reassign propDef to a new variable
Dim lmb_propDef = propDef
'Create a new instance of SettingsPropertyValue from the property definition
Dim setting = New SettingsPropertyValue(propDef)
'Query the database for the one setting that is enabled and
'has the name matching the name of the property
Dim settingEntity = db.Settings.SingleOrDefault(Function(s) _
s.Name = lmb_propDef.Name And _
s.Enabled = True)
If settingEntity IsNot Nothing Then
'If a matching property is found, its value is used.
setting.PropertyValue = settingEntity.Value
Else
'Do nothing: the default property value is used instead.
End If
'Indicate that the setting has not changed
setting.IsDirty = False
'Add the setting to the collection to be returned to the settings class
settings.Add(setting)
Next
End Using
Catch ex As Exception
'TODO
End Try
'Return the collection to the settings class
Return settings
End Function
''' <summary>
''' This method updates the settings in the data source.
''' CAUTION: Before you use this code in produciton, please consider adding security
''' </summary>
''' <param name="context"></param>
''' <param name="collection"></param>
''' <remarks></remarks>
Public Overrides Sub SetPropertyValues(ByVal context As SettingsContext, _
ByVal collection As SettingsPropertyValueCollection)
Try
Using db As New NetrudderDataContext(conn)
For Each setting As SettingsPropertyValue In collection
'If the setting value has changed, update the data source
If setting.IsDirty Then
'Create a new variable for use in the lambda expression
Dim lmb_setting = setting
'Update the correct entity with the new value of the setting property
db.Settings.Single(Function(s) s.Name = lmb_setting.Name) _
.Value = setting.PropertyValue
End If
Next
'Persist to database
db.SubmitChanges()
End Using
Catch ex As Exception
'TODO
End Try
End Sub
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.