CSLA 3.0.2: Single ReadOnlyBase Object

CSLA 3.0.2: Single ReadOnlyBase Object

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


cjherasmus posted on Friday, December 07, 2007

Hi,

Help please, I'm lost.

My use case require the retrieval of a single ReadOnlyBase object.  I would like, not to use the overheads of a ReadOnlyCollection.  This use case will always require a single readonly object.

Any ideas please?

Regards,

JoeFallon1 replied on Friday, December 07, 2007

Just create a BO derived from ReadOnlyBase(Of T)

This is a stand alone read only BO that can fetch itself.

Joe

cjherasmus replied on Tuesday, December 11, 2007

Joe,

Thanks for the help. I got it to work with the following implementation:

[Serializable()]
    public class MyBO : CSLA.ReadOnlyBase<MyBO>
    {
       
private int _id;
        private string _name = string.Empty;

        public override string ToString()
        {
            return string.Format("{0}: {1}", _id, _name);
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        public static bool CanGetObject()
        {
            return true;
        }

        private MyBO() { }

        public static MyBO GetMyBO(int id)
        {
            if (!CanGetObject())
            {
                throw new System.Security.SecurityException("You are not authorized to view the object.");
            }
            return DataPortal.Fetch<FullBibleVerse>(new Criteria(id));
        }

        [Serializable()]
        private class Criteria
        {
            private int _id;

            public int Id
            {
                get { return _id; }
            }

            public Criteria(int id)
            {
                _id = id;
            }
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            using (SqlConnection cn = new SqlConnection(DatabaseConnection.MyDB))
            {
                cn.Open();
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.CommandText = "GetMyBO";
                    cm.Parameters.AddWithValue("@_id", criteria.Id);
                    using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                    {
                        dr.Read();
                        _id = dr.GetInt32("id");
                        _name = dr.GetString("name");
                    }
                }
            }
        }
}

Regards,

Copyright (c) Marimer LLC