Greg 的个人资料Greg Olsen - Yellow Duck...照片日志列表 工具 帮助

日志


6月15日

FileHelpers - Import/Export Data Files (OpenSource.NET)

This is a great handy framework or .NET library when working with data files in .NET. Info about this below (directly from the site http://www.filehelpers.com/)
 
The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.
 
The idea is pretty simple:
You can strong type your flat file (fixed or delimited) simply describing a class that maps to each record and later read/write your file as an strong typed .NET array 
 
The Library also has support for import/export data from differents storages like Excel, Access, SqlServer, etc.
 
The FileHelpers are completely free because they are released under the LPGL Licence that allows the use of the code and binaries in all places, also in commercial applications.
 
Enjoy!
Greg Olsen
Yellow Duck Guy
2月16日

How to read Gmail Email Programmatically - .NET 2.0 Version

Requests have come through for a .NET 2.0 Framework version of the solution I created back in 2nd September 2008.  I have just completed a compiled .NET 2.0 Framework version of this and thought it would be a good idea to publish it. The solution has been created with Visual Studio .NET 2008 with the Target Framework of .NET 2.0 selected. This also runs on the Windows 7 OS platform!

You can download it from here: http://www.box.net/shared/q73k2sid7f

This is a follow up to the original posting here:

“How to read Gmail Email Programmatically” : http://yellowduckguy.spaces.live.com/blog/cns!DA380C13569E8907!225.entry

Enjoy!

Greg Olsen
Yellow Duck Guy

1月9日

XamlPad - Create simple XAML files

XAMLPad is a lightweight tool provided with the .NET Framework 3.0 SDK to quickly create simple Extensible Application Markup Language (XAML) files. To find the application you need to either browse to it on your computer or get a hold of it from the .NET Framework 3.0 SDK.
 
It provides a split screen where the XAML code can be typed manually at the bottom and then immediately previewed in the above XAML renderer, to let you know what it all looks like.
 
Below is where it is located. Note: I am running Windows 2008 Server. If you can't find it, then I suggest you get a hold of the .NET Framework 3.0 SDK.

There is also a number of different versions on the Internet where people have extended the basic version to include more functionality as well. Quick search and you probably will come across them.

Enjoy!
Greg Olsen
Yellow Duck Guy

12月29日

LINQ 2 SQL – Dying Concept?

I have never been a big fan of Linq2Sql and it seems from a number of Microsoft posts I read that link2sql is a ‘dying concept’ within the .NET Framework. Well that seems to be the message I receiving. Most likely linq2sql will still remain within the .NET framework but won’t get the support like other .NET Framework concepts. Entity Framework looks like it’s now the number one focus.

You can have a read here where Tim Mallalieu discusses the roadmap which includes linq2sql: http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx

Also another article, this time written by Damien Guard posted here:
http://damieng.com/blog/2008/10/31/linq-to-sql-next-steps

Something to think about … if you have your own opinion, feel free to post.

Greg Olsen
Yellow Duck Guy

11月15日

.NET Framework 3.5 Common Namespaces and Types Poster

The .NET Framework 3.5 is the newest .NET Framework release from Microsoft and it contains a load of very useful Namespaces and Types which will be in heavy use by every Microsoft .NET developer.

If you would like an overview of the of the Namespaces and Types which make up the new .NET Framework then you can get a hold of the new poster made available.

Simply visit the below link and download your desired output format.

.NET Framework 3.5 Common Namespaces and Types Poster -DOWNLOAD-


Could be something to place on those walls of your development office!

 

Greg Olsen
Yellow Duck Guy

10月6日

Rhino Mocks v3.2 - New Syntax

I have been using Rhino Mocks v3.2 with my current project and i'm finding it is a test mocking tool I can't live without!

What is Rhino Mocks?

"Rhino.Mocks is an attempt to create easier way to build and use mock objects and allow better refactoring support from the current tools. It's a hybrid approach between the pure Record/Replay of EasyMock.Net's model and NMock's expectation based model. Rhino.Mocks originated from EasyMock.Net and attempt to improve on their model to create easy to use and power mocking framework. It's free for use and modification for open source and commercial software.

Licensing: Rhino Mocks is Free Software which is released under the BSD license."

(http://ayende.com/projects/rhino-mocks.aspx )

One aspect I like (which is mentioned on the Rhino homepage) is the fact that Rhino uses typed objects not strings like the others i.e. NMock, TypeMock.Net. Having the objects typed means that the build/compiler process will catch any human error mistakes. If they are not typed i.e. in strings like "ICustomerView" then if they are spelt incorrect or used incorrectly then they will only "blow up" or throw exceptions on you at runtime, which wastes time - that's not really nice! Hence why I like Rhino.

This post will demonstrate the new Rhino Mocking syntax in v3.2 which I think is getting really close the "English spoken behavior" for tests.  What I mean by this is that I would like to see a block which illustrates my Expectations and another block which illustrates my Assertions allowing the Unit Test(s) to be easy read and understood.

There is a blog post at  e-TOBI.net which illustrates really nicely the different versions of usage for Rhino Mocking. 

Below is a few examples from this site demonstrating the older syntax versions:

Example using "Replay All" (Expectations) and "Verify All" (Assertions/Verifications)

           

        MockRepository mocks = new MockRepository();

        IDependency dependency = mocks.CreateMock<IDependency>();

 

        // define expectations

        Expect.Call(dependency.GetSomething("parameter")).Return("result");

        dependency.DoSomething();

        mocks.ReplayAll();

 

        // run test subject

        Subject subject = new Subject(dependency);

        subject.DoWork();

 

        // verify expectations

        mocks.VerifyAll();

Example using "Record" (Expectations) and "Playback" (Assertions/Verifications)

         
        MockRepository mocks = new MockRepository();

        IDependency dependency = mocks.CreateMock<IDependency>();

 

        using (mocks.Record())

        {

             Expect.Call(dependency.GetSomething("parameter")).Return("result");

             dependency.DoSomething();

        }

 

        using (mocks.Playback())

        {

             Subject subject = new Subject(dependency);

             subject.DoWork();

        }

So when I analyse the above two versions I am left feeling confused by the syntax by what's actually happening. I have used both and they work just fine but I would rather have something that is more English readable or "English Fluent" as Rhino Mocks Website states.

This problem then leads developers spending hours to understand how this all works and where should I put my Expectations and Assertions. A prime example is when I try and teach these two syntax versions above to other developers. I can understand why they don't pick it up on the first go! So the latest version is getting better and is more "English Fluent" but does introduce delegates which then may also confuse developers which haven't used them before.

The "blocks which illustrates my Expectations and another block which illustrates my Assertions" which I spoke about earlier are basically defined now with the words of "Expecting" and "Verify" - so we are nearly there! 

Example using the new syntax in v3.2

 

        MockRepository mocks = new MockRepository();

        IDependency dependency = mocks.CreateMock<IDependency>();

 

        With.Mocks(mocks).Expecting(delegate

        {

             Expect.Call(dependency.GetSomething("parameter")).Return("result");

             dependency.DoSomething();

        })

        .Verify(delegate

        {

             Subject subject = new Subject(dependency);

             subject.DoWork();

        });

Below is a couple of Unit Test examples using the syntax from v3.2:

       

        [Test]

        public void SendRemoteIPCallsSendRemoteIPFromService()

        {

            _remoteIPPresenter = new RemoteIPPresenter(_remoteIPView, _remoteIPService);

 

            With.Mocks(_mockRepository).Expecting

                (delegate

            {

                _remoteIPService.SendRemoteIP(false);

            }

            )

            .Verify

            (delegate

            {

                _remoteIPPresenter.SendRemoteIP();

            }

            );

        }

        [Test]

        public void RetrieveCurrentRemoteIPCallsRetrieveRemoteIPFromLocalAndDisplayRemoteIP()

        {

            _remoteIPPresenter = new RemoteIPPresenter(_remoteIPView, _remoteIPService);

  

            With.Mocks(_mockRepository).Expecting

                (delegate

                {

                    RemoteIP remoteIP = new RemoteIP("125.13.23.3", DateTime.Now);

                    Expect.Call(_remoteIPService.RetrieveRemoteIPFromLocal()).Return(remoteIP);

                    _remoteIPView.DisplayRemoteIP(remoteIP.IPAddress, remoteIP.DateReceived);

                }

            )

            .Verify

            (delegate

            {

                _remoteIPPresenter.RetrieveCurrentRemoteIP();

            }

            );

 

        }

So I suggest you start using the syntax from v3.2.  Rhino is certainly a mocking framework for .NET I would recommend to any developer.

Happy coding!

Yellow Duck Guy
Greg Olsen.

9月2日

How to read Gmail Email Programmatically

With my previous post I mentioned I would do a sample app to demonstrate how easy it would be to read Gmail email account programmatically using c# code and with a little help from ATOM.NET ( http://atomnet.sourceforge.net/ ).

Bellow is the Program.cs from the sample console application I created for demo purposes. I developed this using Visual Studio 2008, but the latest version was not required in order to achieve this outcome. 

The code simply connects to the Gmail Atom feed here: https://mail.google.com/mail/feed/atom and sends your credentials (username, password for your email account) and logs into the account and reads the latest email in the inbox. The demo only reads 1 email. You can extend this starting on line 73.

I have put a few comments throughout the code to help with explanations, again just to illustrate how the code is working.  You will notice how ATOM.NET made it very easy reading the XML feed from Google Gmail.

If you want the full Visual Studio 2008 source code of this demo, then you can have it FREE via the link below:

TestCheckGMailEmail.zip (93.4 kb)

If you would like a .NET 2.0 version of the source code then you can add a comment to this posting requesting it, or send me an email to yellow_duck_guy@hotmail.com (Note: there are underscores (_) within the email address).

1: using System;
2: using System.IO;
3: using System.Net;
4: using System.Text;
5: using Atom.Core;
6: using Atom.Core.Collections;
7:  
8: namespace TestCheckGMailEmail
9: {
10:    
/// <summary>
11:    
/// Console Demo Application Code for checking Gmail Email Account
12:    
/// Check out my blog here: http://yellowduckguy.spaces.live.com
13:     /// </summary>
14:     internal class Program
15:     {
16:        
// GMAIL URL for Atom Email Reading
17:         private static readonly Uri _gmailServerUri = new Uri("https://mail.google.com/mail/feed/atom");
18:  
19:         private static void Main(string[] args)
20:         {
21:             ShowAppTitle();
22:             PromptForCredentials();
23:         }
24:  
25:         private static void PromptForCredentials()
26:         {
27:             Console.WriteLine("Enter User Name:");
28:             string username = Console.ReadLine();
29:             Console.WriteLine("Enter Password (Security Note: This will be shown on screen):");
30:             string password = Console.ReadLine();
31:  
32:             ReadGMailEmail(username, password);
33:         }
34:  
35:         private static void ReadGMailEmail(string username, string password)
36:         {
37:            
// Remove data input, comment out if you want to keep the input
38:             Console.Clear();
39:             ShowAppTitle();
40:  
41:             Console.WriteLine("Now attempting login ... ");
42:  
43:             WebRequest webRequestGetUrl;
44:            
try
45:             {
46:                
// Create a new web-request instance
47:                 webRequestGetUrl = WebRequest.Create(_gmailServerUri);
48:  
49:                 byte[] bytes = Encoding.ASCII.GetBytes(username + ":" + password);
50:  
51:                
// Add the headers for basic authentication.
52:                 webRequestGetUrl.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(bytes));
53:  
54:                
// Get the response feed
55:                 Stream feedStream = webRequestGetUrl.GetResponse().GetResponseStream();
56:  
57:                
// Load Feed into Atom DLL
58:                 AtomFeed gmailFeed = AtomFeed.Load(feedStream);
59:  
60:                 AtomEntryCollection entries = gmailFeed.Entries;
61:  
62:                
// Clear the screen just before we tell the user more information
63:                 Console.Clear();
64:                 ShowAppTitle();
65:                
66:                 if (entries.Count > 0)
67:                 {
68:                     Console.WriteLine(string.Format("Found {0} email(s)", entries.Count));
69:                     Console.WriteLine(Environment.NewLine);
70:  
71:                    
// Read the first email in the inbox only.
72:                    
// you can change this section to read all emails.
73:                     for (int i = 0; i < 1; i++)
74:                     {
75:                         AtomEntry email = entries[i];
76:                         Console.WriteLine("Brief details of the first Email in the inbox:");
77:                         Console.WriteLine("Title: " + email.Title.Content);
78:                         Console.WriteLine("Author Name: " + email.Author.Name);
79:                         Console.WriteLine("Author Email: " + email.Author.Email);
80:                         Console.WriteLine(Environment.NewLine);
81:                         Console.WriteLine("Email Content: " + Environment.NewLine);
82:                         Console.WriteLine(email.Summary.Content);
83:                         Console.WriteLine(Environment.NewLine);
84:                         Console.WriteLine("Hit 'Enter' to exit");
85:                         Console.ReadLine();
86:                     }
87:                 }
88:                
else
89:                 {
90:                     Console.WriteLine("No emails found in the inbox");
91:                     Console.WriteLine(Environment.NewLine);
92:                     Console.WriteLine("Hit 'Enter' to exit");
93:                     Console.ReadLine();
94:                 }
95:             }
96:             catch (Exception ex)
97:             {
98:                
// Note: Catching all exceptions as generic exceptions just for demo purposes.
99:                
//       You can be more specific with handled errors
100:                
101:                 Console.Clear();
102:                 ShowAppTitle();
103:                 Console.WriteLine("Oops! An error occurred: " + ex.Message);
104:                 Console.WriteLine(Environment.NewLine);
105:                 Console.WriteLine(Environment.NewLine);
106:                 Console.WriteLine("Hit 'Enter' to exit");
107:                 Console.ReadLine();
108:             }
109:         }
110:  
111:         private static void ShowAppTitle()
112:         {
113:             Console.WriteLine("--------------------------------------------");
114:             Console.WriteLine("Test Application that will check GMail email");
115:             Console.WriteLine("Written By: Greg Olsen, 2nd Sept 2007       ");
116:             Console.WriteLine("Blog: http://yellowduckguy.spaces.live.com     ");
117:             Console.WriteLine("--------------------------------------------");
118:             Console.WriteLine(Environment.NewLine);
119:         }
120:     }
121: }

Yellow Duck Guy
Greg Olsen

ATOM.NET - Object model to Write and Parse Atom feeds

Well I stumbled across this the ATOM.NET open source Website today ( http://atomnet.sourceforge.net/ ), which allows you to write an parse Atom feeds.

More info from the Website:

"Atom is a new syndication format (similar to RSS in functionality) but aimed to simplify the task with a simple and clear specification.
Atom.NET is an open source library entirely developed in C# aimed to handle Atom feeds in an handy way. It provides an object model to write and parse Atom feeds. It's compatible only with the 0.3 Atom draft specification."

Current download version is 0.4.3. The compiled DLL library has been built with .net 1.1, but you can hook this in as a project reference with your .NET 1.1,2.0,3.0,3.5 Visual Studio solutions with no problems.

This site got me thinking that this could be very useful. I know personally I would like a Gmail email reader to read some notifications I get sent to a Gmail email account.  So I will probably use ATOM.NET to read them programmatically.  Once I finish the small demo application I will post the source code to get you started.

ATOM.NET download page is here: ATOM.NET Download Page

Yellow Duck Guy
Greg Olsen