NameValueListBase and ObjectFactory

NameValueListBase and ObjectFactory

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


Brendt posted on Saturday, June 29, 2013

Good day.

i would like to get some help on Name Value List. i have tried to follow the example in the projecttracker but i cannot seem to get mine to work, i have not used the exact way the example is showing. basically would i would like to do is have a name value list in my BO and call the Data access from my 'Data Access'

My Application is setup as follows: i have my 'BusinessLibrary' and my 'Data Access' i would like to have a class (NamedValueList) in the BusinessLibrary and call my DataAccess to return the data for me. how would i go about writting this. i have tried the example in the projecttracker and have basically done the following, put 'Role and Roles' into the BO and and put the 'RolesList' into my data access but its obvious that that does not work the way i envisage because i cannot call the code in the data access 'RolesList' because it inherits from NameValueList and not from ObjectFactory.

 

please could some kind soul point me in the right direction. i would highly appreciate it.

JonnyBee replied on Saturday, June 29, 2013

Assuming that you use ObjectFactory - tha NameValuelist will have its own ObjectFactory that you can call into from the other ObjectFactory 

Brendt replied on Saturday, June 29, 2013

i am using the ObjectFactory because i would like to keep my business objects and data access separate.

how would i call one objectfactory from another? are there any examples of this?

JonnyBee replied on Saturday, June 29, 2013

You create an instance of the ObjectFactory and call the Fetch method.

An object factory is a class just like an other class. 

var nvlList = new NVLObjectFactory().Fetch();

from within the other ObjectFactory. 

Brendt replied on Monday, July 01, 2013

ok, im still quite confused with the whole thing. i have decided to try a different route. is it possible to have NameValueListBase of ReadOnlyBase?

i have added 3 classes: TextList(which is of NameValueListBase<int,string>), TextListInfo(which is of type ReadOnlyBase<TextListInfo>) and TextListFactory (which is where im wanting to get data in from)

 

in my TextListInfo Class i have 2 properties as follows:

public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
        public int Id
        {
            get { return GetProperty(IdProperty); }
            set { LoadProperty(IdProperty, value); }
        }

        public static readonly PropertyInfo<string> ValueProperty = RegisterProperty<string>(c => c.Value);
        public string Value
        {
            get { return GetProperty(ValueProperty); }
            set { LoadProperty(ValueProperty, value); }
        }

 

in my TextList class i have the following:

public class TextList: NameValueListBase<int,string>
    {
        public static TextList GetList()
        {
            return DataPortal.Fetch<TextList>();
        }
    }

 

and in my TextListFactory class i have the method to get the data:

