我在DictionarypersonRepository中实现了以下界面。但是错误在控制器中具有不一致的可访问性 在重新构造项目之后,PersonController构造函数显示为不可访问。
public interface IPersonRepository
{
IEnumerable<Person> Get();
bool TryGet(int id, out Person person);
Person add(Person person);
}
public class DictionaryPersonRepository : IPersonRepository
{
int nextId = 0;
Dictionary<int, Person> persons = new Dictionary<int, Person>();
public IEnumerable<Person> Get()
{
return persons.Values.OrderBy(person => person.ID);
// throw new NotImplementedException();
}
public bool TryGet(int id, out Person person)
{
return persons.TryGetValue(id, out person);
// throw new NotImplementedException();
}
public Person add(Person person)
{
person.ID = nextId++;
persons[person.ID] = person;
return person;
// throw new NotImplementedException();
}
}
我把一切都公之于众。但仍然无法解决无法进入的问题。
这是人类。
public class Person
{
public Person() { }
public Person(string first, string last)
{
First = first;
Last = last;
}
public int ID { get; set; }
[Required]
public string First { get; set; }
[Required]
public string Last { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email {get; set;}
}
PersonController具有该不可用性错误。这里有任何解决方法。 人员控制器如下:
public class PersonController : ApiController
{
IPersonRepository repository;
public PersonController(IPersonRepository repository)
{
this.repository = repository;
}
#region GET
public IQueryable<Person> GetPerson()
{
return repository.Get().AsQueryable();
}
public Person GetPerson (int id)
{
Person person;
if(!repository.TryGet(id, out person)){
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return person;
}
#endregion
}