我无法执行发布后操作。它给我在服务中的:IAuthorService错误: 这是我的代码如下:
'AuthorService'没有实现接口成员'IAuthorService.Post(AutthorViewModel)'。'IAuthorService.Post(AutthorViewModel)'无法实现'IAuthorService.Post(AutthorViewModel)'因为它没有匹配的reurn类型的'AuthorViewModel' '。
作者类:
namespace Entities
{
public class Author
{
public Author()
{
Books = new List<Book>();
}
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Book> Books { get; set; }
}
}
接口:
namespace Interfaces.RepositoryIntefaces
{
public interface IAuthorRepository
{
List<Author> GetAllFromRepo();
void PostFromRepo(Author author);
}
}
存储库:
namespace Repositories
{
public class AuthorRepository : IAuthorRepository
{
AppContext myDB = new AppContext();
public List<Author> GetAllFromRepo()
{
return myDB.Authors.Include(a=> a.Books).ToList();
}
public void PostFromRepo(Author author)
{
myDB.Authors.Add(author);
myDB.SaveChanges();
}
}
}
服务:
namespace Services
{
public class AuthorService : IAuthorService // here is the error
{
private IAuthorRepository _AuthorRepository;
public AuthorService(IAuthorRepository authorRepository)
{
_AuthorRepository = authorRepository;
}
public List<AuthorViewModel> GetAll()
{
List<Author> authors = _AuthorRepository.GetAllFromRepo();
return authors.Select(x => new AuthorViewModel()
{
ID = x.ID,
FirstName = x.FirstName,
LastName = x.LastName,
Books = x.Books.Select(g => new BookViewModel()
{
ID = g.ID,
Name = g.Name
}).ToList()
}).ToList();
}
public void Post(AuthorViewModel author)
{
_AuthorRepository.PostFromRepo(new Author()
{
FirstName = author.FirstName,
LastName = author.LastName,
Books = new List<Book>()
});
}
}
}
作者视图模型:
namespace ViewModels
{
public class AuthorViewModel
{
public AuthorViewModel()
{
Books = new List<BookViewModel>();
}
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<BookViewModel> Books { get; set; }
}
}
控制器:
namespace WebApp.Controllers
{
public class HomeController : Controller
{
private readonly IAuthorService _AuthorService;
public HomeController(IAuthorService authorService)
{
_AuthorService = authorService;
}
public ActionResult Index()
{
List<AuthorViewModel> Authors = _AuthorService.GetAll();
return View(Authors.ToList());
}
[HttpPost]
public ActionResult Create()
{
_AuthorService.Post(new AuthorViewModel() { FirstName = "Olivia", LastName = "Fox", Books = new List<BookViewModel>() });
return View();
}
}
}
这是我在VS2017的错误列表中得到的错误:
严重性代码描述项目文件行抑制状态 错误CS0738'AuthorService'没有实现接口成员'IAuthorService.Post(AuthorViewModel)'。 'AuthorService.Post(AuthorViewModel)'无法实现'IAuthorService.Post(AuthorViewModel)',因为它没有匹配的返回类型'AuthorViewModel'。服务C:\ Users \ Jack \ Desktop \ WebApp Domasna 1.9 \ Services \ AuthorService.cs 14有效
IAuthorService的代码:
namespace Interfaces.ServiceInterfaces
{
public interface IAuthorService
{
List<AuthorViewModel> GetAll();
AuthorViewModel Post(AuthorViewModel author);
}
}
答案 0 :(得分:2)
我认为这很简单。接口的返回类型为AuthorViewModel
,而类的返回类型为void。