Showing posts with label dotnetmag. Show all posts
Showing posts with label dotnetmag. Show all posts

Tuesday, October 4, 2011

BCL Team Blog provides more information on code reuse (Portable Libraries part 4)

Today the BCL Team Blog addressed the issue of code reuse: Porting existing .NET code to Metro style apps

"One of the things we know people want to do is reuse existing .NET code but due to the constraints on Metro style apps it is not possible to directly reuse existing .NET class libraries nor is it always possible to simply recompile existing code. We realize this is going to be a pain point for many current .NET developers so we are working on a guide at the .NET for Metro style apps overview page which will assist developers in translating their existing .NET skill set into the Metro style apps world.”

Bringing existing managed code into Metro style apps" by Daniel Plaisted is a recommended resource.

Daniel ports a windows phone app to a Metro app. Most of presentation is mainly about porting the UI.

Some slides from the presentation:

move_code

differenes

Getting the pictures from a local folder.

folders

There is no database access.

Wednesday, September 28, 2011

.NET portability is not an afterthought (adding another layer of indirection - Portable Libraries part 3)

Before //build/ I was not aware of the Portable Class Libraries Project. The origin of the project is the notion that the number of platform that provide .NET support has increased from one to four and that this number will increase in the future. Making .NET available on more platforms is the future of .NET.

The Portable Class Libraries Project was the first attempt of solving the portability issue but it ran into some constraints. One of them was the increasing complexity: “Portability is an afterthought”.

After watching some of the presentation at //build/ and experimenting with Visual Studio Express for Windows Developer Preview I thought that the whole purpose of creating a portable library was to bridge the gap between .NET and the WinRT.

ClassLibrary

This is not the case. Yesterday I watched .NET 4.5: Portable Libraries by David Kean and Mircea Trofin at Channel 9. It all made more sense to me.

 

The show:

  • The first 15 minutes explains portability and the first version of the Portable Class Libraries Project.
  • 15:00 whiteboard time.
  • Explaining the first way to solve portability problem:
  • wb1
  • wb2
  • Explaining the problems with System.NET
  • wb3
  • 22:00 the new solution: one portable library by design.
  • Portability is not an afterthought
  • 28:00
    • You program against a contract
    • It is up to the platform to implement the contract
    • Type forwarding:
      wb5
    • You program against the exposed api.
    • wb4
    • And the GAC will forward to dll that implements the actual code.
    • 33:00 Portability is a first class citizen
  • 35:00 It is about granularity a method is too small a dll is too big.
  • 36:00
    • Q: Are there two gacs in the Windows 8 world?
    • A: there is one gac. The metro mode is under “us”.
  • 37:00 breaking changes:
    • Type
    • wb6
    • wb7
  • 45:00 Close and Dispose. Close is disposed, no more closing just disposing.
  • They reference: A .NET developer's view of Windows 8 app development by Krzysztof Cwalina
  • 50:00 .NET has a great future with portability in mind.

So it makes sense to fix and recompile your legacy libraries. You can port them to all platforms that will have .NET available.

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 15, 2011

F# type providers: oData from NuGet

Yesterday F# 3.0 Developer Preview  became available at the MSDN Site. It will be general available on Friday.

One of the new features is the type provider.

This little snippet shows how easy it is to query a site that provides oData. NuGet is an example of a site that provides oData.

Code Snippet
  1. #r "FSharp.Data.TypeProviders.dll"
  2. #r "System.Data.Services.Client.dll"
  3.  
  4. open Microsoft.FSharp.Data.TypeProviders
  5.  
  6. [<Generate>]
  7. type NuGet = ODataService<"http://packages.nuget.org/v1/FeedService.svc/">
  8.  
  9. let dbNuGet = NuGet.GetDataContext()
  10. let packages = dbNuGet.Packages
  11.  
  12. let nuGetQuery =
  13.     query { for package in packages do
  14.             where (package.Tags.Contains("fsharp") && package.IsLatestVersion)
  15.             select package }
  16.  
  17. let printPackage (package:NuGet.ServiceTypes.PublishedPackage) =
  18.     printfn "- - - - - - - - - - - - - - - - - - - -"
  19.     printfn "Title: %s" package.Title
  20.     printfn "Authors: %s" package.Authors
  21.     printfn "Description: %s" package.Description
  22.  
  23.     
  24. nuGetQuery |> Seq.iter printPackage

This is the result:

result

One of the nice features of type providers is intellisense for the available types.

