SoftDev

LINQ With C# Book Is Finally Out

It’s finally out!

The LINQ Com C# (LINQ With C#) book that Luís and I wrote is out. Well, mostly Luís than I.

This book, published by FCA, is targeted at anyone that already knows C# 2.0 and wants to know learn the new features introduced with C# 3.0 that made possible LINQ (Language INtegrated Query). The examples in the book are written in C#, but Visual Basic get be get from the book’s site.

Livro LINQ Com C# Title: LINQ Com C#
Authors: Luís Abreu / Paulo Morgado
ISBN: 978-972-722-547-7
Number of Pages: 216
Format: 17,0 x 24,0 x 1,3 cm (6.7 x 9.4 x 0.5 in)

The book can be found here:

ASP.NET Futures: Control ClientID Generation

ASP.NET is expected to have some improvements on the generation of client IDs.

Although this is a major improvement, it comes short by not allowing the generation of shorter client IDs for server controls.

My good friend Nuno Gomes has done some work on generating shorter client IDs for controls (*).

Jeff has taken it one step further with his How to shorten ASP.NET automatically generated control IDs article on CodeProject.

If you want to see a running example, check out http://www.biocompare.com/.

How To Issue Server Callbacks

Callbacks were introduced in ASP.NET 2.0 and is a simple mechanism for calling page or control functionality without page rendering and without the user noticing a post back.

For a page or control to handle callbacks, all it needs is to implement the ICallbackEventHandler Interface.

When the client calls back to de page or control, the initial state of the controls is posted along with the control being called upon in the __CALLBACKID field and the callback parameter in the __CALLBACKPARAM field.

It’s quite a simple procedure.

But what if you want to issue a callback server side?

In order for a request to be identified as a callback (IsCallback), the request must be a postback (IsPostback) and the before mentioned fields must be in the post data of the request. On the other hand, for a request to be considered a postback, the level of server calls (Transfer or Execute) must be 0 (meaning that the current request hasn’t made any Transfer or Execute calls) or the type of the page is the same of the Handler for the current request and the HTTP method is POST.

Changing the HTTP method is (as far as I know) impossible. So, if the request is not already a POST, there’s no way to issue a callback.

Setting the post data is easier. All it’s needed is to override the page’s DeterminePostBackMode method (or in a page adapter) and return the post data previously saved in a context item. Something like this:

protected override NameValueCollection DeterminePostBackMode()
{
    NameValueCollection postBackMode = Context.Items["callbackPostData"] as NameValueCollection;

    return (postBackMode != null) ? postBackMode : base.DeterminePostBackMode();
}

And issue a callback is something like this:

IHttpHandler handler = this.Context.Handler;
try
{
    NameValueCollection postData = new NameValueCollection();
    postData.Add("__CALLBACKID", sender);
    postData.Add("__CALLBACKPARAM", this.argument.Text);

    Context.Items["callbackPostData"] = postData;

    Page calledPage = (Page)PageParser.GetCompiledPageInstance("~/Callback1.aspx", this.Server.MapPath("~/Callback1.aspx"), this.Context);

    this.Context.Handler = calledPage;

    StringWriter writer = new StringWriter();

    Server.Execute(calledPage, writer, false);

    this.response.Text = writer.ToString();
}
finally
{
    this.Context.Handler = handler;
}

You can find an implementation of a caller and a called page here.

PDC2008: Is My House On The Cloud Or Is The Cloud In My House?

This year’s PDC is mostly about the cloud.


Everyone is talking about the cloud: cloud services, cloud computing, etc. but I’m not sure everyone has the same understanding of what the cloud is. Let’s say it’s a cloudy concept.


So, what is this cloud thing, anyway?


Let’s take Windows Home Server (WHS) as an example. Microsoft provides a set of services through homeserver.com that allows me to control my home server and access my other PCs in the home network. I guess I could say that homeserver.com is a cloud platform that puts my house in the cloud.


On the other hand, WHS is built on top of Windows Server 2003 which allows me to take full advantage of its web server (IIS) to expose services through homeserver.com. Services that could be for my personal use or that I could provide to others, thus putting my house on the cloud.


At a larger scale, that’s how I see the cloud and it’s usage. An indistinct universe of service providers and service consumers where some are just providers, others are just consumers and others are both (service aggregators, value added brokers, etc.)


I guess that Microsoft’s view in regards to the cloud and WHS is aligned with mine because there will be two PDC sessions about this:





  • Tue 10/28 | 5:15 PM-6:30 PM | 409A

    Presenter(s): CJ Saretto, Fabian Uhse

    Learn how to build applications and services deployed on Windows Home Server that power PCs and devices throughout a connected home. See an example home automation and energy management service, and watch the creation of an on-premises service that exposes HVAC, window shades, and lighting controls to all devices on the home network. Hear how to package the service as a Windows Home Server add-in, quickly create a configuration UI, deploy a PC application for controlling the service, and demonstrate control from other devices such as TVs and cell phones.




  • Wed 10/29 | 10:30 AM-11:45 AM | 409A

    Presenter(s): Brendan Grant, CJ Saretto

    Learn how to leverage the Remote Access platform in Windows Home Server to expose on-premises services running in the connected home to the Internet. See how to expose a home automation service running on Windows Home Server to the Internet, and watch a demonstration that shows how to expose services that may be hosted on other devices inside the connected home using Windows Home Server as a gateway. Finally, see a sneak preview of Home Server and Live Mesh working together to further enhance the remote experience for the connected home.


Windows Home Server Resources

And, suddenly, a cloud in your future is not a ad thing, quite the opposite.

PDC2008: Are Windows Forms Dead?

This year’s PDC is all about the cloud (or cloudy as I call it) and the .NET Framework 4.0.

