The Loudoun prosecutor’s office temporarily hired McCaffrey to work on the murder trial, which resulted in Castillo’s conviction for killing his wife and staging it to look like a suicide. In July, McCaffrey sued Chapman and Loudoun County alleging retaliation and a violation of his First Amendment right to free speech. Chapman responded in an affidavit that as an elected officer, “I have a right to not swear in any deputies or other sworn personnel at the beginning of my term,” and “I perceived Mr. McCaffrey’s conduct as a threat to my ability to lead and manage” the Loudoun sheriff’s office. He noted that McCaffrey had voiced complaints about Chapman to other deputies and to the Police Benevolent Association, which did not endorse Chapman in 2015.

U.S. District Judge Anthony J. Trenga sided completely with Chapman and dismissed the case Thursday. McCaffrey’s attorney, Robert Cynkar, has already appealed to the U.S. Court of Appeals for the 4th Circuit.

Trenga cited previous 4th Circuit rulings to decide that McCaffrey’s firing was justified. “The law in this circuit is clear,” Trenga quoted from a 1997 ruling, “that sheriffs in Virginia have the right to lawfully terminate their deputies for political affiliation reasons.” The logic behind this is that a sheriff is elected by the public, that the sheriff “owes a duty to the electorate” to ensure that his policies are implemented and that “deputy sheriffs play a special role in the sheriff’s policies and goals.” Therefore, “the office of a deputy sheriff in Virginia is partisan,” Trenga wrote, and “the First Amendment’s protection against retaliatory termination does not require ‘that a sheriff must attempt to implement his policies and perform his duties through deputies who have expressed clear opposition to him.’ ”

But the test of whether a deputy is a partisan is only one step in the analysis, Trenga said. Next, the court has to determine whether the person who was fired has responsibilities of a “policymaker . . . whose function is such that party affiliation is an equally appropriate requirement.” Trenga concluded that “McCaffrey’s role as deputy sheriff was that of a ‘policymaker.’ ”

Trenga cited McCaffrey’s 20 years of experience with another department, and then 10 years in Loudoun, to observe he “was by no means a junior deputy.” Also, the judge noted that “McCaffrey had the discretion to contact directly the Commonwealth’s Attorney’s Office and the Medical Examiner’s Office and to request the resources of those offices in support of the LCSO’s law enforcement mission. . . . A deputy with McCaffrey’s alleged experience, seniority and responsibilities within a sheriff’s office is a policymaker.” As a policymaker, McCaffrey was an “exception to the general rule against partisan terminations of public employees,” Trenga ruled.

The judge granted Chapman’s motion to dismiss the case, less than three months after it was filed. “It says something,” Chapman said Monday, “when the judge rules on a case that quickly. It further validates the constitutional authority of sheriffs throughout the commonwealth of Virginia.” The sheriff declined to explore the issue of who qualified as a policymaker in his department, but he said that “it impacts morale when somebody actively opposes you within the department.” He noted that sheriffs appoint their entire force of deputies every four years and may decline to reappoint anyone, and that only five deputies were not reappointed in 2016, three of them eligible for retirement. McCaffrey was not.

Cynkar, McCaffrey’s attorney, said he was surprised by the ruling. “We felt that the district court got the law fundamentally wrong,” Cynkar said. He noted that Loudoun has the largest sheriff’s office in Virginia, with more than 600 sworn deputies, and that “the sheriff’s orders create a very defined structure” of the sheriff, two chief deputies and five majors at the top of the organization. “They are the only people that make policy,” Cynkar said, “which is done by written guidance down to the commanders. We feel that the judge missed that this isn’t Mayberry — they have structured it quite clearly as to where policy is made.” McCaffrey held the rank of detective. He declined to comment.