Need an ISmartField interface

Need an ISmartField interface

Old forum URL: forums.lhotka.net/forums/t/2890.aspx


david.wendelken posted on Wednesday, May 16, 2007

Hey Rocky!

Some of us have created classes emulating SmartDate for other datatypes (such as SmartInt16, SmartInt32, SmartInt64, SmartFloat, SmartBool, etc.).

I've created an interface to assign to all the locally created Smart structs.  It's called ISmartField.

It makes it much easier (for me at least!) to write generic rule libraries that deal with all the different Smart fields.

 

It would be really nice if the ISmartField interface was added to CSLA and SmartDate implemented it.

The interface doesn't actually have to do anything - it just lets us know we can common Smart struc methods like Parse, DBValue, etc.

I know it's possible to do this other ways, but this seems to be a really pragmatic, productive way to do this.  (Of course, maybe I just need to learn yet another new trick.)

using System;
using System.Collections.Generic;
using System.Text;

namespace Csla
{

/// <summary>
/// All classes that resemble SmartDate implement this interface.
///

/// Sadly, the methods and properties we actually need to specify in this interface
/// cannot be specified, because:
///
/// a) one does not specify properties in an interface, and
/// b) one does not specify static methods in an interface.
///

/// So, this interface mostly lets other code know whether it is dealing with one of the Smart family of
/// data structures. The key methods and properties must be invoked via reflection.
/// </summary>
public interface ISmartField
{
}

}

 

 

What do others think?

DansDreams replied on Thursday, May 17, 2007

What need have you found for SmartInt32 that isn't solved by the .NET framework's nullable int? 

I have found them adequate and quite useful, especially with the null-aware DevExpress input controls.  I haven't seen a need to even use SmartDate any more.

Brian Criswell replied on Thursday, May 17, 2007

How do you handle cases where null is the maximum date?

DansDreams replied on Thursday, May 17, 2007

Brian, was your question to me?

If so, I don't know what you mean.  All that max/min date stuff in SmartDate was created to solve the problem of DateTime being a value type, and therefore not nullable, which means there was no way to preserve a true null from the database into the BO and back to the database.

DateTime? accomplishes exactly that.

What more is necessary?

Brian Criswell replied on Thursday, May 17, 2007

The classic example is sorting start dates vs sorting end dates.  For start dates a null start date would be early than a filled in start date.  This would fit in with how I understand DateTime? to sort.  However, for end dates you would want a null end date to be later than a filled in end date.  How would this work?

david.wendelken replied on Thursday, May 17, 2007

Brian Criswell:
The classic example is sorting start dates vs sorting end dates.  For start dates a null start date would be early than a filled in start date.  This would fit in with how I understand DateTime? to sort.  However, for end dates you would want a null end date to be later than a filled in end date.  How would this work?

Exactly!  Having a class that treats an empty value as the lowest or highest possible value is incredibly handy.  That's one of the two reasons I want SmartInt16, SmartDecimal, etc.  I sometimes need to deal with the same problem in those data types.

The other reason has to do with mental bandwidth.  I have enough to learn as it is. Consistently using SmartDates (etc.) for nulls and for min/max handling is simpler because it is one less thing to have to learn just to get a program out the door. :)   And that learning curve has to be paid by every programmer on the team, so reducing learning curve costs is important.

 

RockfordLhotka replied on Friday, May 18, 2007

I did add this idea to the wish list.

DansDreams replied on Friday, May 18, 2007

Interesting.  One of the reasons I prefer using the DateTime? instead of the SmartDate is because of the learning curve issue, which is approximately nothing in my case IMO.

The sorting issue has not bit me, but that's probably because many of the DevExpress tools seem to have their own sortable wrapper functionality, and I haven't had a specific case of needing to sort a list will nulls.

Good knowledge to have though.  Thanks for esplainin' Lucy.

JTWebMan replied on Thursday, March 20, 2008

Actually I think the reason would be because you can handle user input as string and can set the format of the output all in the business objects using a factory object. This saves from having to set it in the UI code. You also can allow the user to enter in codes like 'm' for Integer.MaxValue or 'mi' for Integer.MaxValue and could then change thing to the integer values. Date time can take advantage of this more adding ’t’ for today, 'y' for yesterday, etc... Also if you had a Float and Double one you could set the format as well which could help out a lot! I only wish Rocky would have written a Smart Field for each value type. It would have saved us time on writing them ourselves. I also wrote my own interface for it and for smart date ended up using copy paste inheritance.

I have debated on refactoring it using Nullable(Of Type) and just writing the convert from and to logic into a shared class. This seems more logical to me then maintaining all these structures, especially now that Nullables are full blown in VB.net 9.0.


RockfordLhotka replied on Thursday, March 20, 2008

I did add the ISmartField interface in 3.5.

I also radically enhanced SmartDate with some type conversion capabilities that make it much nicer to work with. But if you look at the code, it totals up to quite a lot - so I'm sure not looking to wrap every primitive .NET type in a class like that Smile [:)]

JTWebMan replied on Monday, April 07, 2008

I did notice the the CSLA Contrib project does have a few others implemented. I looked into making one for a few different type but defiantly not all of them. It does seem like a ton of work but work that could pay off. I can see it being useful for Decimals, being able to set the places past the decimal to display, as well as the empty being max or min for sorting.

If I built a few could we add them to the framework? How do you deal with having empty Decimals, Integers, etc.. in you application? Do you not make them strings in your object like you do for smart dates or just do the conversion and validate in the UI code? Also now that LINQ is in CSLA it would be nice that the sorting when some of these fields are empty sorting how you want as null able types act different depending on the type and if its VB.net or C#. This was one of the big selling points for me to start using the smart date.

Thanks for adding the interface!

JT

Copyright (c) Marimer LLC