NameValue list parameter

NameValue list parameter

Old forum URL: forums.lhotka.net/forums/t/480.aspx


dstarkey posted on Tuesday, June 27, 2006

Hello,  is it possible to pass a parameter to the static method of a name value list collection. 

For example, if you want a list of <employees> name and <employee number> value for a specific location.  How would you pass in this location parameter to retrieve this data??  Pardon my ignorance, but hope to come up to speed soon.  I looked at the criteria object but it seems that it is already accounted for in the stmt:

_list = DataPortal.Fetch<NameValueList>(new Criteria(typeof(NameValueList)));

Any help, example is greatly appreciated. 

Would this work:

NameValueList NVLbyLoc = GetNameValueList(locNo);

Thanks

Don

RanceDowner1234 replied on Tuesday, June 27, 2006

I had the same problem... solved it by creating my own Private class called FilteredCriteria, instead of using the stock Criteria.  Since you pass it in by Object it works. 

Rance

P.S.  To explain what I'm doing here.  I'm loading the object as a Singleton (so it's only instatiated once for the entire application lifecycle).  But upon initial instantiation it loads a version of the nvList with a Blank Line at the beggining, which is useful for combo boxes with a blank value at the top, and then it loads another instance without a Blank value at the top, which is useful for validation.  Nevertheless, my "FilteredCriteria" decides which list to return.  You can ignore all the Singleton/Blank Line stuff when you port to your own needs.  The most important piece in your case is the FilteredCriteria Object code.

Option Compare Text
Option Explicit On
Option Strict On

Imports Csla
Imports System.Data.SqlClient
Imports SharedLibrary.Data

Public Class OrgActionDescriptionNVList
    Inherits NameValueListBase(Of String, String)

#Region " Module Variables "
    Private Shared mInstance As OrgActionDescriptionNVList
    Private Shared mInstanceWithBlank As OrgActionDescriptionNVList
#End Region

#Region " Factory Methods "

    Private Sub New()
        'Private: Forces Factory
    End Sub

    Public Shared Function GetValueList(ByVal showBlank As Boolean) As OrgActionDescriptionNVList
        'Note: Singleton Pattern
        If mInstance Is Nothing Or mInstanceWithBlank Is Nothing Then
            mInstance = DataPortal.Fetch(Of OrgActionDescriptionNVList)(New FilteredCriteria(False))
            mInstanceWithBlank = DataPortal.Fetch(Of OrgActionDescriptionNVList)(New FilteredCriteria(True))
        End If

        If showBlank = True Then
            Return mInstanceWithBlank
        Else
            Return mInstance
        End If
    End Function

#End Region

#Region " Data Access "

    <Serializable()> _
    Private Class FilteredCriteria
        Private _showBlank As Boolean
        Public ReadOnly Property ShowBlank() As Boolean
            Get
                Return _showBlank
            End Get
        End Property

        Public Sub New(ByVal showBlank As Boolean)
            _showBlank = showBlank
        End Sub
    End Class

    Protected Overrides Sub DataPortal_Fetch(ByVal criteria As Object)
        Me.IsReadOnly = False

        Dim c As FilteredCriteria = DirectCast(criteria, FilteredCriteria)

        If c.ShowBlank Then
            Add(New NameValuePair("", ""))
        End If

        Dim data As New SQLConnector

        Using cn As SqlConnection = data.Connection
            cn.Open()
            Using cm As SqlCommand = cn.CreateCommand
                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "usp_UTIL_OrgActionDescriptionNVList_Select"

                Using dr As New Data.SafeDataReader(cm.ExecuteReader)
                    While dr.Read()
                        With dr
                            Dim id As String
                            Dim desc As String

                            id = .GetString("Key")
                            desc = .GetString("Description")

                            Add(New NameValuePair(id, desc))

                        End With
                    End While
                End Using
            End Using
        End Using

        Me.IsReadOnly = True

    End Sub

#End Region

End Class

Pradeep replied on Tuesday, June 27, 2006

I dont think, you can do this with just NameValueList object. But you could extend the NameValueList object and write your own collection.
 
What I did was, I created a new collection called Lookup, inheriting from NameValueList. This new object will have a new criteria object, that will take in parameter's like employee No, etc. Also, you will have to overwrite the DataPortal_Fetch method to query the database by your new criteria's.
 
Anotherway would be to extend from ReadOnlyCollectionBase object. But  you would have to create a struct to hold the data.
 
HTH
Pradeep

RockfordLhotka replied on Tuesday, June 27, 2006

The solution Rance puts forward is correct. NameValueListBase is just a base class, like BusinessBase. When you create your subclass, you can implement your own criteria class if you desire. I only provide the built-in Criteria class to support the simple case - you should create your own criteria class for anything beyond that simple case.

fabiousa7 replied on Friday, September 29, 2006

I had the same problem, I took the solution Rance presented, translated to C# and modified it a little.

I made this to list available website localization languages for specific companies.

Works great!

Fabio

//codeSample

using System;

using System.Data;

using System.Data.SqlClient;

using Csla;

using Csla.Data;

namespace GTA.Library

{

[Serializable()]

public class LanguageList : Csla.NameValueListBase<int, string>

{

#region Factory Methods

private LanguageList()

{ /* require use of factory method */ }

private static LanguageList _list;

public static LanguageList GetLanguageList(int organizationId)

{

if (_list == null)

_list = DataPortal.Fetch<LanguageList>(new FilterCriteria(organizationId));

return _list;

}

public static void InvalidateCache()

{

_list = null;

}

#endregion //Factory Method

#region Data Access

[Serializable()]

private class FilterCriteria

{

public int OrganizationId;

public FilterCriteria(int organizationId)

{

this.OrganizationId = organizationId;

}

}

private void DataPortal_Fetch(FilterCriteria criteria)

{

RaiseListChangedEvents = false;

using (SqlConnection cn = new SqlConnection(Database.GTA))

using (SqlCommand cm = cn.CreateCommand())

{

cn.Open();

cm.CommandType = CommandType.StoredProcedure;

cm.CommandText = "SP_GetLanguageList";

if (criteria.OrganizationId > 0)

cm.Parameters.AddWithValue("@OrganizationId", criteria.OrganizationId);

using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))

{

IsReadOnly = false;

while (dr.Read())

this.Add(new NameValuePair(dr.GetInt32("Id"), dr.GetString("Name")));

IsReadOnly = true;

}

RaiseListChangedEvents = true;

}

}

#endregion //Data Access

}

}

Copyright (c) Marimer LLC