Ho seguito il codice C #. Mi ha aiutato a evitare alcune ripetizioni di codice in un buon modo.
Il metodo ExecuteQueryGenericApproach<T>
riceve un delegato generico Func
come argomento. Il metodo delegato ha un parametro che riceve IDataRecord
come argomento. Cioè, il metodo ExecuteQueryGenericApproach<T>
fornisce l'IDataRecord richiesto alle funzioni che gli vengono passate.
DOMANDE
- Qual è il nome di questo modello? (Basato su% modelli di progettazione% co_de)
- C'è qualche altro scenario in cui viene utilizzato questo modello?
Nota : conoscere il nome di questo modello mi aiuterà a fare ricerche migliori su questo argomento e a trovare opportunità per utilizzarlo.
CommonDAL
public class CommonDAL
{
public static IEnumerable<T> ExecuteQueryGenericApproach<T>(string commandText, List<SqlParameter> commandParameters, Func<IDataRecord, T> methodToExecute)
{
string connectionString = @"Server=XXXX;Database=AS400_Source;User Id=dxxx;Password=xxxx5";
//Action, Func and Predicate are pre-defined Generic delegates.
//So as delegate they can point to functions with specified signature.
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = commandText;
command.CommandTimeout = 0;
if (commandParameters != null)
{
command.Parameters.AddRange(commandParameters.ToArray());
}
connection.Open();
using (var rdr = command.ExecuteReader())
{
while (rdr.Read())
{
yield return methodToExecute(rdr);
}
rdr.Close();
}
}
}
}
}
EmployeeDAL
public class EmployeeRepositoryDAL
{
public static List<Employee> GetEmployees()
{
string commandText = @"SELECT E.EmployeeID,E.EmployeeName,R.RoleID,R.RoleName FROM dbo.EmployeeRole ER
INNER JOIN dbo.Employee E ON E.EmployeeID= ER.EmployeeID
INNER JOIN dbo.[Role] R ON R.RoleID= Er.RoleID ";
//IEnumerable<Employee> employees = MyCommonDAL.ExecuteQueryGenericApproach<Employee>(commandText, commandParameters, Employee.EmployeeFactory);
//Group By is needed for listing all the roles for an employee.
IEnumerable<Employee> employees = CommonDAL.ExecuteQueryGenericApproach<Employee>(commandText, null, Employee.EmployeeCreator)
.GroupBy(x => new { x.EmployeeID, x.EmployeeName },
(key, group) =>
new Employee
{
EmployeeID = key.EmployeeID,
EmployeeName = key.EmployeeName,
Roles = group.SelectMany(v => v.Roles).ToList()
}
).ToList();
return employees.ToList();
}
}
Ente
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public List<Role> Roles { get; set; }
//IDataRecord Provides access to the column values within each row for a DataReader
//IDataRecord is implemented by .NET Framework data providers that access relational databases.
//Static Method
public static Employee EmployeeCreator(IDataRecord record)
{
var employee = new Employee
{
EmployeeID = (int)record[0],
EmployeeName = (string)record[1],
Roles = new List<Role>()
};
employee.Roles.Add(new Role { RoleID = (int)record[2], RoleName = (string)record[3] });
return employee;
}
}