06/16/10

Permalink 07:52:11 pm, by truewill Email , 94 words, 48 views   English (US)
Categories: .NET, Windows, Tools

It Must Be Microsoft's Fault

Two of us at work have had issues installing Developer Express components in Visual Studio 2008. We get “Cannot Register Toolbox Items” and have to manually add the components to the toolbox.

Apparently this is a known issue caused by the drivers for the Microsoft wireless mouse, and has been since 2008 (we’re installing the new components).

I find it hard to believe that Microsoft would not have fixed this if it were affecting all tool vendors.

As an aside, the DevX install GAC’d both the Web and XPO components that I deselected from the install.

05/20/10

Permalink 09:54:53 pm, by truewill Email , 18 words, 54 views   English (US)
Categories: Announcements [A]

Twit

I finally broke down and joined Twitter. Since Steve Gibson joined, I figured it was past time.

@BillSorensen

05/06/10

Permalink 09:52:47 pm, by truewill Email , 261 words, 266 views   English (US)
Categories: Tips, C#, IoC, .NET

Unity 2.0: Combining InjectionFactory and Automatic Factory

Unity 2.0 is out, and has some very nice features. Two of the coolest are InjectionFactory and Automatic Factories. Nice code samples and tutorials exist, but here’s an example of combining the two features.

using System;
using Microsoft.Practices.Unity;

namespace Unity2Example
{
  class Program
  {
    static void Main()
    {
      using (var container = new UnityContainer())
      {
        container
          .RegisterType<IService, ConcreteService>(
            new InjectionFactory(c => ServiceFactory()));

        Model model = container.Resolve<Model>();

        Console.WriteLine("\nCalling Execute...");
        model.Execute();
        Console.WriteLine("\nCalling Execute...");
        model.Execute();
      }
    }

    static IService ServiceFactory()
    {
      Console.WriteLine("ServiceFactory called.");
      return new ConcreteService();
    }
  }

  interface IService
  {
    void MakeTheCall();
  }

  class ConcreteService : IService
  {
    public ConcreteService()
    {
      Console.WriteLine("ConcreteService instantiated.");
    }

    public void MakeTheCall()
    {
      Console.WriteLine(
        "ConcreteService makes the call.");
    }
  }

  class Model
  {
    public Model(Func<IService> serviceFinder)
    {
      Console.WriteLine("Model instantiated.");

      _serviceFinder = serviceFinder;
    }

    public void Execute()
    {
      _serviceFinder().MakeTheCall();
    }

    private readonly Func<IService> _serviceFinder;
  }
}

This will display:

Model instantiated.

Calling Execute...
ServiceFactory called.
ConcreteService instantiated.
ConcreteService makes the call.

Calling Execute...
ServiceFactory called.
ConcreteService instantiated.
ConcreteService makes the call.

Essentially we’re injecting a Service Locator into our model. My use case for this was injecting an object where the object reference could change (it needed to be deserialized at certain times). This way the dependent class would always get the most current version. It turned out I already had a class that could act as a Service Locator with the addition of an interface, so I injected that instead (it seemed simpler). I think this combination may come in useful someday, though.

04/26/10

Permalink 06:49:37 pm, by truewill Email , 98 words, 70 views   English (US)
Categories: C#, .NET

Dynamic anonymous types

In C# 4.0, you can return anonymous types from methods by using the dynamic keyword.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(GetHello().Name);  // Works.

        var one = GetHello();
        var two = GetHello();

        if (one.Equals(two))
        {
            Console.WriteLine("They are equal!");
            // Hit - they are equal!
        }
        else
        {
            Console.WriteLine("They are NOT equal.");
        }
    }

    static dynamic GetHello()
    {
        return new { Name = "Bob" };
    }
}

Note that Equals still works as expected.

I think this will make it possible to refactor long LINQ statements that use anonymous types, breaking them up into calls to private methods. (Clearly this could be abused as well.)

Permalink 06:17:05 pm, by truewill Email , 18 words, 64 views   English (US)
Categories: Announcements [A], .NET, Conventions

News

Iowa Code Camp is coming up fast!

There’s also a new .NET group starting up in Iowa City.

:: Next Page >>

Development Central

Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.

Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.

| Next >

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 3

powered by b2evolution free blog software