intellisense

It is possible to create your own type provider.

More information will be available on Friday. Don Syme will present all the details at build: http://channel9.msdn.com/events/BUILD/BUILD2011/SAC-904T

update: Writing F# Type Providers with the F# 3.0 Developer Preview - An Introductory Guide and Samples

Tuesday, June 7, 2011

Create Open XML Word document in F# (update)

This time I will create a word document (open xml) in F# without using the Open XML SDK and using XML libraries. Things have been intentionally kept simple.

I start by modelling a simple word document:

There are Two types of paragraphs: normal and bold ones:
  1. //reference to the package
  2. #r "WindowsBase"
  3.  
  4. open System
  5. open System.IO
  6. open System.IO.Packaging
  7.  
  8. type Paragraph =
  9.     | Normal of string
  10.     | Bold of string
 
And a document is a list of paragraphs:
  1. type Document =
  2.     | Paragraphs of Paragraph list
 
This is the test:
  1. //test
  2. let list = [Normal("test1"); Normal("test2"); Bold("test3"); Normal("test4")]
  3. let doc = Paragraphs(list)

 

Next we have the to create an xml version of document:






  1. //helpers

  2.  

  3. // http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx

  4. let encodedXml (text: string) =

  5.     text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;")

  6.  

  7. let xml1 = "<node>it's my \"node\" & i like it<node>"

  8. let encode = encodedXml xml1

  9.  

  10. let addString acc item = acc + "\r\t" + item

  11.  

  12. let createParagraphNormal text =

  13.     "<w:p>

  14.       <w:r>

  15.         <w:t>" + encodedXml text +

  16.         "</w:t>

  17.       </w:r>

  18.     </w:p>"

  19.  

  20. let createParagraphBold text =

  21.     "<w:p>

  22.       <w:r>

  23.         <w:rPr>

  24.          <w:b />

  25.         </w:rPr>

  26.         <w:t>" + encodedXml text +

  27.         "</w:t>

  28.       </w:r>

  29.     </w:p>"

  30.  

  31. let createParagraph = function

  32.     | Normal(text) -> createParagraphNormal text

  33.     | Bold (text) -> createParagraphBold text

  34.  

  35. let createParagraphs = function

  36.     | Paragraphs(list) -> list|> List.map createParagraph |> List.reduce addString

  37.  

  38. //test

  39. let p = createParagraphs doc

  40.  

  41. let createDocument doc =

  42.     let startDocument = @"<?xml version=""1.0"" encoding=""utf-8""?>

  43.                     <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">

  44.                       <w:body>"

  45.     let endDocument = "  </w:body>

  46.                         </w:document>"

  47.     let content = createParagraphs doc

  48.     startDocument + content + endDocument





 

Then we have to create a package, add the document and save it to disk:






  1. //add the document to package and save

  2. let createFile doc (fileName:string) =

  3.     using (Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite))(fun package ->

  4.         let uri = new Uri("/word/document.xml", UriKind.Relative )

  5.         let partDocumentXML =

  6.             package.CreatePart(

  7.                                 uri,

  8.                                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" )

  9.     

  10.         using(new StreamWriter(partDocumentXML.GetStream(FileMode.Create, FileAccess.Write)))(fun stream ->

  11.             doc |> createDocument |> stream.Write

  12.         )

  13.  

  14.         package.CreateRelationship(

  15.                                     uri,

  16.                                     TargetMode.Internal,

  17.                                     "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",

  18.                                     "rId1") |> ignore

  19.     )

  20.  

  21. //test

  22. let fileName = @"D:\Tmp\test.docx"

  23.  

  24. createFile doc fileName;;




 
This is the result:

 

I have created a map from twitter to my document model






  1. type UserStatus =

  2.     { UserName : string;

  3.       ProfileImage : string;

  4.       Status : string;

  5.       StatusDate : DateTime }

  6.  

  7. let mapUserStatus (us:UserStatus) =

  8.     let date = "Date: " + us.StatusDate.ToShortDateString()  + " time: " + us.StatusDate.ToLongTimeString()

  9.     [Normal(date); Bold(us.UserName); Normal(us.Status); Normal("")]

  10.  

  11. let addUserStatus acc item = item @ acc

  12.  

  13. let result = xml|> parseTweetXml |> List.map mapUserStatus|> List.reduce addUserStatus





 


This results in the following document:









Remark: I had to Google “Weiner” to understand the jokes.

Total Pageviews