Control the entry point of WPF application

The app.main() is gone in default auto-gened WPF application, there are some workarounds to add customized startup app class. I found adding Startup attribute then implement startup eventhandler is a eaiser and clean solution:

Adding Startup property to App.xaml:

<Application x:Class="StakeholderDemo.Layers.WpfUI.App"


Implement code:
public partial class App : Application
  void  App_StartUp(object sender, StartupEventArgs e)

Auto-wiring in StructureMap 2.5

While I was confused by other posts mentioning LookForRegistries() by scan assembly, Derik Whittaker’s post explained how to do auto-wire in SC2.5, that’s just what I want! But I couldn’t move this code into a Registry class. Meanwhile I saw Jeremy Miller kept saying:

Don’t ever make any calls to ObjectFactory within a Registry.

I’m not sure does this mean I am not supposed to do OBjectFactory.Configure() in my Registry. If no, how can I refactor this code?

        private static void SetupStructureMap()
                        // <a href="">Ingore config file</a>
                        x.UseDefaultStructureMapConfigFile = false


                        x.Scan(scanner =>


StructureMap Registry in WCF

Following the post by Jimmy Bogard,  with Oran’s example, I figured this out finally. So I can remove the xml config file for both winForm and WCFHost.

I was wondering how to do the similar process in WCF just like appStart in WinForm, even it was so complicated, but it’s feasible, by implementing my own wcf service Factory.

It works with CSLA just like a charm. The only thing you should keep in mind is, in unit-test, still use ObjectFactory.Inject(). Don’t try to touch registry unitll you want to test registry configuration itself.

Replace file content in NAnt task

As JP suggested, I always compile all my layered projects into a huge dll to make NAnt build file eaiser. This solution is perfect until I tried to embeb structuremap config file into cruise control. Because assembly file name changed, I have to find a way to replace the config file content in NAnt.

It was not easy. NAnt doesn’t have a replace task!

I don’t want hack into NAnt source to re-build my own version. It seems Jay Flowers, the creator of CI Factory has his modified version of NAnt which has replace task in it, but, no document to direct me how to use it.

OK, I have to call Ant task in NAnt build file then.

[NAnt build file]
<exec program="c:\ant\bin\ant.bat" basedir="${base.dir} ">
<arg value="-Dcurrentproject.lib=${currentproject.lib}" />

    [Ant file]
    <project name="config" default="replace_config" basedir=".">

    <property name="build.dir" value="${basedir}\build"/>
    <property name="currentproject.lib" value="undefined"/>

    <target name="replace_config">
    <replace file="${build.dir}\StructureMap.config">
    <replacefilter token=",mybizlayer" value=",${currentproject.lib}"  />
    <replacefilter token=",mydatalayer" value=",${currentproject.lib}"  />



Using StructureMap to mock DataAccess in CSLA BO

I had always thought StructureMap is just another IOC tool similar to Castle windsor, until yesterday I read Karl Seguin’s Foundations of Programming, Chapter 4, I was shocked immediately by the injectStub method provided by StructureMap, which is now renamed to inject(). I think this explicitly injection is better than TypeMock implicitly mock instance object.

For example, I have an Organization BO,

public static OrganizationBO GetOrganizationBO(int Id)
return DataPortal.Fetch<OrganizationBO>(new SingleCriteria<OrganizationBO, int>(Id));

private void DataPortal_Fetch(SingleCriteria<OrganizationBO, int> criteria)
OrganizationDTO data = DataAccess.OrganizationRepository().FindById(criteria.Value);

if (data != null)

The repository is declared in DataAccess class to centralize all the dataaccess coupling inside CALS BOs.

public static IRepository<OrganizationDTO> OrganizationRepository()

// instead of direct coupling return new OrganizationRepository();
// I use StrcutureMap
return ObjectFactory.GetInstance<IRepository<OrganizationDTO>>();

The content of IOC config file:

PluginType=”DemoApp.Data.Interfaces.IRepository`1,DemoApp.Data”    PluggedType=”DemoApp.Data.Repository.OrganizationRepository,DemoApp.Data”

We can also use other different ways to set dependency, like setDefault in code, or search assembly at  runtime as shown in this post.

Test  code:

public void CanGetFromInjectedMockRepository()
var mockRepository = new Moq.Mock<IRepository<OrganizationDTO>>();

// Id in MoQ matters. But TypeMock doesn't care.
mockRepository.Expect(x => x.FindById(8172)).Returns(
new OrganizationDTO
Id = 1,
Name = "A test org name"

ObjectFactory.Inject(typeof(IRepository<OrganizationDTO>), mockRepository.Object);

var o = OrganizationBO.GetOrganizationBO(8172);

Assert.AreEqual(1, o.Id);
Assert.AreEqual("A test org name", o.Name);

Here is an artile introducing StructureMap, as it mentioned, StrucutreMap is very good choice if you are working on a class has no “constructor injection”, CSLA is one of those!

I have the same feeling, as the author Karl Seguin said, constructor injection doesn’t really solve the extensibility problem; how does someone add(try to replace) a new plug-in without recompiling the entire application?

The problem/drawback to DI is also obvious, the xml config file keeps growing and expanding. The performance and mantiance will become an issue. That’s why Boo was inverted to save / speed up Castle Windsor. But what about structure Map?

Also I need tool / method to test this xml config file, see details here and here. Unfortunately, I couldn’t make this <structuremap.verification> task loaded into NAnt, (maybe this doesn’t exist in those two dlls at all? I can’t tell because NAnt loadTask doesn’t have output). I tried to different folders and also “Forced scan” using <loadtasks> command, neither one works. Thanks God, Jeremy does have another way to test config file,so called smock test:


Regarding unit-test and mock, I spent a lot time trying to figure out how to reset the default back. It turns out the documented ObjectFactory.resetdefault has been changed to reset.