메신저 asp.net 코어를 사용하고 있습니다. 컨트롤러와 함께 모델을 사용하는 기본 방법은 다음과 같습니다.언제 데이터 클래스가 필요합니까?
public class BookController : Controller
{
private readonly ApplicationDbContext _context { get; set; }
public BookController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Create(Book model)
{
// adding new model
}
public IActionResult Edit(Book model)
{
// modifying the model
}
public IActionResult Delete(Book model)
{
// removing the model
}
}
내 질문 : 컨트롤러 내부에서 코드를 구현해야합니까? 언제 다른 수업에 그것을 시행해야합니까? 이 같은
뭔가 : 나는 같은 필요로하는 많은 컨트롤러가 : 인터페이스, 나는 그것이 경우에 유용 할 수있다 생각 들어
public IActionResult Create(Book model)
{
var bookData = new BookData(_context);
int result = bookData.Add(model);
// ...
}
: 컨트롤러 내부에서 그것을 호출 다음
public interface IBook
{
int Add(Book book);
int Update(Book book);
int Remove(Book book);
}
public class BookData : IBook
{
private readonly ApplicationDbContext _context { get; set; }
BookData(ApplicationDbContext context)
{
_context = context
}
public int Add(Book model)
{
// ...
return _context.SaveChanges();
}
// other implements...
}
, 액션/메소드 이름.
예 : MessageController
은 최소한 (Create/Add
, Edit/Update
, Delete/Remove
) 세 가지 작업/방법이 필요합니다.
public interface IMyService<T> where T : class
{
int Add(T model);
int Update(T model);
int Remove(T model);
}
public class MyService<T> : IMyService<T> where T : class
{
private readonly ApplicationDbContext _context { get; set; }
public MyService(ApplicationDbContext context)
{
_context = context;
}
public int Add(T model)
{
Type type = typeof(model);
if (type == typeof(Book))
{
// adding new book model
}
else if (type == typeof(Comment))
{
// adding new comment model
}
// ...
return -1;
}
// other implements...
}
내가 뭔가를 오해 수행 그것은 그래서
, 인터페이스가 개선 될 수있다 ...
NotificationController
클래스,
CommentController
클래스와 동일합니다?