Derived Class Problem

Posts   
 
    
Dusty
User
Posts: 24
Joined: 19-Feb-2008
# Posted on: 20-Feb-2008 00:12:18   

I am trying to create a wrapper class for an entity class. Everything works fine until I try to get a collection.

When I run this code:

        public IList<User> GetUsers()
        {
            try
            {
                this.SetConnection();
                EntityCollection<User> users = new EntityCollection<User>();
                this._DataAccessAdapter.FetchEntityCollection(
                    users,
                    null,
                    0
                    );
                return users;
            }
            catch (Exception exception)
            {
                throw new Exception(
                    "An error occured while getting all users.\nCheck inner exception.",
                    exception
                    );
            }
        }

I get this error: "entityToAdd isn't of the right type"

I realize what the problem is and why it it happening.

How do I fix it?

I have searched the forum and have found similar threads but not an answer that does not involve reading the SDK documentation (which I don't have).

Here is the entire class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ABS.BusinessObjects.EntityClasses;
using ABS.BusinessObjects.DatabaseSpecific;
using ABS.BusinessObjects.HelperClasses;
using System.Data;
using ABS.BusinessObjects.FactoryClasses;

namespace BusinessObjects
{
    public class User : UserEntity
    {
        private string _ConnectionString;

        public string ConnectionString
        {
            get
            {
                return this._ConnectionString;
            }
            set
            {
                this._ConnectionString = value;
            }
        }

        private DataAccessAdapter _DataAccessAdapter = null;

        public DataAccessAdapter DataAccessAdapter
        {
            get
            {
                return this._DataAccessAdapter;
            }
            set
            {
                this._DataAccessAdapter = value;
            }
        }

        public User()
            : base()
        {
        }

        public User(
            string connectionString
            )
            : base()
        {
            this._ConnectionString = connectionString;
        }

        public User(
            string connectionString,
            string userID
            )
            : base(userID)
        {
            this._ConnectionString = connectionString;
        }

        ~User()
        {
            if (this.DataAccessAdapter != null)
            {
                this.DataAccessAdapter.CloseConnection();
                this.DataAccessAdapter.Dispose();
                this.DataAccessAdapter = null;
            }
        }

        public void SetConnection()
        {
            if (this._DataAccessAdapter == null)
            {
                this._DataAccessAdapter = new DataAccessAdapter(this._ConnectionString);
            }
        }

        public IList<User> GetUsers()
        {
            try
            {
                this.SetConnection();
                EntityCollection<User> users = new EntityCollection<User>();
                this._DataAccessAdapter.FetchEntityCollection(
                    users,
                    null,
                    0
                    );
                return users;
            }
            catch (Exception exception)
            {
                throw new Exception(
                    "An error occured while getting all users.\nCheck inner exception.",
                    exception
                    );
            }
        }

        public void Save()
        {
            try
            {
                this.SetConnection();
                this._DataAccessAdapter.StartTransaction(
                    IsolationLevel.ReadUncommitted,
                    "SaveUser"
                    );
                this._DataAccessAdapter.SaveEntity(this, true);
                this._DataAccessAdapter.Commit();
            }
            catch (Exception exception)
            {
                if ((this._DataAccessAdapter != null) &&
                    (this._DataAccessAdapter.IsTransactionInProgress))
                {
                    this._DataAccessAdapter.Rollback();
                }

                throw new Exception(
                    "An error occured while saving user.",
                    exception
                    );
            }
        }

        public void Delete()
        {
            try
            {
                this.SetConnection();
                this._DataAccessAdapter.StartTransaction(
                    IsolationLevel.ReadUncommitted,
                    "DeleteUser"
                    );
                this._DataAccessAdapter.DeleteEntity(this);
                this._DataAccessAdapter.Commit();
            }
            catch (Exception exception)
            {
                if ((this._DataAccessAdapter != null) &&
                    (this._DataAccessAdapter.IsTransactionInProgress))
                {
                    this._DataAccessAdapter.Rollback();
                }

                throw new Exception(
                    "An error occured while deleting user.",
                    exception
                    );
            }
        }
    }
}


Dusty
User
Posts: 24
Joined: 19-Feb-2008
# Posted on: 20-Feb-2008 04:13:16   

I have gotten it to work by overriding the Factory class.

Could someone look at what I did and make sure that it will not hurt me later?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ABS.BusinessObjects.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using ABS.BusinessObjects;

namespace BusinessObjects
{
    [Serializable]
    public partial class UserFactory : EntityFactoryBase2
    {
        public UserFactory() 
            : base("UserEntity", EntityType.UserEntity) 
        { 
        }

        public override IEntity2 Create()
        {
            IEntity2 toReturn = new User();
            return toReturn;
        }

        public override IEntity2 Create(IEntityFields2 fields)
        {
            IEntity2 toReturn = new User(fields);

            return toReturn;
        }
    }
}

I also had to change my User class to use:

EntityCollection<User> users = new EntityCollection<User>();

instead of

EntityCollection<User> users = new EntityCollection<User>();
Dusty
User
Posts: 24
Joined: 19-Feb-2008
# Posted on: 20-Feb-2008 04:14:59   

And I had to add to constructors.

        public User(IEntityFields2 fields)
            : base(fields)
        {
        }

        public User(
            string connectionString,
            IEntityFields2 fields)
            : base(fields)
        {
            this._ConnectionString = connectionString;
        }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Feb-2008 11:10:11   

Your code looks fine.

Except that I don't see a difference in the following:

I also had to change my User class to use: Code: EntityCollection<User> users = new EntityCollection<User>();

instead of Code: EntityCollection<User> users = new EntityCollection<User>();

Dusty
User
Posts: 24
Joined: 19-Feb-2008
# Posted on: 20-Feb-2008 14:33:48   

Your code looks fine.

Except that I don't see a difference in the following: Quote: I also had to change my User class to use: Code: EntityCollection<User> users = new EntityCollection<User>();

instead of Code: EntityCollection<User> users = new EntityCollection<User>();

Sorry. I meant to change it to:

EntityCollection<User> users = new EntityCollection<User>(new UserFactory());
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Feb-2008 14:49:51   

Alright, then from what I see I think your code is fine simple_smile

Dusty
User
Posts: 24
Joined: 19-Feb-2008
# Posted on: 20-Feb-2008 15:23:29   

Awesome.

Thanks. sunglasses