Hi everyone!
I want to use the n-layer architecture for a very simple CRUD console application. I have a domain layer, presentation layer (a console application), a business layer and a data acces layer.
If I'm right, the DAL is used to interact with a database, and the BL is a layer between the PL and the DAL.
So for exampe, if I have a database with a 'Persons' table and each record consists out of a firstname and lastname.
Imagine in my console application I create an option 'add person to database' or something like that.
the console application asks the user to fill in a firstname and a lastname.
it's the BL that takes the user input from the PL and gives it to the DAL, right?
To become familiar with the whole n-layer architecture, I created a very, VERY, simple console application.
The console application can show a user all records in the 'Persons' table and can let a user add a record to the 'Persons' table.
First things first, I created a 'Person' class in the domain layer:
In the DAL I created a PersonRepository class with a few methods to interact with the database that stores the 'Persons' table. Cause at the moment I don't know how to interact with a database, I created a list containing Person objects to represent the 'Persons' table. I have a static method Seed() to fill the list with dummy data:
In the BL I created a PersonManager class that take user input from the presentation layer and let it interact with the DAL:
An lastly, I created a console application to let a user decide which he/she want's to do:
I'm think i'm using the repository-service pattern. So can I say repository-service pattern can be used in the n-tier achitecture or...?
Thanks!
I want to use the n-layer architecture for a very simple CRUD console application. I have a domain layer, presentation layer (a console application), a business layer and a data acces layer.
If I'm right, the DAL is used to interact with a database, and the BL is a layer between the PL and the DAL.
So for exampe, if I have a database with a 'Persons' table and each record consists out of a firstname and lastname.
Imagine in my console application I create an option 'add person to database' or something like that.
the console application asks the user to fill in a firstname and a lastname.
it's the BL that takes the user input from the PL and gives it to the DAL, right?
To become familiar with the whole n-layer architecture, I created a very, VERY, simple console application.
The console application can show a user all records in the 'Persons' table and can let a user add a record to the 'Persons' table.
First things first, I created a 'Person' class in the domain layer:
C#:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public override string ToString()
{
return $"{FirstName} {LastName}";
}
}
In the DAL I created a PersonRepository class with a few methods to interact with the database that stores the 'Persons' table. Cause at the moment I don't know how to interact with a database, I created a list containing Person objects to represent the 'Persons' table. I have a static method Seed() to fill the list with dummy data:
C#:
public class PersonRepository
{
private static readonly List<Person> _persons = new List<Person>();
static PersonRepository()
{
Seed();
}
public Person ReadPerson(string firstName, string lastName)
{
return _persons.Find(p =>
p.FirstName.ToLower() == firstName.ToLower() && p.LastName.ToLower() == lastName.ToLower());
}
public IEnumerable<Person> ReadPersons()
{
return _persons;
}
public void CreatePerson(Person person)
{
_persons.Add(person);
}
public static void Seed()
{
Person person1 = new Person("Anakin","Skywalker");
_persons.Add(person1);
Person person2 = new Person("Luke","Skywalker");
_persons.Add(person2);
}
}
In the BL I created a PersonManager class that take user input from the presentation layer and let it interact with the DAL:
C#:
public class PersonManager
{
private readonly PersonRepository _personRepository = new PersonRepository();
public Person GetPerson(string firstName, string lastName)
{
return _personRepository.ReadPerson(firstName, lastName);
}
public IEnumerable<Person> GetPersons()
{
return _personRepository.ReadPersons();
}
public void AddPerson(Person person)
{
Person p = _personRepository.ReadPerson(person.FirstName, person.LastName);
if (p == null)
{
_personRepository.CreatePerson(person);
}
else
{
throw new ArgumentException("A person with that first- and lastname already exists!");
}
}
}
An lastly, I created a console application to let a user decide which he/she want's to do:
C#:
class Program
{
private static readonly PersonManager _personManager = new PersonManager();
static void Main(string[] args)
{
bool quit = false;
do
{
Console.WriteLine("0) Quit");
Console.WriteLine("1) View all persons");
Console.WriteLine("2) Create person");
Console.Write("Choice:");
int choice;
bool parseSucces = Int32.TryParse(Console.ReadLine(), out choice);
if (parseSucces)
{
switch (choice)
{
case 0:
quit = true;
break;
case 1:
Console.WriteLine($"\nAll persons:");
foreach (var p in _personManager.GetPersons())
{
Console.WriteLine($"\t -{p.FirstName} {p.LastName}");
}
break;
case 2:
Console.Write($"\nFirstname:");
string firstName = Console.ReadLine();
Console.Write("Lastname:");
string lastName = Console.ReadLine();
try
{
_personManager.AddPerson(new Person(firstName,lastName));
}
catch (Exception e)
{
Console.WriteLine($"\n{e.Message}");
}
break;
}
}
else
{
Console.WriteLine("Please, enter a number!");
}
Console.WriteLine();
} while (!quit);
}
}
I'm think i'm using the repository-service pattern. So can I say repository-service pattern can be used in the n-tier achitecture or...?
Thanks!