消除数据访问层和UI层之间的耦合

我想为Windows应用程序设计一个n层体系结构。我想删除UI层和数据访问层之间的耦合。换句话说,我希望UI层仅依赖于业务逻辑层。

我有一个例子:

public Class Person
{
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
}

在数据访问层中,我在UI层中创建了新的Person,但是我不会调用数据访问层。最好的方法是什么?

谢谢

ranjing9 回答:消除数据访问层和UI层之间的耦合

您可以将MVC - Model View Controller设计模式与DAO - Data Acess Object设计模式结合使用。

MVC将使您的IU与业务逻辑和模型(数据)脱钩,并且在模型内部,您可以使用DAO模式提供的接口来使用数据访问。

,

要开发Windows应用程序并将UI与数据访问层分离,最好使用MVVM模式,请参见说明here

MVVM允许将UI与模型分离,但是为了使数据访问逻辑与业务逻辑分离,您需要拆分模型,我建议使用域驱动设计(DDD),您可以阅读{{3 }}

并使用here

这样的ORM

您应该遵循Entity Framework

您的示例如下: 在域业务逻辑层中,您将拥有

    //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
    }
}

应用层和数据访问层应依赖于域层,并且数据访问逻辑对应用层一无所知

本文链接:https://www.f2er.com/3125528.html

大家都在问