Development In a Tesseract

Observations and Developmental Experiences

  Home  |   Contact  |   Syndication    |   Login
  15 Posts | 0 Stories | 8 Comments | 0 Trackbacks

News

Twitter








Archives

Post Categories

Friday, February 12, 2010 #

I’ve recently been looking at better ways to combine IoC functionality with MEF composition.  I’ve created a workable solution using just MEF but there is some nasty boilerplate floating around when creating nested containers.

As an example to this issue, the data model contains a set of nested objects in which the Root object holds the MEF CompositionContainer and uses that container to satisfy imports on objects it creates.  At some point in the heirarchy, an object representing a context needs to add new features to a container to be used only in children of the context holding object. Currently, I’m using the code below to establish the nested container, but unfortunately, it is tying my code heavily to MEF through the direct use of the container and Import attributes.

  [Import]
  private CompositionContainer CompositionContainer { get; set; }
  
  private void OnImportsSatisfied()
  {
    // we replace the imported container with a new one in which we are adding 
    // context specific services.
    CompositionContainer = new CompositionContainer(CompositionContainer);
      
    var additionalStuff = new AdditionalStuff();
    var batch = new CompositionBatch();
    batch.AddExportedValue(additionalStuff);
    
    // Items deeper in the context chain are going to import this container
    batch.AddExportedValue(CompositionContainer);
    
    CompositionContainer.Compose(batch);
  }

As a starting point for decoupling this code, I’ve looked into the autofac library which with the version 2 experimental version has some MEF integration support.

The example Nicholas Blumhardt uses the Lazy<T> capabilities of .NET 4, but doesn’t really show the integration with MEF using the Catalog registration services.

By making some modifications to the code in his example, you can really show how the MEF components can be pulled into the autofac container very easily.

the first step is to connect the MEF export attributes to the meta data used by the logger imports.

    public interface ILogAppenderMetadata
    {
        string AppenderName { get; }
    }

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class)]
    public class LogAppenderAttribute : ExportAttribute, ILogAppenderMetadata
    {
        public LogAppenderAttribute() : base(typeof(ILogAppender))
        {
        }

        public string AppenderName { get; set; }
    }

The creation of this attribute will simplify the tagging of exports using the ILogAppender contract. The next step is to apply the new attribute to the logger implementations.

 

 [LogAppender(AppenderName="printer")]
 public class PrinterLogAppender : ILogAppender
  {
  ...
  }
  
  [LogAppender(AppenderName="screen")]
  public class ScreenLogAppender : ILogAppender
  {
  ...
  }

Finally, the Main method is modified to make use of the catalog registration services provided by autofac.

 

  static void Main(string[] args)
  {
    var builder = new ContainerBuilder();
    var catalog = new TypeCatalog(typeof (PrinterLogAppender), typeof (ScreenLogAppender));
    builder.RegisterComposablePartCatalog(catalog);
  
    builder.RegisterType<Program>();
    builder.RegisterType<Log>();
  
    using (var container = builder.Build())
    {
        container.Resolve<Program>().Run();
    }
  }

When the program is run, the container correctly picks up the MEF exports without any further reference to imports and exports.  What I need now is to understand a better way to create the nested containers using the autofac library rather than pure MEF.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, January 10, 2008 #

Being an ultra-introvert, it always freaks me out when a comment I make brings me out from under the radar.

I posted on the alt.net list to a thread concerning the pro's and cons.  Jeremy Miller posted an opinion on my comment, and I followed up with a comment on the post.

As a reflection on all that,  I'd like to say that I think that in priciple I'm inclined to agree with Jeremy.  In practice, I'm stuck with the Framework that led to the post.

I believe that my ideas of what a 'Framework' should be is changing.  Jeremy talks about code generation and the templates and IDE productivity stuff, but I think that I'm going a different route myself, although it wouldn't be the first time I was wrong.

What I've been currently trying to do, with the help of the language enhancements (anonymous delegates, generics, lambda expressions, etc.) is get libraries of limited scope reusable objects that can be modified through the use of generics and delegates to perform instance specific work.

What I am working on right now involves a ton of reuse,  many of our systems are basically made up of building blocks that are identical in specification.  Our framework involved creating blocks of reusable objects, along with the rules of validation, that could be dropped into place.

For the first round of systems (which were almost identical), this worked very well; however, as more less similar systems were included, the framework started to show strain.  My original point was that the framework would have been able to be adapted or evolved into a better form, except that the development contracts were written in such a way as to preclude any further development on the system in question (a bad choice by program management).  Once we finally got the money in place to make updates to the framework, the enhancements are stretching the capabilities of the framework.

One of the biggest issues I've had recently was to try to convince a team not to use the system for a project that didn't fit well with the design intent with the framework, and they suffered exactly the fate that Jeremy mentions in his post and would have been better off starting from scratch.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Tuesday, October 30, 2007 #

