I am trying to use three-tier architecture and repository pattern in ASP.NET MVC project. But in some cases, the three-tier architecture and repository pattern look almost same. So I tried to study the followings to make it more clear:
After that, I've come into the following code for implementation and would expect some advices to improve the implementation in a more efficient way:
Models - Department class:
public class Department
{
public int DepartmentID { get; set; }
public string Code { get; set; }
public string DepartmentName { get; set; }
}
Interfaces - IRepository Interface:
public interface IRepository
{
public int Add(Student aStudent); //For Adding Students
public int Add(Department aDepartment); //For Adding Departments
}
DAL - DepartmentGateway Class:
public class DepartmentGateway : IRepository
{
/****Repository Pattern - Starts****/
Gateway aGateway = new Gateway();
public int Add(Department aDepartment)
{
aGateway.Query = "INSERT INTO Departments (Code, Name) VALUES (@Code, @Name)";
aGateway.Command = new SqlCommand(aGateway.Query, aGateway.Connection);
aGateway.Connection.Open();
aGateway.Command.Parameters.Clear();
aGateway.Command.Parameters.Add("Code", SqlDbType.NVarChar);
aGateway.Command.Parameters["Code"].Value = aDepartment.Code;
aGateway.Command.Parameters.Add("Name", SqlDbType.NVarChar);
aGateway.Command.Parameters["Name"].Value = aDepartment.DepartmentName;
int rowAffected = aGateway.Command.ExecuteNonQuery();
aGateway.Connection.Close();
return rowAffected;
}
/****Repository Pattern - Ends****/
}
BLL - DepartmentManager Class:
public class DepartmentManager
{
DepartmentGateway aDepartmentGateway = new DepartmentGateway();
public int Add(Department aDepartment)
{
int affect = aDepartmentGateway.Add(aDepartment);
if (affect > 0)
{
return 1;
}
else
{
return 0;
}
}
}
I am leaving the UI section. I am trying to assure if this is the right way to proceed and let me know. Thanks.
Note: I apology to ask this question. I am actually mixing these two things and would expect some advices from the experts with code samples. Please don't post any links. I've already seen some.