I've started to notice that i my predicate building code looks similar. That's a sign to refactor it. Let's look at the code.
var predicate = PredicateBuilder.False<ArticleLikeInfo>();
// Under the covers it will get Guid from a cookie (one way to track a user)
Guid? key = _userKeyService.GetKey();
if (key.HasValue)
{
predicate = predicate.OrElse(x => x.UserKey == key.Value);
}
String ip = Request.UserHostAddress;
if (!String.IsNullOrEmpty(ip))
{
predicate = predicate.OrElse(x => x.UserIp == ip);
}
UserInfo user = _userSessionService.CurrentUser();
if (user != null)
{
predicate = predicate.OrElse(x => x.UserId == user.Id);
}
bool hasVoted = _articleService.GetLikes().Where(x => x.ArticleId == m.Id)
.Where(predicate)
.Any();
This code runs when i need to check if the user has pressed "like button" on the article. To trace the user i use
- Spy guid key from cookie
- Ip Address
- UserId if he has logged in
The same code runs when i issue ajax call to insert "like vote" for an article in the database.
So i would like to encapsulate it. My first idea was to opt for extensions method approach, but i dont like it, since i will have to resort to ServiceLocator pattern to get references to services like that
IUserKeyService service = ServiceLocator.Current.GetInstance<IUserKeyService>();
I feel that would be a bad design, since its not testable. I would like to have a class which is reusable and at the same time can be controlled by DI container. A class which i can inject dependencies into. Is it possible ?