我正在尝试将我的控制器转换为使用DI。
基于article here,我现在得到了这段代码:
namespace HandheldServer.Controllers
{
public class DuckbillsController : ApiController
{
static IDuckbillRepository _platypiRepository;
public DuckbillsController(IDuckbillRepository platypiRepository)
{
if (platypiRepository == null)
{
throw new ArgumentNullException("platypiRepository is null");
}
_platypiRepository = platypiRepository;
}
public int GetCountOfDuckbillRecords()
{
return _platypiRepository.Get();
}
public IEnumerable<Duckbill> GetBatchOfDuckbillsByStartingID(int ID, int CountToFetch)
{
return _platypiRepository.Get(ID, CountToFetch);
}
public void PostDuckbill(int accountid, string name)
{
_platypiRepository.PostDuckbill(accountid, name);
}
public HttpResponseMessage Post(Duckbill Duckbill)
{
Duckbill = _platypiRepository.Add(Duckbill);
var response = Request.CreateResponse<Duckbill>(HttpStatusCode.Created, Duckbill);
string uri = Url.Route(null, new { id = Duckbill.Id });
response.Headers.Location = new Uri(Request.RequestUri, uri);
return response;
}
}
}
...但它没有编译;我得到了,“不一致的可访问性:参数类型'HandheldServer.Models.IDuckbillRepository'不如方法'HandheldServer.Controllers.DuckbillsController.DuckbillsController(HandheldServer.Models.IDuckbillRepository)'”
错误消息中提到的接口参数类型是:
using System.Collections.Generic;
namespace HandheldServer.Models
{
interface IDuckbillRepository
{
int Get();
IEnumerable<Duckbill> Get(int ID, int CountToFetch);
Duckbill Add(Duckbill item);
void Post(Duckbill dept);
void PostDuckbill(int accountid, string name);
void Put(Duckbill dept);
void Delete(int Id);
}
}
我需要做些什么来解决这个错误信息?
答案 0 :(得分:4)
您需要明确将interface
标记为public
:
public interface IDuckbillRepository
{
// ....
}
另外,请勿在控制器中标记static
。