CSLA Design with Data access layer using DTO and Lazy load

CSLA Design with Data access layer using DTO and Lazy load

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


Maqster posted on Wednesday, November 07, 2007

Hi, I'm trying to design a new CSLA project solution.

My wish is to establish a 'Proof of Concept' using CSLA with DAL and LazyLoad.
Now I'm wondering if I'm on the right track, with the following code, I have just taken out the parts of interest.

Root object:

        private ChildList_children;
        public ChildList ChildList
        {
            get
            {
                // Lazy load
                if (_children== null)
                {
                    _children= ChildList.GetList(_id);
                }
                return _children;
            }
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            try
            {
                DataFactory df = new DataFactory();
                RootDto data;
                using (RootData rd = df.GetRootDataObject())
                {
                    data = rd.LoadRoot(criteria.Id);

                    if (data != null)
                    {
                        _id = data.Id;
                        _name = data.Name;
                    }
                }
                _childList = null;
                MarkOld();
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }

ChildList object:
         internal static ChildList GetList(
          int parentId)
        {
            return new ChildList(parentId);
        }

        private ChildList(int parentId)
        {
            MarkAsChild();
            Fetch(parentId);
        }

         private void Fetch(int parentId)
        {
            RaiseListChangedEvents = false;
            DataFactory df = new DataFactory();
            List<ChildDto> dataList;
            using (RootData rd = df.GetRootDataObject())
            {
                dataList = rd.GetChildList(parentId);
            }
            foreach(ChildDto dto in dataList)
            {
                    this.Add(Child.GetChild(dto));
            }
            RaiseListChangedEvents = true;
        }

DAL RootData:

public RootDto LoadRoot(int id)
        {
            RootDto rootDto = null;
            using (SafeDataReader data = new                 SafeDataReader(Sprocs.Instance.spRootGetById(id)))
            {
                if(data.Read())
                {
                    rootDto = new RootDto();
                    rootDto.Id = data.GetInt32("Id");
                    rootDto.Name = data.GetString("Name");
                }
            }
            return companyDto;
        }

public List<ChildDto> GetChildList(int parentId)
        {
            List<ChildDto> dtoList = new List<ChildDto>();
            ChildDto dto;
            using (SafeDataReader data = new SafeDataReader (Sprocs.Instance.spChildGetListByParentId(parentId)))
            {
                while (data.Read())
                {
                    dto = new ChildDto();
                    dto.ParentId = data.GetInt32("ParentId");
                    dto.ChildId = data.GetInt32("ChildId");
                    dto.ChildName = data.GetString("ChildName");

                    dtoList.Add(dto);
                }
            }
            return dtoList;
        }

Thankful for any comment

Best regards
Maqster

McManus replied on Wednesday, November 07, 2007

Hi Maqster,

IMHO I think there's a flaw in the design, being that in the Factory method of the child list you don't use the DataPortal. Instead of returning new ChildList(parentId), you should return DataPortal.Fetch<ChildList>(new Criteria(parentId)).

In the ChildList class you must define a Criteria class and a DataPortal_Fetch(Criteria criteria) method.

Cheers,
Herman

Maqster replied on Monday, November 12, 2007

Hi Herman and thanks for the tip, now I know how to handle this.

Cheers,
Marqus

Copyright (c) Marimer LLC