1
블로그 작성에 대한 지침을 따르고 있으며 엉망으로 보이는 코드를 어떻게 리펙토링 할 수 있는지 궁금합니다. 특히 태그에서 파싱하는 동안 many-to-many 관계. 이것을 컨트롤러의 더 작은 기능으로 축소하는 더 좋은 방법이 있습니까?다 대다 관계가 포함 된 컨트롤러 리 팩터링
public ActionResult Create(int? id, string title, string body, DateTime datetime, string tags)
{
Post post = GetPost(id);
post.Title = title;
post.Body = body;
post.Tags.Clear();
// I'D Like to refactor this point on to look something like this: ParseTags(tags);
rather than this
tags = tags ?? string.Empty;
string[] tagNames = tags.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
foreach(string tagName in tagNames)
{
post.Tags.Add(GetTag(tagNames));
}
}
private Tag GetTag(string tagName)
{
return _context.Tags.Where(x => x.Name == tagName).FirstOrDefault() ??
new Tag() { Name = tagName };
}
그건 정말 아름답습니다. 감사! – AllocSystems