The ObjectMapper .NET Project

Official blog of the AdFactum ObjectMapper .NET

Exception: WrongTypeException

Posted by Gerhard Stephan on April 21st, 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();

        }

    }

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

Exception: TransactionAlreadyOpenException

Posted by Gerhard Stephan on April 21st, 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;

                }

            }

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

Exception: SqlCoreException

Posted by Gerhard Stephan on April 21st, 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.

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

Exception: PersisterDoesNotSupportRepositoryException

Posted by Gerhard Stephan on April 17th, 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;

 

 

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

Exception: NoOpenTransactionException

Posted by Gerhard Stephan on April 17th, 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);

 

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

Exception: MissingSetterException

Posted by Gerhard Stephan on April 17th, 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));

            }

        }

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

Exception: DirtyObjectException

Posted by Gerhard Stephan on April 17th, 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.

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Exceptions | No Comments »

New Release - AdFactum ObjectMapper .NET 2.3.2619.0

Posted by Gerhard Stephan on March 19th, 2008

This Release is a pure bug fixing Realease. It covers three major bug fixes.

At first, it fixes a memory leak that occures when calling the Method "BaseCache.ClearAllCaches". Because this is not obvious for all users, I have to explain that this method has been called every time the ObjectMapper class has been initialized with a version information greater than zero.

The second fix is a multithreading bug that causes the mapper to throw unmotivated Exceptions, like "NoPrimaryKeyFound" Exception and "CollectionHasBeenModified" Exception. But this only occured when using the ObjectMapper .NET in a strongly multithreaded environment.

The last fix, fixed the update and delete sql statements of dictionary types (e.g. SortedList and Hashtable). Due to that the link table had a wrong primary key definition and that caused the update of the link table to fail.

Hope you enjoy the new version.

Cheers
- Gerhard

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Releases | No Comments »

Attribute: [Unicode]

Posted by Gerhard Stephan on March 7th, 2008

For all databases that supports Unicode (e.g. Oracle and Microsoft SQL Server),  a string property can be tagged with the [Unicode] attribute. The AdFactum ObjectMapper .NET does therefore use the Unicode Data Types for the specific property.

Below you’ll find all supported datatypes that can be used with the Unicode attribute.

    public class UnicodeTestEntity : ValueObject

    {

        private char unicodeChar;

        private string unicodeString;

        private string unicodeMemo;

 

        /// <summary>

        /// Gets or sets the unicode char.

        /// </summary>

        /// <value>The unicode char.</value>

        [Unicode]

        public char UnicodeChar

        {

            get { return unicodeChar; }

            set { unicodeChar = value; }

        }

 

        /// <summary>

        /// Gets or sets the unicode string.

        /// </summary>

        /// <value>The unicode string.</value>

        [Unicode]

        [PropertyLength(30)]

        public string UnicodeString

        {

            get { return unicodeString; }

            set { unicodeString = value; }

        }

 

        /// <summary>

        /// Gets or sets the unicode memo.

        /// </summary>

        /// <value>The unicode memo.</value>

        [Unicode]

        [PropertyLength(int.MaxValue)]

        public string UnicodeMemo

        {

            get { return unicodeMemo; }

            set { unicodeMemo = value; }

        }

    }

 

 

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Attributes | No Comments »

Attribute: [GroupBy]

Posted by Gerhard Stephan on March 7th, 2008

The [GroupBy] attribute can be used to group properties by there value. This property can only be used in projection classes.

    /// <summary>

    /// Groups the time entries by their project id

    /// </summary>

    public class TimeEntryGrouping

    {

        private DateTime minDate;

        private string project;

 

        /// <summary>

        /// Gets or sets the min date.

        /// </summary>

        /// <value>The first date.</value>

        [ProjectOntoProperty(typeof(TimeEntry), "StartDate")]

        [Min]

        public DateTime MinDate

        {

            get { return minDate; }

            set { minDate = value; }

        }

 

        /// <summary>

        /// Gets or sets the project.

        /// </summary>

        /// <value>The project.</value>

        [ProjectOntoProperty(typeof(TimeEntry), "ProjectId")]

        [GroupBy]

        public string Project

        {

            get { return project; }

            set { project = value;  }

        }

    }

 

Select the grouping:

 

                List<TimeEntryGrouping> result =

                    new List<TimeEntryGrouping>(

                        new ListAdapter<TimeEntryGrouping>(

                    mapper.FlatSelect(typeof(TimeEntryGrouping))));

 

 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (No Ratings Yet)
Loading ... Loading ...


Posted in Attributes | No Comments »