Everyone else is correct that this creates two separate classes. To not create two separate classes and separate your data from logic through partial classes, you could have the following two files in your project:
- Employee.cs
- Employee.bl.cs
Then your Employee.cs code would look like this:
namespace YourNamespace
{
partial class Employee
{
public string EmpID { get; set; }
public string EmpName { get; set; }
}
}
And Employee.bl.cs would look like this:
namespace YourNamespace
{
partial class Employee
{
public static List<Employee> GetListOfEmployees()
{
//DATA ACCESS
var emps = GetEmployeesFromDb(); // fetch from db
return emps;
}
}
}
Though I would think that having a Repository
class for retrieving data would be more appropriate than having GetListOfEmployees
inside of Employee
.
Update:
When I say repository, I'm referring to the Repository design pattern. A repository is an interface for retrieving and storing objects (from a relational database, for example). If you are using an ORM like LINQ to SQL or the ADO.NET Entity Framework, they typically auto-generated classes that fill this role. If, however, if you writing your own database access code, you could create your own repository class like this:
public class Repository
{
public Repository(string connectionString)
{
// ...
}
public IEnumerable<Employee> GetEmployees()
{
return GetEmployeesFromDb();
}
public Employee GetEmployeeById(Guid id)
{
// ...
}
public void StoreEmployee(Employee employee)
{
// ...
}
// etc.
}
The benefit of this is that you don't have to place database code throughout your Employee
class or any of the other persistent classes. All of the database access is done through one interface. You could also create an interface
and have multiple implementations of the repository; that way, you could have a way to store Employee
instances in a file, for example, without having to change the Employee
class.