Lansing Day of Dot Net 2008 a success!

clock June 23, 2008 01:09 by author Ryan Montgomery

For all those who made it out the the Lansing DODN, Thank you for attending my session on Dependency Injection with Spring.NET. I hope I was able to set some folks on the path to IoC enlightenment, or at least break any barriers in your way to getting started. I also got to attend some other great sessions by Len Smith, Jay Wren, and Michael Eaton on NHibernate, Inversion of Control, and Active Record, respectively.

There were some good questions asked during the discussion so I've posted the question and answerers below. Please forgive me if they are not verbatim.

Questions Answered

Q - Do you have to configure every member of an object in the container?

A - No. Spring.NET will use a mechanism called Autowiring to infer the object instantiation and configuration. Autowire is set to 'No' by default and you are encouraged not to change this for large applications, since specifying your collaborators explicitly gives you a feeling for what you're actually doing (always a bonus) and is a great way of somewhat documenting the structure of your system. Setting Autowire to 'byType'  will resolve collaborators by type instead of by name.

See 5.3.5 Autowiring collaborators

Q - Does Spring.NET configure objects as singletons by default?

A - Yes. But turning this off is as easy as adding singleton="false" to the object definition in the configuration file.

See 5.4 Object Scopes

Q - So which is better, Castle Windsor or Spring.NET?

A - It depends what you're doing. You might find that you use Windsor for one thing and Spring.NET for a another. While both frameworks have some overlaps in functionality, there are also some stark differences that give each one it's place in the world. Why not try both? They're free to download, and if you follow good OO practices, then you shouldn't be tightly coupled to either framework.

Materials

I've also posted the slides, demo, and a video of the presentation for those who are interested.

1. Slides

 

2. Demo

3. Video


Lansing Day of Dot Net from Ryan Montgomery on Vimeo.

Thank you Matt for taking the video!



Calling Stored Procedures in NHibernate using Spring.NET

clock June 11, 2008 17:32 by author ryan montgomery

Recently I ran into a situation with NHibernate where I needed to call a stored procedure. Ok. That should be easy right? Well, not so much. I mean it wasn't rocket science, but it didn't feel like a first class citizen in the NHibernate framework, and maybe it's not supposed to be...I don't know.

So I googled it, of course, and I found a few related blog posts referencing the different ways of going about this. The problems started when my stored procedure needed to return multiple rows and not a scalar value. By problem I mean I had to do another Google search. :)

I found the following solution:

public IList<IPityTheFoo> FindByFoo(string foo) { return HibernateTemplate.SessionFactory.GetCurrentSession() .GetNamedQuery("StoredProcedureName") .SetParameter("Foo", foo) .List<IPityTheFoo>(); }

The general idea here is that you need to call GetNamedQuery which expects the name of the query as defined by your Hibernate Mapping File:

<sql-query name="StoredProcedureName"> <return class="IPityTheFoo"> <return-property name="Foo" column="Foo"/> </return> exec [dbo].[StoredProcedureName] ? </sql-query>

The Spring.NET HibernateTemplate exposes the current session through the SessionFactory.GetCurrentSession(). To return the collection of your objects simpoly call List<T>().



The Lansing, MI Day of Dot Net 2008!

clock June 10, 2008 18:21 by author ryan montgomery

In a mix of excitement and nausea, I have been chosen to speak at the Lansing Day of Dot Net 2008! My session, 'An Introduction to Dependency Injection using Spring.NET' was selected and I will be speaking on the subject there June 21st, 2008. It all started when I found the www.dayofdotnet.org site while browsing the Internet and happened to see they were looking for speakers. I figured "hey that would be cool" and I submitted my application.

I am very excited to be speaking, but I also feel the nausea that comes with speaking in front of large crowds. I'm a decent public speaker, but this will be the first time I'll be speaking to a room of my peers and mentors talking about technology - hence the nausea.

See you there!



S#arp Architecture

clock June 10, 2008 18:18 by author ryan montgomery

I've been working with NHibernate and Spring.NET recently on a project and have had to struggle with learning these new technologies of which I had no experience with before. I think I've reached that point where I 'get it' and now I'm able to implement real business value, which is the general purpose of all frameworks.

Taking these frameworks a step further, S#arp Architecture, pronounced 'Sharp' Architecture, makes working with NHibernate, Spring.NET, and ASP.NET MVC even easier! One thing S#arp Architecture provides is a Generic DAO which enables your custom DAO's (Customer, Order, etc.) to remain very light, as they only need provide implementation code when doing more than Save, Update, or Delete. The example below shows a CustomDAO which can Save, Update, SaveOrUpdate, Delete, etc... The only "special" method is the FindByCountry method.

S#arp Architecture Generic DAO in action.

1 public class CustomerDao : GenericDaoWithTypedId<Customer, string>, ICustomerDao 2 { 3 public List<Customer> FindByCountry(string countryName) 4 { 5 ICriteria criteria = Session.CreateCriteria(typeof(Customer)) 6 .Add(Expression.Eq("Country", countryName)); 7 8 return criteria.List<Customer>() as List<Customer>; 9 } 10 }