我对存储库模式的使用相当陌生,而且我正在努力如何在使用存储库的模型中实现关系。因此,例如我有以下两种库接口:IPersonRepository
和IAddressRepository
存储库模式和模型关系和依赖注入
public interface IPersonRepository
{
IList<Person> GetAll();
Person GetById(int id);
}
public interface IAddressRepository
{
IList<Address> GetAll();
Address GetById(int id);
Address GetByPerson(Person person);
}
和两个模型类:Person
和Address
public class Person
{
private IAddressRepository _addressRepository;
public string FirstName { get; set; }
public string LastName { get; set; }
private Address _address;
public Address Address
{
get { return _addressRepository.GetByPerson(this); }
set { _address = value; }
}
Person(string firstName, string lastName, IAddressRepository addressRepository)
{
this.FirstName = firstName;
this.LastName = lastName;
this._addressRepository = addressRepository;
}
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public List<Person> Persons { get; set; }
Address(string street, string city, string zip)
{
this.Street = street;
this.City = city;
this.Zip = zip;
}
}
所以现在我的问题是:是否有罚款注入的IAddressRepository
成Person
类并通过从实际的Person
对象中的获取器中延迟加载请求实际地址?另外,如果它有一个像GetPersons()
这样的方法,我会注入IPersonRepository
到Address
对象吗?我这样问是因为我重构了一些代码来使用存储库模式,并希望利用依赖注入来使它在稍后的时间点更好地测试。
此外:我没有使用任何ORM,因为我正在SharePoint环境中开发,我正在使用SharePoint列表作为域模型的实际数据存储。
在对象本身中引用存储库似乎非常奇怪。做这件事的原因是什么? –
嗯,我想我只是不知道如何做得更好。 :)在支持数据存储的延迟加载的同时,如何建立与“Address”的关系? –
你正试图做EF已经为你做的事情。 EF生成代理类以注入额外的代码以用于延迟加载。 检查** [this](http://www.alachisoft.com/resources/articles/entity-framework-poco-lazy-loading.html)**文章,看看它是否有帮助。 – Nilesh