Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio
This is the forum to discuss the Wrox book ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solution by Vincent Varallo; ISBN: 9780470396865
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 24th, 2009, 02:23 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default write a sophisticated generic class in BLL

Hello friends
according to the 3rd chapter of the book, I have defined following class:

Code:
    [Serializable()]
    public abstract class AdminBaseEOList<T> : BaseBOList<T> where T : AdminBaseEO, new()
    {
        
        public bool Save(ValidationErrors validationErros, long id)
        {
            // check if this object has any item
            if (this.Count > 0)
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    using (lqAdminDataContext db = new lqAdminDataContext())
                    {
                        if (this.Save(db, ref validationErros, id))
                        {
                            // commit transaction if update was successful
                            ts.Complete();
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            else
            {
                // No items in list so return true
                return true;
            }
        }

        public bool Save(lqAdminDataContext db, ref ValidationErrors validationErrors, long id)
        {
            foreach (AdminBaseEO genericEO in this)
            {
                if (genericEO.DBAction == DBActionEnum.Save)
                {
                    if (!genericEO.Save(db, ref validationErrors, id))
                    {
                        return false;
                    }
                    else
                    {
                        if (genericEO.DBAction == DBActionEnum.Delete)
                        {
                            if (!genericEO.Delete(db,ref validationErrors, id))
                            {
                                return false;
                            }
                        }
                        else
                        {
                            throw new Exception("Unknown DBAction");
                        }
                    }
                }
            }
            return false;
        }
        
    }
now I want to write a generic class that can be used for any table that exist in the database. note that in my program every table has its own dbml file in 'lq[tablename]DataContext' pattern.
I writed following class but there is a problem: abstract classes
I receive and error message: " Should be non-abstract with a public parameterless constructor". I can't emit 'abstract' keyword because it is necessary and unavoidable.

Code:
[Serializable()]
    public abstract class myEOL<A, B> : BaseBOList<A>
        where A : myBaseEO<B>
        where B : DataContext, new()
    {
        
    }
here is definition of MyBaseEO class

Code:
    [Serializable()]
    public abstract class myBaseEO<T> : BaseBO where T : DataContext, new()
    {
        #region Properties

        public enum DBActionEnum
        {
            Save,
            Delete
        }

        // note: value of 0 for ID indicates new record

        // Property declaration
        public DBActionEnum DBAction { get; set; }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public myBaseEO()
            : base()
        {
            // Default the action to save
            DBAction = DBActionEnum.Save;
        }

        #endregion

        #region Methods

        public bool Save(ref ValidationErrors validationErrors, long id)
        {
            if (DBAction == DBActionEnum.Save)
            {
                // begin database transaction
                using (TransactionScope ts = new TransactionScope())
                {
                    // create the DataContext
                    using (T db = new T())
                    {
                        // now save the record
                        if (this.Save(db, ref validationErrors, id))
                        {
                            // commit transaction if update successful
                            ts.Complete();
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("DBAction not save.");
            }
        }

        public abstract bool Save(T db, ref ValidationErrors validationErrors, long id);

        #region Validate

        protected abstract void Validate(T db, ref ValidationErrors validationErrors);

        #endregion

        #region Init

        public abstract void Init();

        #endregion

        #region Delete

        protected abstract void DeleteForReal(T db);

        protected abstract void ValidateDelete(T db, ValidationErrors validationErrors);

        public bool Delete(ref ValidationErrors validationErros, long id)
        {
            if (DBAction == DBActionEnum.Delete)
            {
                // begin database transaction
                using (TransactionScope ts = new TransactionScope())
                {
                    // create the DataContext
                    using (T db = new T())
                    {
                        this.Delete(db, ref validationErros, id);

                        if (validationErros.Count == 0)
                        {
                            // commit the trancsaction since the delete was successful
                            ts.Complete();
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("DBAction not delete.");
            }
        }

        internal virtual bool Delete(T db, ref ValidationErrors validationErrors, long id)
        {
            if (DBAction == DBActionEnum.Delete)
            {
                // check if the record can be deleted. There may be referential
                // integrity rules preventing it from being deleted
                ValidateDelete(db, validationErrors);

                if (validationErrors.Count == 0)
                {
                    this.DeleteForReal(db);
                    return true;
                }
                else
                {
                    // the record can not be deleted
                    return false;
                }
            }
            else
            {
                throw new Exception("DBAction not delete.");
            }
        }

        #endregion

        #region IsNewRecord

        public bool IsNewRecord()
        {
            return ID == 0;
        }

        #endregion

        #region UpdateFailed

        protected void UpdateFailed(ref ValidationErrors validationErrors)
        {
            validationErrors.Add("This record was updated by someone else while you where editing it. Your changes were not saved.");
        }

        #endregion

        #endregion

    }
here is definition of BaseEO class:

Code:
    [Serializable()]
    public abstract class BaseBO
    {
        #region Properties

        public long ID { get; set; }

        public string DisplayText
        {
            get
            {
                return GetDisplayText();
            }
        }

        protected abstract string GetDisplayText();

        #endregion

        #region Methods

        public abstract bool Load(long id);

        protected abstract void MapEntityToCustomProperties(IBaseEntity entity);

        public void MapEntityToProperties(IBaseEntity entity)
        {
            if (entity != null)
            {
                this.MapEntityToCustomProperties(entity);
            }
        }

        #endregion
    }
how can I solve this problem?
  #2 (permalink)  
Old November 29th, 2009, 04:05 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default problem solved

Hello
I realized that I dont need to do this
Because even i can write a generic class for myEO<T> I'm forced to write a myEOList for every entity that I have, because of deferences in properties for each entity.


Similar Threads
Thread Thread Starter Forum Replies Last Post
BLL Edit Object Class ZeroFactorial BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 3 March 25th, 2009 02:17 PM
Difference between Trace.write and Debug.write surendraparashar ASP.NET 2.0 Professional 1 June 27th, 2007 02:12 AM
Why not using a common detail class for DAL BLL Ghistos BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 April 17th, 2007 01:04 AM
How to call normal "write" function inside a class jonen C++ Programming 3 May 25th, 2006 02:19 PM
How to write abstract class to xml haiying General .NET 4 September 21st, 2004 08:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.