I can't make the Post Action to work. It gives me error over :IAuthorService in Services: This is my code below:
'AuthorService' does not implement intreface member 'IAuthorService.Post(AutthorViewModel)'.'IAuthorService.Post(AutthorViewModel)' cannot implement 'IAuthorService.Post(AutthorViewModel)' because it does not have the matching reurn type of 'AuthorViewModel'.
Author class:
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; }
}
}
Interface:
namespace Interfaces.RepositoryIntefaces
{
public interface IAuthorRepository
{
List<Author> GetAllFromRepo();
void PostFromRepo(Author author);
}
}
Repository:
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();
}
}
}
Services:
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>()
});
}
}
}
Author View Model:
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; }
}
}
Controllers:
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();
}
}
}
This is the error i get in error list in VS2017:
Severity Code Description Project File Line Suppression State Error CS0738 'AuthorService' does not implement interface member 'IAuthorService.Post(AuthorViewModel)'. 'AuthorService.Post(AuthorViewModel)' cannot implement 'IAuthorService.Post(AuthorViewModel)' because it does not have the matching return type of 'AuthorViewModel'. Services C:\Users\Jack\Desktop\WebApp Domasna 1.9\Services\AuthorService.cs 14 Active
the code for IAuthorService:
namespace Interfaces.ServiceInterfaces
{
public interface IAuthorService
{
List<AuthorViewModel> GetAll();
AuthorViewModel Post(AuthorViewModel author);
}
}
I think this is straight forward. Your interface has return type of AuthorViewModel
while your class has return type of void.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments