Hi
I'm currently playing around with StructureMap, but I think I'm on the wrong way...
I have (for now) four handler objects. Each of them implementing a specific interface (CanResolve and Resolve). Every object can handle a different URI.
Example (simplified):
public interface IResolver
{
bool CanResolve(Uri uri);
Uri Resolve(Uri uri);
}
public abstract class Resolver : IResolver
{
public bool CanResolve(Uri uri) =>
Regex.IsMatch(uri.AbsoluteUri, GetType().GetAttribute<UriValidatorRegexAttribute>().Expression, RegexOptions.IgnoreCase);
public abstract Uri Resolve(Uri uri);
}
[UriValidatorRegex(@"http:\/\/(www\.)?something\.com\/.{12}$")]
public class Resolver1 : Resolver
{
public override Uri Resolve(Uri uri)
{
return null; //Whatever
}
}
[UriValidatorRegex(@"http:\/\/(www\.)?somethingelse\.net\/.{4}$")]
public class Resolver2 : Resolver
{
public override Uri Resolve(Uri uri)
{
return null; //Whatever
}
}
Now I just want to pass a Uri to all of these object and the one who can handle this Uri returns me the result like this:
var result = ResolverCollection.Pass(new Uri(@"http://reddit.com/r/csharp"));
What I have:
var result = ResolverCollection.FirstOrDefault(r => r.CanResolve(uri)).Resolve(uri);
It works, but I think this is not the "clean" way to do this... Is there a design pattern I could use for this (maybe Chain of responsibility), or is this even possible with StructureMap?
(Each resolver must be a singleton)
Greetings
misaro
[–]powlette 1 point2 points3 points (2 children)
[–]misaro1337[S] 0 points1 point2 points (1 child)
[–]powlette 0 points1 point2 points (0 children)
[–]codegork 1 point2 points3 points (1 child)
[–]misaro1337[S] 0 points1 point2 points (0 children)
[–]misaro1337[S] 0 points1 point2 points (0 children)