Search
Archives

You are currently browsing the ZMeier.com blog archives for January, 2012.

Archive for January, 2012

This article explains how to create custom serialization and deserialization events without having to inherit iSerializable.
This allows you to skip serialization on a field and then fill it in upon deserialization.


[XmlIgnore()]


class SomeClass {
    [XmlIgnore] int someID;
    public string someString;
}


Serializing Events


The gist is put these items before an property:

  • [Serializable()]
  • [NonSerialized()]

And these are events that get called by serialization: (place in front of methods)

  • [OnSerializing()]
  • [OnSerialized()]
  • [OnDeserializing()]
  • [OnDeserialized()]

Method structure:     internal void customMethod(StreamingContext context){}


Example Serializing Events

Example code from Microsoft:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

// This is the object that will be serialized and deserialized.
[Serializable()]
public class TestSimpleObject
{
    // This member is serialized and deserialized with no change.
    public int member1;
    // The value of this field is set and reset during and
    // after serialization.
    private string member2;
    // This field is not serialized. The OnDeserializedAttribute
    // is used to set the member value after serialization.
    [NonSerialized()]
    public string member3;
    // This field is set to null, but populated after deserialization.
    private string member4;
    // Constructor for the class.
    public TestSimpleObject()
    {
        member1 = 11;
        member2 = "Hello World!";
        member3 = "This is a nonserialized value";
        member4 = null;
    }
    public void Print()
    {
        Console.WriteLine("member1 = '{0}'", member1);
        Console.WriteLine("member2 = '{0}'", member2);
        Console.WriteLine("member3 = '{0}'", member3);
        Console.WriteLine("member4 = '{0}'", member4);
    }
    [OnSerializing()]
    internal void OnSerializingMethod(StreamingContext context)
    {
        member2 = "This value went into the data file during serialization.";
    }
    [OnSerialized()]
    internal void OnSerializedMethod(StreamingContext context)
    {
        member2 = "This value was reset after serialization.";
    }
    [OnDeserializing()]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        member3 = "This value was set during deserialization";
    }
    [OnDeserialized()]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        member4 = "This value was set after deserialization.";
    }
}


Example Serializing Events Output



// Output:
// Before serialization the object contains:
// member1 = '11'
// member2 = 'Hello World!'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
// After serialization the object contains:
// member1 = '11'
// member2 = 'This value was reset after serialization.'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
// After deserialization the object contains:
// member1 = '11'
// member2 = 'This value went into the data file during serialization.'
// member3 = 'This value was set during deserialization'
// member4 = 'This value was set after deserialization.'


Conclusions


With these methods you can fully regualte how things are serialied:

  • How do I serialize a property but clear it upon deserialization?  Serialze property but dont deserialize?
    • Keep the attribute as normal in the serializeable class,  in the [OnDeserialized()] method clear out the value
  • How do exclude  a property from serialization but fill it in on deserialization?  Don’t serialze property but load/fill/set it on deserialize?
    • Set the attrbiute as [NonSerialized()]  in the serializeable class,  in the [OnDeserialized()] method clear set it, based on some information form serialization.

My goal was to figure out the difference between two DateTime objects, more specifically how many days form a specific date.  I Originally searched for DateTime.CompareTo hoping that would be the answer, however DateTime.CompareTo returns (-1, 0, 1) (Less Then, Equal To, Greater Then) respectivly.


Code


 

DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds( 75 );


TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );


Output




Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0


Other TimeSpan Options



Only a couple worth mentioning: 
Methods:

Public method Supported by the XNA Framework Supported by Portable Class Library Add Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.
Public method Supported by the XNA Framework Supported by Portable Class Library CompareTo(Object) Compares this instance to a specified object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the specified object.
Public method Supported by the XNA Framework Supported by Portable Class Library Subtract Returns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Converts the value of the current TimeSpan object to its equivalent string representation. (Overrides ValueType.ToString.)   [-][d.]hh:mm:ss[.fffffff]  where – if you want to show if there is a negative time span, d = days, and fffffff is fraction seconds.

 

Properties:

Public property Supported by the XNA Framework Supported by Portable Class Library Days Gets the days component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Hours Gets the hours component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Milliseconds Gets the milliseconds component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Minutes Gets the minutes component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Seconds Gets the seconds component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Ticks Gets the number of ticks that represent the value of the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalDays Gets the value of the current TimeSpan structure expressed in whole and fractional days.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalHours Gets the value of the current TimeSpan structure expressed in whole and fractional hours.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalMilliseconds Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalMinutes Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalSeconds Gets the value of the current TimeSpan structure expressed in whole and fractional seconds.

View all options here: http://msdn.microsoft.com/en-us/library/system.timespan.aspx

References:

http://www.dotnetspider.com/resources/458-How-find-difference-between-two-Dates-C-or.aspx
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
http://msdn.microsoft.com/en-us/library/1ecy8h51.aspx
http://msdn.microsoft.com/en-us/library/system.datetime.compareto.aspx

Todo: Proof read and fix the bullet point hierarchy  ( I put this up to help as a quick guide, because its information I needed fast )

Visual Studio 2010 has made it very easy to publish files right the virtualized or ghosted directory structure of SharePoint 2010.

