Fluent Interface for NH Facility – Take 1

27. June 2009

We’ve been getting many requests on having fluent configuration for NHibernate Integration Facility, and as I like programmatic configuration more than XML configuration (did I mention that I hate XML?), I decided to work on it. After 2-3 hours, I got the below more or less working

container.Register(
Fluently.ConfigureFacility()
    .Id("nhibernateFacility")
    .DefaultConfigurationBuilder<DefaultConfigurationBuilder>()
    .DefaultConfigurationPersister<DefaultConfigurationPersister>()
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias("myAlias")
                        .Id("myId")
                        .UsingConfiguration(
                        FactoryConfigurator.DefaultBuilder()
                            .ConnectionProvider("…………………………")
                            .ConnectionDriver("…………………………")
                            .ConnectionString("…………………………")
                            .Dialect("…………………………")
                            .ProxyFactory("…………………………")
                            .Assemblies("…………………………")))
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias("myAlias")
                        .Id("myId")
                        .UsingConfiguration(
                        FactoryConfigurator.XmlBuilder().File("myFile.xml"))));

 

The pieces in Italic are not necessary to write as they have their defaults, also we have some generic overloads for things like Dialect, ProxyFactory and ConnectionProvider.

What the above interface does is that it actually converts all the above configuration to IConfiguration and add them to the container as Facility Configuration, this is actually what is done behind the scenes when you use XML configuration.

I asked for a review over the syntax from several tweeps (@kkozmic, @dagda1, @mikehadlow, @chriscanal) and from one other NH Facility user, German Schuager, and got great feedback.

One of the issues that they pointed out with the above interface is that it is less discoverable. You have to find Fluently class, and then FactoryConfigurator class. Another issue is that it feels less natural to configure the facility like this. Instead, they prefer the configuration take place right on the facility.

This one is what German Schuager has suggested

container.AddFacility<NHibernateFacility>("nhibernateFacility", cfg => cfg
        .DefaultConfigurationBuilder<DefaultConfigurationBuilder>()
        .DefaultConfigurationPersister<DefaultConfigurationPersister>()
        .AddFactory("id1", f => f
                .Alias("myAlias")
                .UsingConfiguration<DefaultBuilder>(c => c
                        .ConnectionProvider("………")
                        .ConnectionDriver("………")
                        .ConnectionString("………")
                        .Dialect("………")
                        .ProxyFactory("………")
                        .Assemblies("………")
                ))
        .AddFactory("id2", f => f
                .Alias("alias")
                .UsingConfiguration<XmlBuilder>(c => c
                        .ReadFrom("myfile.xml")
                ))
 });

and similar one from Krzysztof Kozmic.

Looks like i was the only one that is fan of Castle Microkernel style Fluent Interface.

Let’s see what I’ll come up for the second take, If I ever do it.

Two VANs on Castle Project

27. June 2009

I like VAN meetings, and I believe I am gaining a lot from them. Due to some time zone problem, I remember waiting 1 hour in front of the PC :) I had even plans for presenting something on those VANs but nah things don’t go like the way I want. Zachariah Young informed me that there are 2 VANs in the following two weeks that cover some topic on Castle. I believe it would be a good idea to join to those meetings, even if you’re using some other framework or don’t use anything at all.

Ryan will be doing a two part series on the Castle Project.  Mark your calendar for some Castle Project fun.

A little information on Ryan Svihla
Ryan Svihla has been working as a C# developer Farm Bureau Bank in San Antonio since September 2007. Before that he worked as  a Consultant in Lincoln, NE for 3 years, where he had working experience with Php, some Perl, Python and of course C#.  Attemping Agile since early 2008 as an eager student with a focus on making programming more useful and relevant for the end user.


IoC and Dip through Castle Windsor
I assume readers have some familiarity with IoC and DI, so I skip description
Central Daylight Time
Start Time: Web, July 1, 2009 8:00 PM UTC/GMT -5 hours
End Time: Web, July 1, 2009 10:00 PM UTC/GMT -5 hours
Attendee URL: http://snipr.com/virtualaltnet (Live Meeting)


