When working with data structures like List or Collections etc, it may not be so advisable to expose those interface public to other users of the code. Wrote about it on
my blog for a while ago. I prefer to expose IEnumerable instead of a List, IList etc when only items should be listed. Sometimes it can be interesting to find if an item exists in the returned list. Instead of adding a ForEach and search for an item (will
eventually results in duplication of code), a Exists extension method could be added:
public static bool Exists<T>(this IEnumerable<T> value, Predicate<T> predicate)
{
foreach (var v in value)
{
if (predicate(v))
return true;
}
return false;
}
It can be used in the following way (Where Fees is a property returning
IEnumerable<Fee>:
Fees.Exists(fee => fee is NotificationFee)
Fork
1 Feedback
You must log in before you can give any feedback
0
0
0
@jeffhandley:
Good point!
The use of System.Linq will be needed and adds a lot of extension method into the context, methods others maybe shouldn't have access to at all in the current context. Everything to prevent a dev for using members they shouldn't use in the current context to protect the system from potential fails.
But you are right.. it may be duplicated when Any() exists in the current context.
-1
Reply to: FredrikN
Well maybe we should reimplementing all methods in System.Linq with that argument?
0
Reply to: NPehrsson
Or maybe re-implement you comment ;) hehe...
NP if you want a YAGNI friendly system (prefer) than System.Linq will give you to much methods that you don't really need, why expose hundreds of methods for others (coders in the future or in your team) and let them have the chance to add lot's of work around because of laziness? this is happens to often :(
I like the idea with the existing extension, not using it my self but the Any is no nice word for a methods that shall explain the user story eg: Check if a user exist. The nice thing med DDD is that you always translate you stories to some readable code with no more functions/methods needed to the context.
For me the word Any is more like functional oriented name and it execute a condition (method) ... Exist would be a better word in a DDDish context or in OOP ... This is my opinion... the Any is to abstract it can by anything. if you implement your own Any methods it will be like invent the wheel again, I'm aware of that, but it can also interfere negative not doing it in a specific context. That's why experts often use the word "It depends" because it really depends. For whom, for what, why? ,when? etc...
As I often tell people on my seminar regarding creativity. If you are the one following others rules, than you are always one step behind or more.
0
You must log in before you can post a comment


1.02k
0


Mark 'c#' tag as 'like'
Mark 'c#' tag as 'ignore'