一个控制器中的多个存储库

时间:2015-03-14 05:20:33

标签: c# asp.net-mvc-4

我有3张桌子 人,地址,personaddress 人有身份证,姓名 地址有id,地址 personaddress有personid,addressid 这是一个多对多的人 如果我按人名搜索,删除,更新,添加,所有这一切都正常。与地址相同的东西。但是,我想要做的是当我按名称搜索时,我希望看到它不仅会提取名称,还会提取该名称与之关联的所有地址,反之亦然。这就是为什么我有personaddress表作为交叉引用。所以这就是我被困的地方,我不知道如何在select上进行连接,因为它需要在所有3个表上进行连接。然后删除要求我删除外部参照表和主表。任何帮助都非常感谢。

所以我在Model文件夹中创建了person,address,personaddress类 人

public class Person 
{
    public int Id {}
    public string Name {}
}

地址

public class Address
{
    public int Id {}
    public string Address{}
}

personaddress

public class PersonAddress 
{
    public int PersonId {}
    public string AddressId {}
}

我还在Model文件夹中创建了iperson,iaddress,ipersonaddress类, 界面IPerson

{
    IEnumerable<Person> GetAll();
    Person Get(int id);
    Person Add(Person pers);
    void Remove(int id);
    bool Update(Person pers);        
}

interface IAddress

{
    IEnumerable<Address> GetAll();
    AddressGet(int id);
    AddressAdd(Address addr);
    void Remove(int id);
    bool Update(Address addr);        
}

interface IPersonAddress

{
    IEnumerable<PersonAddress> GetAll();
    PersonAddressGet(int id);
    PersonAddressAdd(PersonAddress persaddr);
    void Remove(int id);
    bool Update(PersonAddress persaddr);        
}

然后我在模型文件夹

中创建了personrepository,addressrepository,personaddressrepository
public class PersonRepository : IPersonRepository
{
    private List<Person> people= new List<Person>();
    private int _nextId = 1;

    public PersonRepository()
    {
        Add(new Person{ Name = "Peter Smith" });
        Add(new Person{ Name = "Joe Doe" });
    }

    public IEnumerable<Person> GetAll()
    {
        return people;
    }

    public PersonGet(int id)
    {
        return people.Find(p => p.Id == id);
    }

    public PersonAdd(Person pers)
    {
        if (pers== null)
        {
            throw new ArgumentNullException("pers");
        }
        pers.Id = _nextId++;
        people.Add(pers);
        return pers;
    }

    public void Remove(int id)
    {
        people.RemoveAll(m => m.Id == id);
    }

    public bool Update(Person pers)
    {
        if (pers==null)
        {
            throw new ArgumentNullException("pers");
        }
        int index = people.FindIndex(p => p.Id == pers.Id);
        if (index ==-1)
        {
            return false;
        }
        people.RemoveAt(index);
        people.Add(pers);
        return true;
    }
}

然后我为这3个人,地址,人物地址添加了控制器

public class PersonController : ApiController
{        
    static readonly IPersonRepository repository = new PersonRepository();
    public IEnumerable<Person> GetPersonByName(string name)
    {
        return repository.GetAll().Where(p => p.Name.ToLower().Contains(name.ToLower()));
    }

    public HttpResponseMessage AddPerson(Person pers)
    {
        pers= repository.Add(pers);
        var response = Request.CreateResponse<Person>(HttpStatusCode.Created, pers);

        string uri = Url.Link("DefaultApi", new { id = pers.Id });
        response.Headers.Location = new Uri(uri);
        return response;
    }

    public void UpdatePerson(int id, Person person)
    {
        person.Id = id;
        if (!repository.Update(person))
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public void DeletePerson(int id)
    {
        Person pers= repository.Get(id);
        if (pers== null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        repository.Remove(id);
    }
}

1 个答案:

答案 0 :(得分:0)

你有几种可能性。这是其中两个。

  1. public virtual ICollection<Address> Addresses{ get; set; }添加到您的模型类中,并在Constructor Addresses = new Collection<Addresses>();内进行初始化。这将启用延迟加载相关子集合。对Address类做同样的事情。

  2. 急切地使用Include()Get()方法中的GetAll()加载它们。

    示例:context.Persons.Include("Address").Where(x => x.PersonId == Id).ToList(); 或使用Linq

  3. 塑造您想要的结果

    删除:

        var person = context.Persons.GetById(personId);
    
        foreach(var personAddress in person.Addresses)
        {
            person.Addresses.Remove(personAddress);
            context.SaveChanges();
        }
    

    或者

        var person = context.Persons.GetById(personId);
        person.Addresses.Load();
        person.Addresses.ToList().ForEach(x => context.Addresses.DeleteObject(x));
    

    编辑:样本人员类

    public class Person()
    {
        public Person()
        {
            Addresses = new Collection<Address>();
        }
    
        public int Id {get; set;}
        public string Name {get; set;}
        public virtual Collection<Address> Addresses {get; set;}
    }