0
votes

I want to design a n-tier architecture for a Windows application. I want to remove coupling between UI layer and data access layer. In the other words, I want the UI layer to depend to the business logic layer only.

Example I have:

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

in the data access layer, I create new Person in UI layer but I won't call the data access layer. What is the best approach?

Thanks

2

2 Answers

0
votes

For developing Windows applications and decouple the UI from data access layer it is better to use MVVM pattern see explanation here

MVVM it allows to decouple the UI from Model, but in order to decouple the Data Access logic from Business logic you need to split your model and I recommend to use Domain Driven Design (DDD)you can read about it here

and use the ORM like Entity Framework

And you should follow the Persistence Ignorance Principle

For your example could be following: in Domain Business logic layer you will have

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

in Application layer

    //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()); }
    }
}

and in the Data Access you will have the implementation of 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
    }
}

Application Layer and Data Access layer should depend on Domain layer and Data Access logic doesn't know nothing about the Application layer

0
votes

You can apply the MVC - Model View Controller design pattern combined with the DAO - Data Acess Object design pattern.

The MVC will decouple your UI (user-interface) from the business logic and model (data) and inside the model, you can make use of your data access, using an interface provided by the DAO pattern.