New Posts New Posts RSS Feed: FixedLength fields
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

FixedLength fields

 Post Reply Post Reply
Author
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Topic: FixedLength fields
    Posted: 27-Dec-2010 at 12:14pm
Is there a way to determine if a property has the FixedLength=True set at run time?

<Property Name="StatusCode" Type="String" Nullable="false" MaxLength="6" Unicode="false" FixedLength="true" />

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 27-Dec-2010 at 4:52pm
mikewishart;

Unfortunately there is no easy way to do that. What are you trying to achieve?

Here are some suggestions:


or

2. Write your own custom T4 template for code generation.
Back to Top
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Posted: 27-Dec-2010 at 5:02pm
Hi Dennis,

Thanks for the link.  I'll look at it.  Trying to write a save event handler on the entitymanager which trims leading/trailing characters of all the varchar() fields and Nulls those that support it.  Just a generic safety.  The problem is that some of the fields are char() and shouldn't be trimmed.

for example... right now I have any string[6] hardcoded to not be trimmed...  Not elegant but functional for our application.

    private static void TrimStringProperties(IEnumerable<object> entities)
    {
      foreach (Entity entity in entities)
      {
        var properties = entity.GetType().GetProperties().Where(p => p.PropertyType == typeof(string));
        foreach (var property in properties)
        {
          var attributeNames = property.GetCustomAttributes(false).Select(a => a.ToString()).ToList();
          if (!attributeNames.Any(a => a.Contains("DataMemberAttribute")))
            continue;
          var isNullable = ((IBaseEntity)entity).IsNullable(property.Name);
          var maxLength = ((IBaseEntity)entity).MaxStringLength(property.Name);
          if (maxLength == 6) continue; // fixed length char(6) properties

          var value = (string)property.GetValue(entity, null);
          if (value == null) continue;

          value = value.Trim();
          if (isNullable && string.IsNullOrEmpty(value))
            value = null;
          property.SetValue(entity, value, null);
        }
      }
    }

Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 27-Dec-2010 at 7:28pm
I see. 

Another suggestion is to do the trimming on a varchar on the SQL database. You can use SQL_Variant_PROPERTY function to determine the field type and LTrim or RTrim function to do the trimming.

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down