2015-02-10 60 views
0

我得到了這個問題與控制器: 嘗試創建類型'* .WebMvc.Controllers.HomeController'類型的控制器時發生錯誤。確保控制器有一個無參數的公共構造函數。無參數的公共構造函數 - Unity

public class HomeController : BaseController 
{ 
    #region Fields 
    private readonly INewsService _newsService; 
    #endregion 

    #region Constructors 

    public HomeController(INewsService newsService) 
    { 
     this._newsService = newsService; 
    } 

    #endregion 

    #region unilities 
    public ActionResult Index() 
    { 

     return View(); 
    } 
    #endregion 

} 


public interface INewsService : IEntityService<proNew> 
{ 
    proNew GetById(int Id); 
} 


    public interface IEntityService<T> : IService where T : class 
{ 
    void Create(T entity); 
    void Delete(T entity); 
    IEnumerable<T> GetAll(); 
    void Update(T entity); 
} 

public abstract class EntityService<T> : IEntityService<T> where T : class 
{ 
    IUnitOfWork _unitOfWork; 
    IGenericRepository<T> _repository; 
    public EntityService(IUnitOfWork unitOfWork, IGenericRepository<T> repository) 
    { 
     _unitOfWork = unitOfWork; 
     _repository = repository; 
    } 
    public virtual void Create(T entity) 
    { 
     if (entity == null) 
     { 
      throw new ArgumentNullException("entity"); 
     } 
     _repository.Add(entity); 
     _unitOfWork.Commit(); 
    } 
    public virtual void Update(T entity) 
    { 
     if (entity == null) throw new ArgumentNullException("entity"); 
     _repository.Edit(entity); 
     _unitOfWork.Commit(); 
    } 
    public virtual void Delete(T entity) 
    { 
     if (entity == null) throw new ArgumentNullException("entity"); 
     _repository.Delete(entity); 
     _unitOfWork.Commit(); 
    } 
    public virtual IEnumerable<T> GetAll() 
    { 
     return _repository.GetAll(); 
    } 
} 


public class NewsService : EntityService<proNew>, INewsService 
{ 
    IUnitOfWork _unitOfWork; 
    INewsRepository _countryRepository; 

    public NewsService(IUnitOfWork unitOfWork, INewsRepository newsRepository) : base(unitOfWork, newsRepository) 
    { 
     _unitOfWork = unitOfWork; 
     _countryRepository = newsRepository; 
    } 

    public proNew GetById(int Id) 
    { 
     return _countryRepository.GetById(Id); 
    } 

} 
+0

你在這裏使用依賴注入嗎? – 2015-02-10 12:41:10

回答

0

如果您使用Unity進行依賴注入並希望它爲您解決依賴關係,請查看this article。它有一個完整的配方爲您的方案。

總之,您必須將Unity設置爲您的依賴關係解析器,然後配置公開您的控制器所需接口的類型。

相關問題