New Posts New Posts RSS Feed: How can I make ORM Mapper to start using Nullable<> types in the generated code in case of an existing solution (which uses System.Objects)?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How can I make ORM Mapper to start using Nullable<> types in the generated code in case of an existing solution (which uses System.Objects)?

 Post Reply Post Reply
Author
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Topic: How can I make ORM Mapper to start using Nullable<> types in the generated code in case of an existing solution (which uses System.Objects)?
    Posted: 28-Aug-2013 at 4:24pm
Yes, it's the "Use Generics" switch which controls whether "Nullable<DateTime>" or "object" is used when the property allows nulls.  The default for this switch is true, so you might have an older model or you switched it off for some other reason.   If you're using .NET 4 then there's probably no reason you can't turn generics on for all your entities.
 
Also, all entity code except the "developer class" will be re-written when the entity is marked as modified in the ORM Designer.
Back to Top
ctoth View Drop Down
Newbie
Newbie
Avatar

Joined: 04-Mar-2013
Location: Fresno, CA
Posts: 22
Post Options Post Options   Quote ctoth Quote  Post ReplyReply Direct Link To This Post Posted: 28-Aug-2013 at 3:35pm
I'm looking at the IdeaBlade ORM source code. In our existing solution the "Use Generics"option is off. This maybe the cause. I'm looking at line 514 of EntityBaseclass_cs.template. The System.Object type getter is used if "<?includeif ../@useGenerics='false' ?>".
Back to Top
ctoth View Drop Down
Newbie
Newbie
Avatar

Joined: 04-Mar-2013
Location: Fresno, CA
Posts: 22
Post Options Post Options   Quote ctoth Quote  Post ReplyReply Direct Link To This Post Posted: 28-Aug-2013 at 1:57pm
Ok, I edited my post and clarified things.

In the Mapping Miscellanea section of the IdeaBlade DevForce Developers Guide:

"Properties of Nullable Data Columns

The data column that feeds a business object property might allow null values. The order's [ShipDate] won't have a value until the order is shipped so the database datetime column for the ship date allows nulls.

However, the .NET DateTime type is a value type. It can't be null. Only reference types can be null. Which means the order property corresponding to the ship date cannot be a DateTime type.

This was a serious problem in .NET 1.1. The predecessor to DevForce worked around that problem. The Object Mapper made the property an Object type and marked the property with an attribute that said it was a date. Unfortunately, it takes reflection to read an attribute. Developers had to write guard code to watch out for nulls and to make sure no one tried to set the ship date with something awful – like a string.

.NET 2.0 introduced nullable types. Now our programs can tell that ShipDate is supposed to be a DateTime and ensure at compile time that its value is either null or a DateTime. Our code is cleaner, clearer, and more testable which means it can be more reliable."




Edited by ctoth - 28-Aug-2013 at 1:59pm
Back to Top
ctoth View Drop Down
Newbie
Newbie
Avatar

Joined: 04-Mar-2013
Location: Fresno, CA
Posts: 22
Post Options Post Options   Quote ctoth Quote  Post ReplyReply Direct Link To This Post Posted: 28-Aug-2013 at 11:19am
If I look into our business objects we have two types of System.DateTime properties. One part of them are "Nullable" and "Source Nullable", the others are not nullable. The generated DataRow classes for the former ones contain raw System.Object properties:
    /// <summary>Gets or sets the ActivityDate.</summary>
    [DBDataType(typeof(System.DateTime))]
    public virtual Object ActivityDate {
      get { 
        Object result_;
        if (GetInterceptor<Object>("ActivityDate", GetActivityDateImpl, out result_)) return result_;
        return GetActivityDateImpl();
      }
      set { 
          if (!SetInterceptor<Object>("ActivityDate"value, SetActivityDateImpl)) {
             SetActivityDateImpl(value);
          }
      }    
    }
    private Object GetActivityDateImpl() {
      return GetColumnValue(ActivityDateColumn, typeof(System.DateTime), true); 
    }
    private void SetActivityDateImpl(Object value) {
      SetColumnValue(ActivityDateColumn, value);
    }
while the latter ones are typed:
    /// <summary>Gets or sets the DeletedDateTime.</summary>
    [DBDataType(typeof(System.DateTime))]
    public virtual System.DateTime DeletedDateTime {
      get { 
        System.DateTime result_;
        if (GetInterceptor<System.DateTime>("DeletedDateTime", GetDeletedDateTimeImpl, out result_)) return result_;
        return GetDeletedDateTimeImpl();
      }
      set { 
          if (!SetInterceptor<System.DateTime>("DeletedDateTime"value, SetDeletedDateTimeImpl)) {
            SetDeletedDateTimeImpl(value);
          }  
      }    
    }
    private System.DateTime GetDeletedDateTimeImpl() {
      return (System.DateTime) GetColumnValue(DeletedDateTimeColumn, typeof(System.DateTime), false); 
    }
    private void SetDeletedDateTimeImpl(System.DateTime value) {
      SetColumnValue(DeletedDateTimeColumn, value);
    }
All these are generated. As far as I know the reason for this is that in early version of .NET there weren't nullable types, and DateTime is a value type. Nowadays if we create a new solution and new business objects if we map a DateTime column which is nullable in the DB, then IdeaBlade generates nullable C# datetime (using the Nullable<System.DateTime> notation AFAIK). Same with othe value types, a nullable integer in the DB will be mapped to Nullable<Int32>. However, our existing solution keeps using the old methods, RAW object types.
Our big solution used .NET 3.5 for a while, and now we use .NET 4.0. So we could happily take advantage of nullable types in C#.

My questions:
1. Is there a switch somewhere, which could trigger IdeaBlade ORM mapper to use the Nullable<> notation in our existing solution?
2. Or can I mark/trigger certain fields in the ORM Relationship Mapper tool so they get regenerated into Nullable<System.DateTime>? (This way I could "fix" the raw objects to become System.DateTime).



Edited by ctoth - 28-Aug-2013 at 3:28pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down