More comfort reading values from lines in CSV-files

Today I had to read values from multiple CVS-files. I wanted to build a testable and reusable solution instead of the ugly sequential spaghetti that easily happens. So here is a sample of a line: 1 var lineReadFromCsv = 2 "42;Daniel;Fisher;1980-03-25;{79704C0D-1A4F-4DDD-80F6-CA79E81BF7CD}"; To increase the readability I created enumerations that point to the index of the position of the values in each line that I wanted to read: 1 public enum DataFormatField 2 { 3 Id = 0, 4 GivenName = 1, 5 Surname = 2, 6 DateOfBirth = 3, 7 UniqueId = 4, 8 } I created a class called ParserParameter that contains enums that 1 public class ParserParameter 2 { 3 prop Enum FirstName { get; set; } 4 //... 5 } 6 Last but not least an extension class the provides a generic method TryGetValueOrDefault<T>(). It takes an enum member as parameter. It can convert int, long, float, decimal, double, guid, datetime as well as string and can easily be extended. 1 int id; 2 if (fields.TryGetValueOrDefault( 3 DataFormatField.Id, 4 out id)) 5 { 6 Console.WriteLine(id); 7 } 8 9 string givenName; 10 if (fields.TryGetValueOrDefault( 11 DataFormatField.GivenName, 12 out givenName)) 13 { 14 Console.WriteLine(givenName); 15 } 16 The complete code can be downloaded at forkcan.

Project Springboard

Am 5. Juni war ich als Sprecher beim “Project Springboard” zu Gast. Dennis Zielke und das Student Partner Team haben ganze Arbeit geleistet und ein Spitzen Event auf die Beine gestellt – nochmal nochmal nochmal! Mein Vortrag “IIS, PHP & WCF – Web Services InterOp” hat wirklich Laune gemacht und ist laut Feedback bester Vortrag der Konferenz – DANKE, IHR WARD EINE SUPER AUDIENCE!!!   Hier nun wie versprochen das Slide und der Code (PHP gehostet über FastCGI im IIS 7.0 ruft über SSL und Basic Athentication einen WCF Service mit einer Complex-SOAP-Message auf…): springbreak_IIS_PHP_und_WCF.zip

Simplify LINQ code?

A few days ago a developer at a customer asked me how he could simplify the following code as he identified a pattern: X tries and the error handling. public PlanungsGruppenLesenA PlanungsGruppenLesen(     PlanungsGruppenLesenF param) {     PlanungsGruppenLesenA ret = new PlanungsGruppenLesenA();       if (ret.PlanungsGruppen == null)     {         throw new ArgumentNullException("param.PlanungsGruppen");     }     int nRetry = 0;     while (nRetry < DBWerkzeug.MaxWiederholungen)     {         try         {             using (XXX_POC.POC db =                 new XXX_POC.POC(                     DBWerkzeug.GetConnectionString()))             {                 var q =                     from p in db.Stammdaten_Planungsgruppe                     orderby p.Name.ToLower()                     select new PlanungsGruppeDM                     {                         ID = p.Rowid,                         Name = p.Name                     };                 ret.PlanungsGruppen = q.ToList<PlanungsGruppeDM>();             }         }         catch (ChangeConflictException e)         {             nRetry++;             if (nRetry >= DBWerkzeug.MaxWiederholungen)             {                 ret.FehlerText = e.Message;                 ret.HatFehler = true;                 throw;             }         }         catch (Exception e)         {             ret.FehlerText = e.Message;             ret.HatFehler = true;             throw;         }     }     return ret; } I ended up with this: public PlanungsGruppenLesenA PlanungsGruppenLesen(     PlanungsGruppenLesenF param) {     PlanungsGruppenLesenA ret = new PlanungsGruppenLesenA();       if (ret.PlanungsGruppen == null)     {         throw new ArgumentNullException("param.PlanungsGruppen");     }     ret.PlanungsGruppen = CatchExceptions<PlanungsGruppeDM>(         delegate         {             using (XXX_POC.POC db =                 new XXX_POC.POC(                     DBWerkzeug.GetConnectionString()))             {                 var q =                     from p in db.Stammdaten_Planungsgruppe                     orderby p.Name.ToLower()                     select new PlanungsGruppeDM                     {                         ID = p.Rowid,                         Name = p.Name                     };                 return q.ToList<PlanungsGruppeDM>();             }         },         ret);                return ret; } What calls the encapsulated X tries and the error handling: public delegate List<T> MachMal<T>();   public static List<T> CatchExceptions<T>(MachMal<T> machMichFertig, BasisA ret) {     int nRetry = 0;     while (nRetry < DBWerkzeug.MaxWiederholungen)     {         try         {             return machMichFertig();         }         catch (ChangeConflictException e)         {             nRetry++;             if (nRetry >= DBWerkzeug.MaxWiederholungen)             {                 ret.FehlerText = e.Message;                 ret.HatFehler = true;                 throw;             }         }         catch (Exception e)         {             ret.FehlerText = e.Message;             ret.HatFehler = true;             throw;         }     }     return null; } But I'm not quite sure If this made stuff really easier ;-)

IE Active Content Update

I'm back on ASP.NET (at least more than last year :-)) - And it feels great (again) digging deep into some interesting areas around user interfaces (also WPF!!!), processes and tasks, state management, async stuff (multi threading/AJAX & JSON) and so on... Personal Ref.: Activating ActiveX Controls Adobe's comments on the topic... IE Persistence Behaviour Tabbed Browsing for Developers Search Provider Extensibility in Internet Explorer

Macro to enable Code Analysis and define FxCop rules

I'm currently helping a customer to migrate a *really large* web application from ASP.NET 1.1 to 2.0 (round about 130 projects in the master solution...). I always liked FxCop but enabling "Code Analysis" manually is way to complicated (or not geekish enough?!?). So I wrote this small macro this morning. Imports System Imports System.Diagnostics Imports System.Text Imports System.Windows.Forms   Imports EnvDTE Imports EnvDTE80   Public Module CodeAnalysis       Public Sub EnableFxCop()         Dim objProj As Object()         Dim proj As Project           For i As Integer = 1 To DTE.Solution.Projects.Count             proj = DTE.Solution.Projects.Item(i)             EnableFxCop(proj)         Next     End Sub       Private Sub EnableFxCop(ByVal project As Project)           If project.Kind = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}" Then             'Filter Project Folders             For Each subProject As ProjectItem In project.ProjectItems                 EnableFxCop(subProject.SubProject)             Next         Else             project.ConfigurationManager.ActiveConfiguration.Properties.Item( _                 "RunCodeAnalysis").Value = "True"               project.ConfigurationManager.ActiveConfiguration.Properties.Item( _                 "CodeAnalysisRules").Value = String.Concat( _                     "-Microsoft.Design#CA2210;", _                     "-Microsoft.Design#CA1020;", _                     "-Microsoft.Naming#CA1705;", _                     "-Microsoft.Naming#CA1709")             project.Save()         End If     End Sub End Module

WCF Sample - Attribute driven Constraints

I posted some Bits Michael and I wrote recently in a WCF Project:The Constraints-Extensions let you easily define attribute based limitations for input, output and return parameters. Limitations can be set to service contracts operations as well as to data contract data member implementations. The constraints validation gets called at runtime by the WCF infrastructure and the behavior will throw a ConstraintViolationException with descriptive messages in case of a constraint gets violated/the validation fails. The following constraint attributes have been implemented:BetweenBetweenExclusiveCompareAgainstEarlierThanNowEarlierThanTodayEqualToGreaterEqualToGreaterThanLaterThanNowLaterThanTodayLessEqualToLessThanMatchMaxElementsMaxLengthMinElementsMinLengthNotBetweenNotEmptyNotEqualToNotNullNullableOneOff·     Data Contract Sample: [DataContract]class TestClass{    [DataMember]    [LessEqualTo(CompareAgainst.FieldOrProperty,"EndDate")]    public DateTime StartDate;     [DataMember]    [GreaterEqualTo(CompareAgainst.FieldOrProperty, "StartDate")]    public DateTime EndDate;     public TestClass(DateTime startDate, DateTime endDate)    {        StartDate = startDate;        EndDate = endDate;    }}   Service Contract Sample: [ServiceContract, ConstraintsValidatorBehavior]interface ITestConstraints{     [OperationContract]    [return:GreaterEqualTo(0)]     int MethodA(        [Between(0,100)]         int a,         [LessEqualTo(20)]         double b,         [NotEmpty]         string c);      [OperationContract]    int MethodB(        [Between(0, 100)]         int a,         [LessEqualTo(20)]         ref double b,         [NotEmpty]         out string c);     [OperationContract]    [return:LaterThanNow]    DateTime MethodC(        [GreaterThan(0)]         int a);       [OperationContract]    void MethodD(        [VerifyObject]         TestClass a);} Download at wcf.netfx3.com