I have covered the IDisposable interface in my previous posts IDisposable and using in C# and Implementing IDisposable. To make the implementation of IDisposable easier, I have written an abstract base class that handles the details of the IDisposable pattern. The objectives of the class is to:
Provide a simple, reusable implementation of the disposable pattern.
There are lot of code generation tools available. Microsoft Visual Studio has had code generation possibilities since I started using it in the mid 90-ties. Code generation can be a blessing for getting something up and running quickly, but also a curse when maintaining code.
Whenever a code generation tool is considered, there are two very important aspects that need to be considered:
Is there support for regeneration when the code needs to be updated?
Are there possibilities to make adaptions without loosing them on regeneration?
Unfortunately a lot of the tools fail on at least one of these points. The blessing that helped creating something working quickly becomes a curse during further development and maintenance.
Controls that can be used with data binding typically expect some kind of collection as the data source. One example is the ASP.NET DetailsView control. It displays a single record, but still expects a collection (to be exact: something that implements IEnumerable) to be assigned to its DataSource property.
I use a small extension method called WrapInEnumerable to help in this situation:
publicstaticclass ObjectExtensions
{publicstatic IEnumerable<T> WrapInEnumerable<T>(this T t){yieldreturn t;}}
public static class ObjectExtensions
{
public static IEnumerable<T> WrapInEnumerable<T>(this T t)
{
yield return t;
}
}
With this function it is easy to use the DetailsView for displaying a single object: