对于开发 Windows 应用程序并将 UI 与数据访问层解耦,最好使用 MVVM 模式,请参阅说明here
MVVM 它允许将 UI 与模型分离,但为了将数据访问逻辑与业务逻辑分离,您需要拆分模型,我建议使用域驱动设计 (DDD),您可以阅读它here
并使用像 Entity Framework 这样的 ORM
你应该关注Persistence Ignorance Principle
您的示例可能如下:
在域业务逻辑层中,您将拥有
//Domain Business layer logic
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
interface IPersonRepository
{
IEnumerable<Person> GetAll();
void Update(Person person);
}
在应用层
//Application logic
class PersonViewModel
{
private readonly IPersonRepository _personRepository;
public PersonViewModel(IPersonRepository personRepository)
{
_personRepository = personRepository;
}
public ObservableCollection<Person> Persons
{
get { return new ObservableCollection<Person>(_personRepository.GetAll()); }
}
}
在数据访问中,您将拥有 IPersonRepository 的实现
//Data Access layer Persistance logic
class PersonRepository : IPersonRepository
{
public IEnumerable<Person> GetAll()
{
// ORM Implementation here
return new List<Person>();
}
public void Update(Person person)
{
// Update logic here
}
}
应用层和数据访问层应该依赖于领域层,数据访问逻辑对应用层一无所知