public static List<TextListInfo> _list;

   public TextList FetchList()
        {
            MessageBox.Show("Being called");

            var obj = (TextList)MethodCaller.CreateInstance(typeof(TextList));
            _list = new List<TextListInfo>();
            //_list = new List<NameValueListBase<int,string>>();
            //public TextListInfo _list;


            obj.RaiseListChangedEvents = false;

            ConnSingleton cn = ConnSingleton.getDbInstance();
            cn.GetDBConnection();

         
            using (SqlCommand cm = cn.CreateCommand())
            {

                cm.CommandType = CommandType.Text;
                //cm.CommandText = "SELECT * FROM tTP WHERE TP_Name = '" + Name + "'";
                cm.CommandText = "";

                using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                {
                    while (dr.Read())
                    {

                 
                     _list.add( <--dont know how to add it to the list

                    }
                    cn.CloseConn();
                };
                //obj.AddRange(_list); <--get an error here adding it to the object
               
            }

            obj.RaiseListChangedEvents = true;
            return obj;

No the problem im facing is that i dont know how to had the to my list a collection of NamedValueList of my TextlistInfo class, could you please point me in the right direction of how i would go about implementing this. i would like to bind the list to a combo box and then be able to get the ID of the Value of the selected item from the combo box.

 

Brendt replied on Monday, July 01, 2013

ok, im still quite confused with the whole thing. i have decided to try a different route. is it possible to have NameValueListBase of ReadOnlyBase?

i have added 3 classes: TextList(which is of NameValueListBase<int,string>), TextListInfo(which is of type ReadOnlyBase<TextListInfo>) and TextListFactory (which is where im wanting to get data in from)

 

in my TextListInfo Class i have 2 properties as follows:

public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
        public int Id
        {
            get { return GetProperty(IdProperty); }
            set { LoadProperty(IdProperty, value); }
        }

        public static readonly PropertyInfo<string> ValueProperty = RegisterProperty<string>(c => c.Value);
        public string Value
        {
            get { return GetProperty(ValueProperty); }
            set { LoadProperty(ValueProperty, value); }
        }

 

in my TextList class i have the following:

public class TextList: NameValueListBase<int,string>
    {
        public static TextList GetList()
        {
            return DataPortal.Fetch<TextList>();
        }
    }

 

and in my TextListFactory class i have the method to get the data:

public static List<TextListInfo> _list;

   public TextList FetchList()
        {
            MessageBox.Show("Being called");

            var obj = (TextList)MethodCaller.CreateInstance(typeof(TextList));
            _list = new List<TextListInfo>();
            //_list = new List<NameValueListBase<int,string>>();
            //public TextListInfo _list;


            obj.RaiseListChangedEvents = false;

            ConnSingleton cn = ConnSingleton.getDbInstance();
            cn.GetDBConnection();

         
            using (SqlCommand cm = cn.CreateCommand())
            {

                cm.CommandType = CommandType.Text;
                //cm.CommandText = "SELECT * FROM tTP WHERE TP_Name = '" + Name + "'";
                cm.CommandText = "";

                using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                {
                    while (dr.Read())
                    {

                 
                     _list.add( <--dont know how to add it to the list

                    }
                    cn.CloseConn();
                };
                //obj.AddRange(_list); <--get an error here adding it to the object
               
            }

            obj.RaiseListChangedEvents = true;
            return obj;

No the problem im facing is that i dont know how to had the to my list a collection of NamedValueList of my TextlistInfo class, could you please point me in the right direction of how i would go about implementing this. i would like to bind the list to a combo box and then be able to get the ID of the Value of the selected item from the combo box.

 

Brendt replied on Monday, July 01, 2013

ok, im still quite confused with the whole thing. i have decided to try a different route. is it possible to have NameValueListBase of ReadOnlyBase?

i have added 3 classes: TextList(which is of NameValueListBase<int,string>), TextListInfo(which is of type ReadOnlyBase<TextListInfo>) and TextListFactory (which is where im wanting to get data in from)

 

in my TextListInfo Class i have 2 properties as follows:

public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
        public int Id
        {
            get { return GetProperty(IdProperty); }
            set { LoadProperty(IdProperty, value); }
        }

        public static readonly PropertyInfo<string> ValueProperty = RegisterProperty<string>(c => c.Value);
        public string Value
        {
            get { return GetProperty(ValueProperty); }
            set { LoadProperty(ValueProperty, value); }
        }

 

in my TextList class i have the following:

public class TextList: NameValueListBase<int,string>
    {
        public static TextList GetList()
        {
            return DataPortal.Fetch<TextList>();
        }
    }

 

and in my TextListFactory class i have the method to get the data:

public static List<TextListInfo> _list;

   public TextList FetchList()
        {
            MessageBox.Show("Being called");

            var obj = (TextList)MethodCaller.CreateInstance(typeof(TextList));
            _list = new List<TextListInfo>();
            //_list = new List<NameValueListBase<int,string>>();
            //public TextListInfo _list;


            obj.RaiseListChangedEvents = false;

            ConnSingleton cn = ConnSingleton.getDbInstance();
            cn.GetDBConnection();

         
            using (SqlCommand cm = cn.CreateCommand())
            {

                cm.CommandType = CommandType.Text;
                cm.CommandText = "";

                using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                {
                    while (dr.Read())
                    {

                 
                     _list.add( <--dont know how to add it to the list

                    }
                    cn.CloseConn();
                };
                //obj.AddRange(_list); <--get an error here adding it to the object
               
            }

            obj.RaiseListChangedEvents = true;
            return obj;

No the problem im facing is that i dont know how to had the to my list a collection of NamedValueList of my TextlistInfo class, could you please point me in the right direction of how i would go about implementing this. i would like to bind the list to a combo box and then be able to get the ID of the Value of the selected item from the combo box.

 

JonnyBee replied on Monday, July 01, 2013

I'm not sure I understand what you want to do either.

NameValueList is a separate base class and can be a property on another object or returned as its own list.

The most basic parts is:

  [ObjectFactory(typeof(NVLListFactory))]
  public class NVLList : NameValueListBase<stringstring>
  {
    public static NVLList GetList()
    {
      return DataPortal.Fetch<NVLList>();
    }
  }
  public class NVLListFactory : ObjectFactory
  {
    [RunLocal]
    public NVLList Fetch()
    {
      var list = (NVLListMethodCaller.CreateInstance(typeof (NVLList));
      SetIsReadOnly(list, false);
 
      list.Add(new NameValueListBase<stringstring>.NameValuePair("1""Item1"));
      list.Add(new NameValueListBase<stringstring>.NameValuePair("2""Item2"));
      list.Add(new NameValueListBase<stringstring>.NameValuePair("3""Item3"));
 
      SetIsReadOnly(list, true);
      return list;
    }
  }

Usage on client:
      var nvl = NVLList.GetList();
Usage in another ObjectFactory;
var nvl = new NVLListFactory().Fetch();

Brendt replied on Monday, July 01, 2013

that's perfect. thanks JonnyBee. was exactly what i was looking for.

one other thing, how would i get the key from the value of the item? basically i want to show the values in a combo box (eg. Item1, Item2, Item3) how would i get their corresponding ID (eg. 1,2,3)?

Brendt replied on Monday, July 01, 2013

never mind, i managed to find the key from the list.

thanks again JonnyBee

Copyright (c) Marimer LLC