我正在编写一些编写得不好的代码,并涉及一些我希望重构的相当复杂的逻辑。主题是规则的验证和报告可能的违规行为。不幸的是,课程设计相当奇怪,所以我遇到了一些IEnumerable挑战。
作为简化示例,我有以下内容:
IEnumerable<RuleDefinition>
IEnumerable<Request>
,其中
public class RuleDefinition
{
public RequestType ConcerningRequestType { get; set; }
public int MinimumDistanceBetweenRequests { get; set; }
}
public class Request
{
public int TimeIndex { get; set; }
public RequestType TypeOfThisRequest { get; set; }
}
显然,当请求类型匹配且两个请求之间的间隔(TimeIndex)太短时,违反了规则。现在,我想提取:
所以在我们的案例中,我想获得这样的东西:
public class Violation
{
public RuleDefinition ViolatedRule { get; set; }
public Request FirstRequest { get; set; }
public Request SecondRequest { get; set; }
}
我认为这是一个相当简单的问题,但我没有想出一个可以被称为良好可读和可维护的解决方案。我尝试了各种各样的事情......它总是变得非常混乱(我只是试图实现这个例子,这很糟糕)
在这种情况下使用的任何想法,模式? (Resharper经常正确地建议。选择很多,但这会使事情变得更不可读)
编辑:这是我漫长而丑陋的实现。 ;)var ruleDefinitions = new List<RuleDefinition>
{
new RuleDefinition {
ConcerningRequestType = RequestType.Exclusive,
MinimumDistanceBetweenRequests = 2}
};
var requests = new List<Request>()
{
new Request { TimeIndex = 1, TypeOfThisRequest = RequestType.Normal },
new Request { TimeIndex = 1, TypeOfThisRequest = RequestType.Normal },
new Request { TimeIndex = 2, TypeOfThisRequest = RequestType.Normal },
new Request { TimeIndex = 3, TypeOfThisRequest = RequestType.Exclusive },
new Request { TimeIndex = 4, TypeOfThisRequest = RequestType.Exclusive },
};
var violations = new List<Violation>();
foreach (var rule in ruleDefinitions)
{
var requestsMatchingType = requests.Where(r => r.TypeOfThisRequest == rule.ConcerningRequestType);
foreach (var firstRequest in requestsMatchingType)
{
var collidingRequest = requests.FirstOrDefault(secondRequest =>
secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < rule.MinimumDistanceBetweenRequests);
if (collidingRequest != null)
{
violations.Add(new Violation
{
ViolatedRule = rule,
FirstRequest = firstRequest,
SecondRequest = collidingRequest
});
}
}
}
Console.WriteLine("found {0} violations.", violations.Count());
Console.ReadKey();
答案 0 :(得分:2)
这不是一项简单的任务,所以我要做的第一件事是定义一个界面,看看我需要什么:
interface IViolationFinder
{
IEnumerable<Violation> Search(
IEnumerable<RuleDefinition> ruleDefinitions,
IEnumerable<Request> requests);
}
现在我们清楚地看到我们需要实施的内容。因为你的搜索逻辑非常复杂,我认为你不应该用一个linq表达它。你可以,但你不应该。这两个嵌套的foreq循环内嵌linq是非常讨厌的,我不认为它对linq本身更干净。
您需要在实施中制作更多方法。它会增加可读性。所以天真的实现就是这个(这是你的):
class ViolationFinder : IViolationFinder
{
public IEnumerable<Violation> Search(IEnumerable<RuleDefinition> ruleDefinitions, IEnumerable<Request> requests)
{
var violations = new List<Violation>();
foreach (var rule in ruleDefinitions)
{
var requestsMatchingType = requests.Where(r => r.TypeOfThisRequest == rule.ConcerningRequestType);
foreach (var firstRequest in requestsMatchingType)
{
var collidingRequest = requests.FirstOrDefault(secondRequest =>
secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < rule.MinimumDistanceBetweenRequests);
if (collidingRequest != null)
{
violations.Add(new Violation
{
ViolatedRule = rule,
FirstRequest = firstRequest,
SecondRequest = collidingRequest
});
}
}
}
return violations;
}
}
你可以开始重构这个。我们不是在一种方法中思考,而是提取最明显的部分:
class ViolationFinder : IViolationFinder
{
public IEnumerable<Violation> Search(IEnumerable<RuleDefinition> ruleDefinitions, IEnumerable<Request> requests)
{
var violations = new List<Violation>();
foreach (RuleDefinition rule in ruleDefinitions)
{
IEnumerable<Request> requestsMatchingType = requests.Where(r => r.TypeOfThisRequest == rule.ConcerningRequestType);
violations.AddRange(
FindViolationsInRequests(requestsMatchingType, requests, rule));
}
return violations;
}
private IEnumerable<Violation> FindViolationsInRequests(
IEnumerable<Request> matchingRequests,
IEnumerable<Request> allRequest,
RuleDefinition rule)
{
foreach (Request firstRequest in matchingRequests)
{
var collidingRequest = allRequest.FirstOrDefault(secondRequest =>
secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < rule.MinimumDistanceBetweenRequests);
if (collidingRequest != null)
{
yield return new Violation
{
ViolatedRule = rule,
FirstRequest = firstRequest,
SecondRequest = collidingRequest
};
}
}
}
}
搜索几乎是干净的,但我们看到FindViolationsInRequests获取每个请求和规则,因此传递过滤的请求列表是没用的。所以我们这样做:
class ViolationFinder : IViolationFinder
{
public IEnumerable<Violation> Search(IEnumerable<RuleDefinition> ruleDefinitions, IEnumerable<Request> requests)
{
var violations = new List<Violation>();
foreach (RuleDefinition rule in ruleDefinitions)
{
violations.AddRange(FindViolationsInRequests(requests, rule));
}
return violations;
}
private IEnumerable<Violation> FindViolationsInRequests(
IEnumerable<Request> allRequest,
RuleDefinition rule)
{
foreach (Request firstRequest in FindMatchingRequests(allRequest, rule))
{
var collidingRequest = allRequest.FirstOrDefault(secondRequest =>
secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < rule.MinimumDistanceBetweenRequests);
if (collidingRequest != null)
{
yield return new Violation
{
ViolatedRule = rule,
FirstRequest = firstRequest,
SecondRequest = collidingRequest
};
}
}
}
private IEnumerable<Request> FindMatchingRequests(IEnumerable<Request> requests, RuleDefinition rule)
{
return requests.Where(r => r.TypeOfThisRequest == rule.ConcerningRequestType);
}
}
接下来的事情是
var collidingRequest = allRequest.FirstOrDefault(secondRequest =>
secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < rule.MinimumDistanceBetweenRequests);
足够复杂,可以为它制定一些方法:
class ViolationFinder : IViolationFinder
{
public IEnumerable<Violation> Search(IEnumerable<RuleDefinition> ruleDefinitions, IEnumerable<Request> requests)
{
var violations = new List<Violation>();
foreach (RuleDefinition rule in ruleDefinitions)
{
violations.AddRange(FindViolationsInRequests(requests, rule));
}
return violations;
}
private IEnumerable<Violation> FindViolationsInRequests(
IEnumerable<Request> allRequest,
RuleDefinition rule)
{
foreach (Request firstRequest in FindMatchingRequests(allRequest, rule))
{
Request collidingRequest = FindCollidingRequest(allRequest, firstRequest, rule.MinimumDistanceBetweenRequests);
if (collidingRequest != null)
{
yield return new Violation
{
ViolatedRule = rule,
FirstRequest = firstRequest,
SecondRequest = collidingRequest
};
}
}
}
private IEnumerable<Request> FindMatchingRequests(IEnumerable<Request> requests, RuleDefinition rule)
{
return requests.Where(r => r.TypeOfThisRequest == rule.ConcerningRequestType);
}
private Request FindCollidingRequest(IEnumerable<Request> requests, Request firstRequest, int minimumDistanceBetweenRequests)
{
return requests.FirstOrDefault(secondRequest => IsCollidingRequest(firstRequest, secondRequest, minimumDistanceBetweenRequests));
}
private bool IsCollidingRequest(Request firstRequest, Request secondRequest, int minimumDistanceBetweenRequests)
{
return secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < minimumDistanceBetweenRequests;
}
}
好的,它越来越清洁了。我几乎可以很容易地说出每种方法的目的。只需要做一些工作,你最终会得到这样的结果:
class ViolationFinder : IViolationFinder
{
public IEnumerable<Violation> Search(IEnumerable<RuleDefinition> ruleDefinitions, IEnumerable<Request> requests)
{
List<Request> requestList = requests.ToList();
return ruleDefinitions.SelectMany(rule => FindViolationsInRequests(requestList, rule));
}
private IEnumerable<Violation> FindViolationsInRequests(IEnumerable<Request> allRequest, RuleDefinition rule)
{
return FindMatchingRequests(allRequest, rule)
.Select(firstRequest => FindSingleViolation(allRequest, firstRequest, rule))
.Where(violation => violation != null);
}
private Violation FindSingleViolation(IEnumerable<Request> allRequest, Request request, RuleDefinition rule)
{
Request collidingRequest = FindCollidingRequest(allRequest, request, rule.MinimumDistanceBetweenRequests);
if (collidingRequest != null)
{
return new Violation
{
ViolatedRule = rule,
FirstRequest = request,
SecondRequest = collidingRequest
};
}
return null;
}
private IEnumerable<Request> FindMatchingRequests(IEnumerable<Request> requests, RuleDefinition rule)
{
return requests.Where(r => r.TypeOfThisRequest == rule.ConcerningRequestType);
}
private Request FindCollidingRequest(IEnumerable<Request> requests, Request firstRequest, int minimumDistanceBetweenRequests)
{
return requests.FirstOrDefault(secondRequest => IsCollidingRequest(firstRequest, secondRequest, minimumDistanceBetweenRequests));
}
private bool IsCollidingRequest(Request firstRequest, Request secondRequest, int minimumDistanceBetweenRequests)
{
return secondRequest.TimeIndex > firstRequest.TimeIndex &&
Math.Abs(secondRequest.TimeIndex - firstRequest.TimeIndex) < minimumDistanceBetweenRequests;
}
}
请注意,single responsibility principle也适用于方法。除了Search方法之外,所有内容都是私有实现的一部分,但正如您可能会看到每个处理部分都有一个带有名称的方法。每种方法都有它的单一责任,因此您可以更轻松地阅读实现。
这些是此实施的单位。
如果您为原始实现编写单元测试,并且只有在您启动重构之后,重构过程才会更安全。然后你总是知道你没有打破你的逻辑。如果你在第一个变体(当我把你的完整代码放入搜索方法时)再次编写它们,你的单元测试就没问题,所以对着界面。
另一个不太重要的部分是:
public IEnumerable<Violation> Search(IEnumerable<RuleDefinition> ruleDefinitions, IEnumerable<Request> requests)
{
List<Request> requestList = requests.ToList();
return ruleDefinitions.SelectMany(rule => FindViolationsInRequests(requestList, rule));
}
我从这些项目中创建一个列表,所以我绝对相信我不会多次枚举IEnumerable(这可能会导致某些实现出现问题,请考虑IQueryable)。
答案 1 :(得分:0)
如果您不反对使用查询表达式,那么您可以将您的实现编写为:
var violations = from rule in ruleDefinitions
join r1 in requests on rule.ConcerningRequestType equals r1.TypeOfThisRequest
join r2 in requests on rule.ConcerningRequestType equals r2.TypeOfThisRequest
where r1 != r2 &&
r2.TimeIndex > r1.TimeIndex &&
Math.Abs(r2.TimeIndex - r1.TimeIndex) < rule.MinimumDistanceBetweenRequests
select new Violation() { FirstRequest = r1, SecondRequest = r2, ViolatedRule = rule };