IList<> and the Command Pattern

Mar 3 2008 12:59 AM

Hi there!

Hmm, this is probably me just being dim about Generics, but I am trying to return a 'generic' list from a series of Browse handlers using the Command Pattern.  The call is made from a generic place and does the following:

DomainEntityBrowseHandlerFactory.Create(domainEntityType).Execute(searchCriteria)

The code for the factory is as follows

public static class DomainEntityBrowseHandlerFactory

{

public static IDomainEntityBrowseHandler Create(string domainEntityType)

{

if (domainEntityType == typeof(CreditReviewStatus).Name)

return new CreditReviewStatusBrowseHandler();

else if (domainEntityType == typeof(CreditReviewType).Name)

return new CreditReviewTypeBrowseHandler();

else if (domainEntityType == typeof(CreditRiskStatus).Name)

return new CreditRiskStatusBrowseHandler();

else if (domainEntityType == typeof(CreditRiskStatusReason).Name)

return new CreditRiskStatusReasonBrowseHandler();

else

throw new PresentationLayerException(String.Format("Unable to locate {0} Browse request handler"));

}

}

It attempts to return a Handler which implements the IDomainEntityBrowseHandler interface, which is defined as follows:

public interface IDomainEntityBrowseHandler<TEntity>

where TEntity : NamedDomainEntity

{

/// <summary>

/// Provides a contract for all the simple entity browsers

/// </summary>

/// <param name="searchCriteria"></param>

/// <returns></returns>

IList<TEntity> Execute(NamedDomainEntitySearchCriteria searchCriteria);

}

an example of one of the implmentations of the handler is as follows

public class CreditReviewTypeBrowseHandler : IDomainEntityBrowseHandler<CreditReviewType>

{

#region IDomainEntityBrowseHandler Members

public IList<CreditReviewType> Execute(NamedDomainEntitySearchCriteria searchCriteria)

{

return DomainEntityService<CreditReviewType>.FindBySearchCriteria(

RepositoryFactory.Instance.GetBaseRepository<CreditReviewType>(

NamedDomainEntityConfigurationFactory.GetUsageArguments<CreditReviewType>()

),

searchCriteria

);

}

#endregion

}

All the handler return an IList of their specific type, and they all inherit from NamedDomainEntity, so I figure that will work, as I resolve the TEntity in each handler, unfortunately it is the factory which fails me, in that where I've specified the return type for the Create method it is expecting IDomainEntityBrowseHandler to have <xxx> in it.  Now I obviously can't put a specific type in here because it doesn't know which one it will return at this point, if I do the one I put in is accepted but all the other 'if's fail.  I can't put TEntity in there and specify a where clause because the compiler doesn't allow it.

I've tried this and it compiles, but surely this means that I have to specify what type it is when I call create, and I don't know that, so the object is defeated, if you'll pardon the pun!

public static IDomainEntityBrowseHandler<TEntity> Create<TEntity>(string domainEntityType)

where TEntity : NamedDomainEntity

{

if (domainEntityType == typeof(CreditReviewStatus).Name)

return new CreditReviewStatusBrowseHandler() as IDomainEntityBrowseHandler<TEntity>;

else if (domainEntityType == typeof(CreditReviewType).Name)

return new CreditReviewTypeBrowseHandler() as IDomainEntityBrowseHandler<TEntity>;

else if (domainEntityType == typeof(CreditRiskStatus).Name)

return new CreditRiskStatusBrowseHandler() as IDomainEntityBrowseHandler<TEntity>;

else if (domainEntityType == typeof(CreditRiskStatusReason).Name)

return new CreditRiskStatusReasonBrowseHandler() as IDomainEntityBrowseHandler<TEntity>;

else

throw new PresentationLayerException(String.Format("Unable to locate {0} Browse request handler", domainEntityType));

}

So I'm stumped, I would be stoked if someone could point me in the right direction

Many thanks in advance

Colin


Answers (1)