I've been working in organizations that adhere to some level of the CMMI for some time now.  And we are trying to fit what we can of agile programming into our process, and hopefully are achieving better results while still keeping the structure required of our contracts.

 

A long time ago I ran across this article which discusses some of the levels of process that are often encountered in the wild, but rarely discussed by SEI.

 

So here's the The Capability Im-Maturity Model (CIMM) as envisioned by Capt. Tom Shorsch USAF.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Wednesday, October 24, 2007 #

Chris Sells has been writing on working at home, and Scott Hanselman has as well.

As for myself, my employer frowns on working at home, although I am on a laptop, and we do have a VPN.  Several times I have gotten permission to work from home simply so I can focus on one project at a time and actually accomplish things.

My normal course of action is to keep accepting small tasks that 'should be done easily' and then have them pile up as the daily fires appear and consume my time.

Ultimately, my to do list threatens to suck my computer into the void due to its massive gravitational weight and I have to blow everyone off and get the plate clean again.

I'm not sure if working full time from home would work from me,  I certainly doubt my employer would buy into it.  But currently, I'm sitting at the office trying to play the same game of catch-up.  It's 10pm and I'd rather be home to do this work.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Earlier Jeff Brown posted some comments on some new features of MbUnit v3, which I responded to by how to answer the NUnit critics concerning isolation of test fixtures.

Jeff responded with what I think is a pretty good answer.  I'll have to think it through for a bit.

I believe that my original question was not so much a question of the validity of the approaches, but that after reading the book, I found that I had not considered the original issue of isolation via new test fixture classes.

I started with NUnit as my first experience and not JUnit, so the set and tear down mechanisms were just 'what was' rather than what should be.

As I've said in my other posts,  I am often constrained by decisions made from outside my organization and the choice of unit testing framework is no different so having the best arsenal of responses to the unavoidable battles for these types of choices is a good thing.



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Friday, October 19, 2007 #

I have started looking at improvements to the NMock2 code with respect to writing a branch of the code in .NET 3.5.

The availability of extension methods in C# 3.0 have solved a common code issue of mine where I want to use Dictionary.TryGetValue to see if there is a value in a dictionary for a given key and create a default if not.  Which leads to the following code.

public static class ExtensionMethods
{
    public static U SafeGetValue<T,U>(this Dictionary<T,U> dictionary, T key)
      where U:new()
    {
        U value;
        if (!dictionary.TryGetValue(key, out value))
        {
            dictionary[key] = value = new U();
        }
        return value;
    }

    public delegate U NewInstance<U>();

    public static U SafeGetValue<T, U>(this Dictionary<T, U> dictionary, T key, NewInstance<U> newInstance)
    {
        U value;
        if (!dictionary.TryGetValue(key, out value))
        {
            dictionary[key] = value = newInstance();
        }
        return value;
    }
}

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, September 20, 2007 #

I'm currently involved in getting mono to compile on Solaris 9, Solaris 2.5 and DEC Tru64 Unix.  I'll be posting more about my experiences with this.  Hopefully I can get it to work, because the projects I'm working on would be much better done in a cross-platform managed environment than having to worry about the details of C++ compatibility.  If anyone has any experience here, I'd appreciate pointers.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Wednesday, September 12, 2007 #

Currently I'm in the process of reviewing some code that was written and then had MbUnit tests written to back flush the requirements.  Since I'm part of a more traditional shop, the code has already been peer reviewed.  The tests, of course, revealed errors in the code that weren't caught as a part of the peer review; so the code was corrected and re-reviewed.  Of course, when the tests were reviewed, the tests were found to have errors, which precipitated further changes.

I addressed the approach we are using to fix this effect (although the fix won't be perfect due to the limitations of our process) in the post How to make the most of traditional peer reviews.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, September 06, 2007 #

It is entirely possible for something to have too many features.

In response to a question from a co-worker, we spent approximately 2 hours digging through the options and model structure of the Infragistics UltraGrid control to discover the way to get the bound rectangle of a row for a drag operation.

While I get the desire for everything to be super customizable, at some point it seems like overkill.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Thursday, August 23, 2007 #

Today I had an issue where an application that as a mixture of .NET, VB6 and C++ code needed to be debugged.  Since the component that I needed to look at was in the VB 6 portion, I attempted to debug from VB6 only to be met with a variety of bizarre errors.

I don't have visibility into the rest of the application, so I continued with the .NET 2005 debugging session, which executed correctly, and noticed that I could set a breakpoint in the VB 6 code when the unmanaged debugging support was turned on.

The debugger visualizations are not quite correct, the .NET objects were showing up a <void>, but I was surprised to see that you could do this at all.  I was able to trace through to the source of the error and move on.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati