The ObjectMapper .NET Project

Official blog of the AdFactum ObjectMapper .NET

Archive for April, 2008

Exception: WrongTypeException

Posted by Gerhard Stephan on 21st April 2008

The WrongTypeException will always be thrown if your object implements the ICreateObject interface, but does not create the expected object type as the result value.

Here’s an example for this:

    /// <summary>

    /// This class creates the wrong object type and causes the WrongTypeException

    /// </summary>

    public class WrongType : BaseVO, ICreateObject

    {

        private int number;

 

        /// <summary>

        /// Gets or sets the number.

        /// </summary>

        /// <value>The number.</value>

        [PropertyName("IntNumber")]

        public int Number

        {

            get { return number; }

            set { number = value; }

        }

 

        /// <summary>

        /// Creates the new object.

        /// </summary>

        /// <returns></returns>

        public IValueObject CreateNewObject()

        {

            return new NullValue();

        }

    }

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »

Exception: TransactionAlreadyOpenException

Posted by Gerhard Stephan on 21st April 2008

The TransactionAlreadyOpenException will always be thrown if you try to open an already open transaction a second time. In a simple example this exception can be forced with the following code.

            using (AdFactum.Data.ObjectMapper mapper = OBM.CreateMapper(Connection))

            {

                try

                {

                    mapper.BeginTransaction();

                    mapper.BeginTransaction(); // Second try causes the exception

                }

                catch(Exception)

                {

                    mapper.Commit();

                    throw;

                }

            }

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »

Exception: SqlCoreException

Posted by Gerhard Stephan on 21st April 2008

The SqlCoreException is the Exception you’ll get most times when working with the ObjectMapper .NET. The SqlCoreException wraps the ADO .NET Exceptions and offers a common interface for catching those exceptions.

Due to this, you don’t have to catch OleDBException, SqlException and OracleException. You only have to catch the SqlCoreExceptions. Thus makes it easy to handle database failures in different scenarios.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »

Exception: PersisterDoesNotSupportRepositoryException

Posted by Gerhard Stephan on 17th April 2008

The PersisterDoesNotSupportRepositoryException will be thrown by the XmlPersister if you try to access the Repository of the ObjectMapper class.

The Repository interface is especially designed for use with relational databases and therefore not accessible when using the XmlPersister.

      string file = Directory.GetCurrentDirectory() + "\\XmlTest.xml";

      XmlPersister persister = new XmlPersister("XmlTest", file);

      AdFactum.Data.ObjectMapper xmlMapper = new AdFactum.Data.ObjectMapper(new UniversalFactory(), persister, Transactions.Manual);

 

            IRepository repository = xmlMapper.Repository;

 

 

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »

Exception: NoOpenTransactionException

Posted by Gerhard Stephan on 17th April 2008

The NoOpenTransactionException will always be thrown if a Save-Method is called without opening a Transaction first.

        [Test]

        [ExpectedException(typeof(NoOpenTransactionException))]

        public void TestNoOpenTransactionException()

        {

            Buying buying = new Buying(5, "Hotdogs");

 

            using (AdFactum.Data.ObjectMapper mapper = OBM.CreateMapper(Connection))

            {

                // A NoOpenTransactionException will be thrown, because no transaction has been opened.

                mapper.Save(buying);

            }

        }

To prevent this exception you have to call the BeginTransaction Method. It’s recommended to use the BeginTransaction of the OBM Helper class in order to handle nested transactions.

                nested = OBM.BeginTransaction(mapper);

                mapper.Save(buying);
 
               OBM.Commit(mapper, nested);

 

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »

Exception: MissingSetterException

Posted by Gerhard Stephan on 17th April 2008

The MissingSetterException will be thrown if a value object shall be loaded that has a property without a setter.

    /// <summary>

    /// This is an uncomplete value object that throws a missing setter exception

    /// </summary>

    public class MissingSetter : ValueObject

    {

        private string missing = string.Empty;

 

        public string Missing

        {

            get { return missing; }

        }

    }

The following test will throw the MissingSetterException when trying to load the object.

        [Test]

        [ExpectedException(typeof(MissingSetterException))]

        public void TestMissingSetterException()

        {

            MissingSetter missingSetter = new MissingSetter();

 

            using (AdFactum.Data.ObjectMapper mapper = OBM.CreateMapper(Connection))

            {

                // First save

                bool nested = OBM.BeginTransaction(mapper);

                mapper.Save(missingSetter);

                OBM.Commit(mapper, nested);

 

                // The Load throws the MissingSetterException, because the value can’t be set to the object.

                ObjectDumper.Write(mapper.Load(typeof (MissingSetter), missingSetter.Id));

            }

        }

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »

Exception: DirtyObjectException

Posted by Gerhard Stephan on 17th April 2008

The DirtyObjectException will be thrown if an object is changed by another user meanwhile the object is in use by the first user. This Optimistic Locking feature is used for objects that are derived from MarkedValueObject or MarkedAutoIncValueObject.

To evaluate the feature you can force the ObjectMapper .NET to throw a DirtyObjectException.

        [Test]

        [ExpectedException(typeof(DirtyObjectException))]

        public void TestDirtyObjectException()

        {

            Buying buying = new Buying(5, "Hotdogs");

           

            using (AdFactum.Data.ObjectMapper mapper = OBM.CreateMapper(Connection))

            {

                // First save

                bool nested = OBM.BeginTransaction(mapper);

                mapper.Save(buying);

                OBM.Commit(mapper, nested);

 

                // Simulate, that an other user has changed the object

                buying.Count = 3; // the object has to be changed

                buying.LastUpdate = buying.LastUpdate.AddHours(-1);

 

                // Second save, tend to fail

                nested = OBM.BeginTransaction(mapper);

                mapper.Save(buying);

                OBM.Commit(mapper, nested);

            }

        }

Normaly you definitly would’nt change the LastUpdate Property. That’s only for showing when the DirtyObjectException will be thrown. So let us have a look at the created SQL Statement.

UPDATE [BUYING]        SET [LASTUPDATE] = CONVERT (DATETIME, '2008-04-17 20:07:46'), [COUNT] = 3
 WHERE [BUYING].[ID]=1 AND [LASTUPDATE] = CONVERT (DATETIME, '2008-04-17 19:07:46');

As you can see, the WHERE Clause contains a check condition that fails, if the object has been modified meanwhile it is in use by the current user. If that happens the DirtyObjectException will be thrown to indicate that the object has been changed.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Posted in Exceptions | No Comments »