No one at Microsoft has ever told me that Windows Forms are dead (quite the opposite) but looking at the list of sessions for this year’s PDC I can only see WPF.

So the question remains: Are Windows Forms dead?

Typemock Isolator 5.1 Released

This major version adds static method support and non-public method faking to the AAA API. Check out the release notes.

I don’t like the reflective approach to testing private methods.

With the new additions to the AAA API, testing this class:

public class MyClass
{
    public string Public()
    {
        return this.Private();
    }

    private string Private()
    {
        throw new NotImplementedException();
    }
}

can be done like this:

[TestMethod]
[Isolated]
public void PrivateTest()
{
    MyClass fake = Isolate.Fake.Instance<MyClass>();

    Isolate.WhenCalled(() => fake.Public()).CallOriginal();

    Isolate.NonPublic.WhenCalled(fake, "Private").WillReturn("FAKE");

    string fakePublic = fake.Public();

    Assert.AreEqual("FAKE", fakePublic);

    Isolate.Verify.WasCalledWithExactArguments(() => fake.Public());

    Isolate.Verify.NonPublic.WasCalled(fake, "Private");
}

I would like it better if it was like this:

[TestMethod]
[Isolated]
public void PrivateTest()
{
    MyClass fake = Isolate.Fake.Instance<MyClass>();

    MyClass_Accessor fakeAccessor = MyClass_Accessor.AttachShadow(fake);

    Isolate.WhenCalled(() => fakeAccessor.Private()).WillReturn("FAKE");

    Isolate.WhenCalled(() => fake.Public()).CallOriginal();

    string fakePublic = fake.Public();

    Assert.AreEqual("FAKE", fakePublic);

    Isolate.Verify.WasCalledWithExactArguments(() => fake.Public());

    Isolate.Verify.WasCalledWithExactArguments(() => fakeAccessor.Private());
}

Looks almost the same but there aren’t any method names in the test code.

They were able to do it for Natural Mocks. I’m sure they will eventually do it for AAA.

Faking Output Parameters With Typemock Isolator

Some time ago I was asked if it was possible to fake output parameters with Typemock Isolator.

It’s actually very easy using any of the APIs.

Given this class:

public class MyClass
{
    public bool MyMethod(string input, out int output1, out double output2)
    {
        throw new NotImplementedException();
    }
}

Using the new AAA API, it's as clean as:

[TestMethod]
[Isolated]
public void TestMethodIsolated()
{
    MyClass target = Isolate.Fake.Instance<MyClass>();

    string input = "test value";
    int expectedOutput1 = 1;
    double expectedOutput2 = 2;

    Isolate.WhenCalled(() => target.MyMethod(input, out expectedOutput1, out expectedOutput2)).WillReturn(true);

    int output1;
    double output2;
    bool result = target.MyMethod(input, out output1, out output2);

    Assert.IsTrue(result);
    Assert.AreEqual<int>(expectedOutput1, output1);
    Assert.AreEqual<double>(expectedOutput2, output2);
}

Using Natural Mocks, it's as easy as:

[TestMethod]
[VerifyMocks]
public void TestMethodNatural()
{
    MyClass target = RecorderManager.CreateMockedObject<MyClass>();

    string input = "test value";
    int expectedOutput1 = 1;
    double expectedOutput2 = 2;

    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.ExpectAndReturn(target.MyMethod(input, out expectedOutput1, out expectedOutput2), true);
    }

    int output1;
    double output2;
    bool result = target.MyMethod(input, out output1, out output2);

    Assert.IsTrue(result);
    Assert.AreEqual<int>(expectedOutput1, output1);
    Assert.AreEqual<double>(expectedOutput2, output2);
}

It's also possible using Reflective Mocks:

[TestMethod]
[VerifyMocks]
public void TestMethodReflective()
{
    MockObject<MyClass> targetMock = MockManager.MockObject<MyClass>();

    string input = "test value";
    int expectedOutput1 = 1;
    double expectedOutput2 = 2;

    targetMock.ExpectAndReturn(
        "MyMethod",
        new DynamicReturnValue(delegate(object[] parameters, object context)
            {
                parameters[1] = expectedOutput1;
                parameters[2] = expectedOutput2;
                return true;
            }));

    int output1;
    double output2;
    bool result = targetMock.Object.MyMethod(input, out output1, out output2);

    Assert.IsTrue(result);
    Assert.AreEqual<int>(expectedOutput1, output1);
    Assert.AreEqual<double>(expectedOutput2, output2);
}

All you have to do is choose which one you like most.

LINQ With C# Book


Luís just broke the news on our LINQ with C# book.


I was honored with the invitation from Luís to write this book with him for FCA, for which he has already published a few books [^] [^] [^] before.


This will be an entry level book in Portuguese targeted to anyone wanting to learn LINQ with C#.


It has been a fun project with great discussions (only possible because we have half an ocean between us 🙂 ).

PDC2008: Meet Me In Los Angeles

It’s not the first time I try but, for one reason or another, this is the first time I will go to the PDC (Microsoft’s Professional Developers Conference).

According to the site “The PDC is designed for leading-edge developers and software architects. If you’re interested in the future of the Microsoft platform, you’re responsible for the technical strategy in your organization, or you’re a highly skilled developer who likes to delve deep into the heart of the platform, then the PDC is for you!”. If you fit this description, hurry up and register. Early bird discount has been extended until September 8th.

What I like most of these events is networking with Microsoft staff and other attendees. So, if you want to meet me, I’ll be glad to meet you.

Clone Detective For Visual Studio

Clone Detective for Visual Studio

Clone Detective is a tool that integrates with Visual Studio and uses the ConQAT (Continuous Quality Assessment Toolkit) to analyze C# projects and search for duplicated source code.

Watch the videos and see if this is the tool you were looking for.