While this was possible with SharePoint 2007 and Windows SharePoint Services 3.0 (WSS 3.0) it was quite a pain in Visual Studio 2008.

SharePoint Architects, Consultants and/or Managers

The first part of this article is written with you in mind. It contains information needed to make decisions on long term supportability, security, and cost.

SharePoint Developers

If you are a developer making this decision use _layouts.  It’s easier to develop against as well as easier to support code wise.

  • I have had Visual Studio 2010 Corrupt a Module solution,  in this situation start over and copy your files to the new solution.

You will find that in most cases _layouts is the way to go from both perspectives.

Debunking the myths:

  • Both the _layouts folder and Modules based solutions publish a Dynamic Link Library (DLL) to the Global Assembly Cache (GAC)
  • Both solutions are Standard SharePoint 2010 Solutions ( while _layouts is the more preferred/suggested method for custom code )
  • Polution of the _layouts directory ( i.e. name conflicts between custom solutions ) is easily averted by the use of namespaces.  ( i.e. /_layouts/namespace/custom solution )

The big question: Modules or _layouts

The answer depends on your customers needs:

Hard/Easy – Fast/Slow – Cheaper/More Expensive

  • Easy Faster Cheaper Standard Solution = _layouts
    • Less development time
  • Harder Slower More Expensive but fully limited to a single site = Module
    • So why module?  For the right small solution this is the way to go.
    • Ability to edit the aspx in SharePoint Designer ( Good and Bad ), in some cases allow customer to make quick changes.
    • Some claim that this is more secure, I can break it down, but the basic answer is no its not.
      • Its the same to less secure, because now you can change the page in designer for one.

SharePoint Configuration Breakdown

  • Single Farm / Single Site = _layouts
    • I would say just for ease of use for developer and administrator
  • Multi Farm / Multi Site
    • Multisite Solution = _layouts
      • This is a custom solution that is used by multiple SharePoint sites and Sub-Sites
      • Absolutely,  this is the best possible solution both for Developers and Admins
      • Since all sites potentially have access to the _layouts directory this allows for the ability for multiple sites to access the same code and thus single deployment for multiple sites, single update for multiple sites, and single retract.
      • Two common concerns with this:
        • Security & Access Restriction:  This can successfully be handled by a list in the base site that all sub-site custom apps use to verify access,  this as well as both Claims based and Active Directory based user access can be utilized to restrict on a user basis. That is to say you can very easily secure access.
        • Polution of _layouts director or name conflicts: This was previously stated, but the simple solution is a sub-directory a.k.a namespace.   ( i.e. /_layouts/namespace/custom solution )
    • Single Site Simple Solution = Modules
      • Limits to that specific sites virtualized/ghosted directory  structure ( single site )
      • Allows for quick customer reaction changes though SharePoint Designer 2010
    • High Demand Solutions = _layouts
      • Because Modules deploy to the virtualized/ghosted directory structure this means that the page will need to be completely generated on the fly.  Meaning your pages will have a build lag and a possibly a database lag.
      • Many times a lot of solutions are deployed to one server, add this all together with the lab above and you start to get really slow.
      • _layouts solutions are actual real files, not generated, and are not subject to the two previous lags

Coming Soon: How-To Convert you _layouts solution to a vitrualized / ghosted solution a.k.a Module Solution

Coming Soon: How-To create a custom solution that resides in the virtual / ghosted directory structure.  Otherwise stated How-To Create a Module Solution

 

I will add the tutorial to this article soon,  I don’t get paid to write this stuff, so please contact me if you need the solution faster.

Sorting if flash AS3 is now easy!


Sort Array of Strings


Array.sort(); //Easy Alpha

Array.sort(compareFunction);

function compareFunction(a:String, b:String){ }

  • Negative = A before B
  • Zero = Same
  • Positive = B before A

Array.sort(options);

  • 1 or Array.CASEINSENSITIVE
  • 2 or Array.DESCENDING
  • 4 or Array.UNIQUESORT
  • 8 or Array.RETURNINDEXEDARRAY
  • 16 or Array.NUMERIC

Array.Sort(CompareFunciton, Options);


Sort Array of Objects


Array.sortOn(fieldName); //Easy Alpha

Array.sort(compareFunction);

function compareFunction(a:Object, b:Object){ }

  • Negative = A before B
  • Zero = Same
  • Positive = B before A

Array.sortOn(fieldName, options);

  • 1 or Array.CASEINSENSITIVE
  • 2 or Array.DESCENDING
  • 4 or Array.UNIQUESORT
  • 8 or Array.RETURNINDEXEDARRAY
  • 16 or Array.NUMERIC


 

This sites primary function is to be a dynamic resume,  click the navigation at the top to view my skill-set and get my contact information.  I plan to keep this site up to date with my skills/education/programs as they develop.

 

I’ve debated over weather or not to create this site into a blog based site.  Recently I turned the blog feature back on.

This site is a simple re-development of ZMeier.com with a more professional look and feel.  ZMeier.com is a base site for all my development and serves as a extended resume for my skills, ideals, and a place to share lessons learned and free programs.

I hope to take advantage of the blog feature and actually post useful information to it.

I will upload my first programs to the site late today or tomorrow.