Friday, July 12, 2013

Moving the blog

I'm in the process of moving this blog to and to start blogging again after some busy time... It will take me a few days more to complete moving everything and start publishing there...but i'll remove this blog when I complete the work...

Tuesday, June 15, 2010

Problem debugging with multiple processes

For some reasons when debugging applications using multiple threads i had an awkward behavior with Visual Studio 2008 SP1, where the debugger will skip some steps or act as I pressed F5.

After looking for a while in the web I arrived to a hotfix that solves this problem and it applies only for VS 2008 SP1.

http://support.microsoft.com/?scid=kb;en-us;957912&x=9&y=14

I hope this is useful for other people suffering the same issues.

Saturday, February 13, 2010

Is NHibernate difficult or why people do not read documentation

I found this post in google alert today

http://mattthr.blogspot.com/2010/02/quey-across-join-in-nhibernate.html

He claims this SQL is easy to write and he says that he has to work a lot with the highly “undocumented?” stupid framework called NH to make it work

SELECT * from product p
inner join sku s on s.id = p.id
WHERE p.Name like '%widget%'
OR s.SkuCode like '%wd_%'

If i write it as i write every query like this, the corresponding HQL will be something like:

SELECT p from product p
join p.SKU s
WHERE p.Name like '%widget%'
OR s.SkuCode like '%wd_%'

I mean is it easier in NH or what? And this feature is much documented…there is a trend of users trying to use criteria as the default…which i don’t think is right at all…criteria has its place and it will be overridden by a good implementation of Linq2NH but if all what you want is some translation of your SQL world to NH then you should use HQL.

Friday, November 20, 2009

Extending ICriterion to use a sql LIKE for a numeric field

I’m almost sure it is not ANSI-SQL but, in SQL Server you can do a Like query for an int field.

Using the Like Restriction in NHibernate won’t send a like for an int field, because it is not supported for every DB. I’m migrating an application which uses like for integer fields to find domain objects which some codes starts with a known prefix.

Considering that I’m working now with SQL Server and i don’t think about migrating the app to another database i decided to extend ICriterion to get this using a StartsWith restriction

The work is very easy, basically when i create the expression i convert it in a LikeExpression, and convert the value toString, in SQL if you send a string to a like for a numeric value it will return an empty result set which for me is good enough:

public class StartsWithExpression : AbstractCriterion
{
private readonly ICriterion realCriterion;

/// <summary>
///
Initialize a new instance of the <see cref="EqOrNullExpression" /> class for a named
/// Property and its value.
/// </summary>
/// <param name="propertyName">
The name of the Property in the class.</param>
/// <param name="value">
The value for the Property.</param>
public StartsWithExpression(string propertyName, object value)
{
realCriterion= new LikeExpression(propertyName, value.ToString(), MatchMode.Start);
}

public override string ToString()
{
return realCriterion.ToString();
}

public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return realCriterion.GetTypedValues(criteria, criteriaQuery);
}

public override IProjection[] GetProjections()
{
return realCriterion.GetProjections();
}

public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
IDictionary<string, IFilter> enabledFilters)
{
return realCriterion.ToSqlString(criteria, criteriaQuery, enabledFilters);
}
}


The code is ready in the uNhAddins source.



Next thing i want to do is use this in an Example query and get the EnableLike to work with it, but currently i didn’t find out how to extend Example for this.

Thursday, November 19, 2009

uNHAddins MultiSessionFactory and Conversation

I have been asked sometimes in the Blog comments, without time to answer, how to configure the conversation to use a MultiSessionFactoryProvider instead of the regular SessionFactoryProvider we use in most of the examples.

The real place to start this is Fabio’s Blog where he talks about the SessionFactory providers.

I took the ideas from there, configured the conversation and made a simple test that has a CrudModel for an animal and a person and shows them in a ConsoleApplication.

You can find the example in the uNHAddins source

For sure it is not the best example you will see, but you can see how easily you can configure your repositories / daos to access one factory or other using uNHAddins

Tuesday, July 7, 2009

Asserting an event was raised

I found the answer to this in some posts in the net. But i always forgot it and find my self looking for it again, so i will post the solution Im using here for future reference.

[Test]
public void during_single_load_getting_two_barcode_raises_event_with_them()
{
var eventRaised = false;

dispenserProcessManager = CreateStubbedDispenserManager();
  var barcodes = new[] { "4444455555", "4444455556" };
  busterManager.Stub(bm => bm.GetSingleLoadBarcodes(Context.TelephoneType)).Return(barcodes);

dispenserProcessManager.BarcodesRead += (sender, collection) =>
{
collection.Elements.Count().Should().Be.EqualTo(2);
collection.Elements.Should().Contain(barcodes[0]);
collection.Elements.Should().Contain(barcodes[1]);
eventRaised = true;
};

dispenserProcessManager.SingleLoad();

eventRaised.Should().Be.True();
}

Inside the event i’m checking that the event was raised with the right data, and i set the eventraised variable to true. At the end i check that i really hit that line.

The Should() syntax is a handy way of asserting (at least for me) using NunitEx

Friday, May 29, 2009

Rhino Mocks Constraints for Generic List

Today i was doing some tests that check that user input in a windows forms is ok and sends a notification to the user.

The notification is a list of Errors, each error has the control which has a problem and the message to show to the user, all of this to set the ErrorProvider.

Well i really want to test I’m sending the right things, but i don’t care if the error provider works fine, so i obviously decided to mock the view…

I’m not so acquainted with Rhino Mocks but find very nice the way to set the expectation for the list of errors that should be passed to the view.

view.Expect(v => v.NotifyErrors(null).Constraints(
List.Count(Is.Equal(1)) &&
Is.TypeOf<IEnumerable<Error>>() &&
List.Element(0, new PropertyIs("Message", Messages.ToDateCannotBeLessThanFromDate)));
The only thing i would like to improve is the string in the PropertyIs, i maybe missing some option to do e => e.Message == Messages.ToDateCannotBeLessThanFromDate