Generic Strategy Pattern
When asked about what is the most important feature in .NET 2.0, I'll say: Generics! This feature does a lot of job now and a lot more in the future technologies (LINQ, etc.).
I was in a project, coded using VS 2005 Beta 2, so there are Generics there, also bugs. A day before deadline, I asked myself whether porting the code to 1.1 is better. The questions stopped when I realize that when moving back to 1.1, I'll have to change all those Generics to old-school inheritances... That's a no-go.
So, when working on a running project (due late January, will be released on freeware license), I've found out that those old ORM codes using Strategy Pattern is obsolete. I've created a new, more flexible approach using Generics and Strategy, and it's called Generic Strategy Pattern. Here is an example of the actual code:
Public
Interface IDataAccessProvider(Of [Object])
Event ObjectChanged(ByVal e As Collection(Of [Object]))
Function [Get]() As Collection(Of [Object])
Function [Get](ByVal id As Guid) As [Object]
Sub Add(ByVal O As [Object])
Sub Update(ByVal O As [Object])
Sub Delete(ByVal id As Guid)
End
Interface
This code highlights a basic ORM logic. The implementing class should define what Object (from the O part of the ORM) is to be mapped to relational form. Furthermore, by parameterizing the Object part I've been able to provide a flexible approach to the old Data Access Layer classes. Using this Interface, you can define a mapping for specific Object (well, Class) to specific Relational form. Afterwards, you will need to create a Class that contains an implementation of IDataAccessProvider for all Object that you need to be mapped. For example, I have two Objects (Classes), Foo and Bar. And I have two database implementation, SQLServer and XML. Using IDataAccessProvider, I have created 4 implementations, that is Foo-SQL, Foo-XML, Bar-SQL, and Bar-XML. The final step is compositing those implementations. I can use a data access layer where Foo is stored in a SQL Server database and Bar is stored in XML file, without the need of declaring another Interface. Helps a lot! Please contact me for a code sample. Those codes are not for public use (yet!).