Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, September 22, 2011

Reuse of legacy software by WinRT and Metro (Windows 8) (Portable Libraries part 2)

 

This is a follow up to my previous post Create a Metro App in F# (NOT).

Things become quit complex if you want to reuse your windows legacy software. In this case legacy has a positive connotation. Legacy software is an asset of you and/or your company.

Suppose one want to reuse it and make it available for a Metro app on Windows 8 what are your options? After watching some of the build presentations:

 

I came up with the following flow chart:

 

LegacyWinRT_4

So:

  • You can reuse html, css and JavaScript.
  • I am not an expert on C++, COM, etc. so I do not know.
  • If you have only a dll you cannot use it.
  • If you have source code that references a language dll (like F#) or have no options today. This will change when the final product becomes available.
  • If you want to create a Metro Style project in C++ or JavaScript you can reuse your components code by fixing issues and recompile it to a WinMD file (WinRT component). Some issues can be exposing:
    • non WinRT types
    • non sealed classes
    • non system provided generic types
  • If you want to create a Metro Style project and this is one of the .NET languages you can reuse your components code by fixing issues and recompile it to a dll. Some issues can be:
    • Streams

 

Disclaimer: This is my current understanding, based on the presentations I viewed. If you have other information available please leave a note in the comments.

Thursday, September 1, 2011

Monads for .NET developers

I have written an article for the Dutch developer magazine of SDN (Magazine 111). It is titled: “Monads voor de .NET-ontwikkelaar”.

In this post I will list some of the available resources on monads for .NET developers:

C# resources

Books:

Web:

F# resources

Computation Expressions are addressed in most of the available F# books (Professional F# 2.0 does not): F# and functional programming

Web:

Presentations

Some theory

Code:

The source code of the article: MonadsVoorDeDotNetOntwikkelaar and MonadsVoorDeDotNetOntwikkelaar_english

Sunday, August 7, 2011

Adding xml strings in C# using LINQ

This is follow up on my previous blog post:Adding xml strings in F# using computation expressions. The text is nearly identical, the language is C# instead of F#.

In some cases classes are enhanced wrappers of simple classes. For instance in .NET:

  • DateTime is a point in time and provides, among others things, extra functionality related to representing date and time formats.
  • XElement is an xml string and provides, among others things, checks to determine the validity of the xml string.
  • Option can contain a value and can tell you if there is value available.

Sometimes you have to deal with underling class and reuse some of the functionality of the wrapper. In this case you could consider using LINQ. We first define the Return, Bind and SelectMany functions to create the required functions.

For details more details see:

All excellent resources.

    public static classXmlMonad
    {
      //monad
     
    public staticXElement Return(this stringtext)
      {
        try
      
    {
          returnXElement.Parse(text);
        }
        catch(XmlException exc)
        {
          returnXElement.Parse(String.Format("<XmlException>{0}</XmlException>", exc.Message));
        }
      }

      public staticXElement Bind(thisXElement el, Func<string, XElement> f)
      {
        try
      
    {
          returnf(el.ToString());
        }
        catch(XmlException exc)
        {
          returnXElement.Parse(String.Format("<XmlException>{0}</XmlException>", exc.Message));
        }
      }

      public staticXElement SelectMany(thisXElement el, Func<string, XElement> f, Func<string, string, string> select)
      {
        returnel.Bind(text => f(text).Bind(x => select(text, x).Return()));
      }
    }

We can now define a add function that will xml elements by using a string manipulation:
private static XElement Add(XElement el1, XElement el2)
{
XElement result =
from x1 in el1
from x2 in el2
select x1.Substring(0, x1.LastIndexOf("<")) + x2.Substring(x2.IndexOf(">") + 1);

return result;
}
Add in action:
var el1 = XElement.Parse("<a><b>1</b><b>2</b></a>");
var el2 = XElement.Parse("<a><b>3</b><b>4</b></a>");

Console.WriteLine(string.Format("Add: {0}", Add(el1,el2)));

Results in:


AddInAction


When we change the xml into:

var el2 = XElement.Parse("<c><b>3</b><b>4</b></c>");

The result is:


AddInActionException

Total Pageviews