Web Development with Castle Monorail, Active Record and Brail view engine
Have a look at the first popular MVC  .Net based web framework. Also will be covering persistance with ActiveRecord, and view templates using Brail.  Bonus, will demo a plugin framework for building CMS like applications.
Central Daylight Time
Start Time: Web, July 8, 2009 8:00 PM UTC/GMT -5 hours
End Time: Web, July 8, 2009 10:00 PM UTC/GMT -5 hours
Attendee URL: http://snipr.com/virtualaltnet (Live Meeting)


For more info on VAN go to www.virtualaltnet.com

I’ll try to join those, see you there!

VS Addin: Fast Add Reference Dialog - No more Coffee Break, Steve!

18. June 2009

UPDATE 3: The project is available on Google Code

UPDATE 2: Now solution folders and some other stuff are done, thanks to Huseyin Tufekcilerli

UPDATE: I updated the dlls and the source in the link, now it doesn't crash VS. Hopefully the only problem left is that solutions with multiple solution folders won't be reflected in Projects tab.

I have spent a couple of days on implementing a fast add reference dialog box for Visual Studio(with some help from an existing addin). The time of adding a reference has been a great time to have some coffee, to have lunch, or even a way to save economy (remember, developer time costs money!). If you don’t believe me, take a look at what tweeps say.

It is actually easy once you find some of the extension points of Visual Studio.

In my case, I had to implement IDTExtensibility2 which contains method signatures to be called when a plugin is loaded, unloaded etc.

As first step, I had added “Add Quick Reference” item to the context menu when a project is right clicked. This is the most tricky part as there is almost no documentation on that.

The method that we should implement for this is:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)

The type of the application parameter implements DTE2 interface and the addInInst parameter implements AddIn interface, which has all we need.

The DTE2 interface has CommandBars property which gives us access to various VS stuff. The matter is to find the right one. After for-looping all CommandBar item, i found that the place I should add my custom item is “Context Menus”->”Project and Solution Context Menus”->”Reference Root”. Man this is hard to locate! Looking forward to MEF in 2010!

applicationObject = (DTE2)application;
addInInstance = (AddIn)addInInst;
CommandBars cb = applicationObject.CommandBars as CommandBars;
CommandBar bar = cb["Context Menus"];
CommandBarPopup cbarControl = bar.Controls["Project and Solution Context Menus"] as CommandBarPopup;
var commandBarControls = cbarControl.Controls;
this.referenceRoots = commandBarControls["Reference Root"] as CommandBarPopup;button = this.referenceRoots.Controls.Add(MsoControlType.msoControlButton,
                           System.Reflection.Missing.Value,
                           System.Reflection.Missing.Value, 1, true)   as CommandBarButton;
button.Caption = "Add Quick Reference";
button.Click += oControl_Click;

There is one thing to be careful about: If you want to handle events of a button, for example, you should hold a reference in your class. Local method variables wouldn’t work. This situation is better told here

Now, I am done. I should now design the dialog itself that looks very similar to the original one. There is another problem: There is no "Windows Explorer Like” control for Windows Forms. There is OpenFileDialog but it is a dialog, not a control. I found the most similar one at GongShell Project which is licensed under GPL.

My current screen looks like the one below:

image

Very similar, even if I say so myself.

One thing to notice here is that the first tab is browse(.NET in original dialog), second is .net (Browse in original dialog) and third one is Projects. I haven’t written “Recent” part as I don’t want to deal with I/O really.

I believe that Browse window is more frequently used than .NET tab. There can be more improvements on that screen, such as having “common” tab which does the copying of commonly used references to a new project but hey this is a demonstration only ! :)

The items in the .NET tab are loaded in the background, and once it is loaded, it will be cached during the lifetime of the application.

I won’t comment more on the code, go grab it and try it. I wont continue developing this little addin, so you are free to do it on your own. Just drop me an email when you do it, though.

I just warn you: Com stuff is like walking on a mine field, and I am not taking any responsibility in case you loose data.

Download the code and the binaries here and put the binaries into Documents\Visual Studio 2008\Addins folder. Have fun!