2015-02-10 44 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设置为您的依赖关系解析器,然后配置公开您的控制器所需接口的类型。

相关问题