Tuesday, April 10, 2007

HowTo: Application Block Software Factory (ABSF) Part III: An example

After an introduction to ABSF and a explanation about providers, here you have an example of how an application block and a provider library can be created. What I did was to take snapshots of the solution after executing each recipe:

1. New Animal Application Block
2. New Provider Factory and Base 'Animal'
3. New Provider (Typed) 'Dog'
4. New Provider (Untyped) 'Monkey'
5. New Design-Time Base Provider Node 'AnimalNode'
6. New Design-Time Provider Node 'DogNode'
7. New Provider Library
8. New Provider (Typed) 'Cat'
9. New Design-Time Provider Node 'CatNode'

The example is based on the Animal Application Block of Brian Button. Brian and Tom did a series of webcasts explaining the Entlib Core and Design-Time creating an Animal Application Block. You can download the webcasts from here and here.

The idea is that you can open each snapshot and take a look at what stuff is created after executing each recipe. In order to build the snapshots, you will have to copy the Common.dll, Configuration.Design.dll and ObjectBuilder.dll to the lib folder of each snapshot. I recommend you to unzip the snapshots in a directory near the root (c:\) because of the path length restriction (you won't be able to build the snapshots if the path is longer than 248 characters)  

Finally here you have the link to the snapshots!

Enjoy!

Labels: ,


Friday, April 06, 2007

Enterprise Library 3.0 Released!

I've been part of this excited project mostly developing the Application Block Software Factory (ABSF) and some parts of the Validation Application Block (VAB) (which core and intergation support was developed by Fernando). I want to thanks all to let me be part of this great experience and learn a lot from this spectacular people. 

You can download it, start using it and provide feedback! Here you have the links:

Download: http://www.microsoft.com/downloads/details.aspx?FamilyID=62ef5f79-daf2-43af-9897-d926f03b9e60&displaylang=en
Codeplex community site: http://www.codeplex.com/entlib
Tom Hollander's Blog: http://blogs.msdn.com/tomholl/
Olaf's Blog: http://bloggingabout.net/blogs/olaf

Enjoy!

Labels: ,


Wednesday, April 04, 2007

.Net Generic Finder (VS Automation Model Usage)

I know there are Find/FindAll methods in the System.Collection.* namespace but what I was looking for is something quite different. Here is more or less the scenario:

Suppose you have objects that can be any of the following interfaces: Solution (S), Project (P) or ProjectItem (PI). You don't have a common super type to treat all as polymorphic objects. So if you have a collection of them (Objects) you are not able to have Collection<T> where T is { S | P | PI } because you could have a mix of them (of course at least you have a Collection<Object> but in this case we would not need Generics anyway).

I wanted a finder that allows me to search for an object in this object collection but using the benefits of Generics. I wanted to use the most abstract representation of a collection I could have and I decided to use an IEnumerable parameter. Then I needed to define a Predicate<T> and what I am going to expect is a T or IEnumerable<T> result.

For example: If the col is = { S1, P1, P2, P3, PI1, PI2, PI3 }

The following tests should pass:

1. Finder.FindAll<Solution>(col, delegate( return Predicates.GetNullPredicate<T>())) contains {S1}
2. Finder.FindAll<Project>(col, delegate( return Predicates.GetNullPredicate<T>())) contains {P1, P2, P3}
3. Finder.FindAll<ProjectItem>(col, delegate( return Predicates.GetNullPredicate<T>())) contains {PI1, PI2, PI3}

So I am able to get a IEnumerable<T> where T is { S | P | PI }. I could also specify any other predicate to evaluate specific properties of each T. Notice that I am using the Predicates.Is<T> helper method I published in my previous post to validate if the predicate can be evaluated. I am also using the yield support to collect the result.

public static class Finder
{
public static T Find<T>(IEnumerable elements, Predicate<T> predicate) where T : class
{
IEnumerator<T> enumerator = Find(elements, predicate, true).GetEnumerator();
return enumerator.MoveNext() ? enumerator.Current : null;
}

public static IEnumerable<T> FindAll<T>(IEnumerable elements, Predicate<T> predicate) where T : class
{
return Find<T>(elements, predicate, false);
}

private static IEnumerable<T> Find<T>(IEnumerable elements, Predicate<T> predicate, bool returnFirst) where T : class
{
if (null != elements)
{
foreach (object element in elements)
{
if (Predicates.Is<T>(element, predicate))
{
yield return (T)element;
if (returnFirst) yield break;
}
}
}
yield break;
}
}

Labels: ,


And/Or/Is Predicates

The .Net fx doesn't have any support to compose Predicates so I decided to create a Predicates helper for a small project I was working on the last weeks. Here you have my composite predicate methods:

    public static class Predicates
{
public static Predicate<T> And<T>(Predicate<T> predicate, Predicate<T> anotherPredicate)
{
return delegate(T t) { return predicate(t) && anotherPredicate(t); };
}

public static Predicate<T> Or<T>(Predicate<T> predicate, Predicate<T> anotherPredicate)
{
return delegate(T t) { return predicate(t) || anotherPredicate(t); };
}

public static bool Is<T>(object target, Predicate<T> predicate) where T : class
{
T targetAsT = target as T;
if ( null != targetAsT )
{
return predicate(targetAsT);
}
return false;
}

public static Predicate<T> GetNullPredicate<T>()
{
return delegate(T t) { return true; };
}
}

The Is<T> method allows you to validate if the target object is of T type before evaluating the predicate. This is something I found very useful when I am working with VS Automation Model because there are "Objects" that can implement many many interfaces so you have a collection of those objects that may or not target the T type. I will publish another post with a Finder example.

 

Labels:


This page is powered by Blogger. Isn't yours?