From my code below i am trying to use the entity frame work as my ORM of choice.
And my choice of implementation is seen in the code below.
The problem now is based on the practice of writing loosely coupled code.
The class StaffRepository
below depends on CashFlowEntities
which is the
object context generated from my existing database as a means to query the database and Save stuffs in it.
Now below is how i tend to implement it in my code.
I initialized it in the StaffRepository
constructor. Note thatStaff
is also generated from database, its a model of a table in my existing database and has no relationship with IStaff
I recall it is not good coding practice to instantiate objects in our classes as it creates TIGHTLY coupled codes.
But I really dont see another option in this scenario.
Any tips on how else to implement this?
Example code would be very helpful.
Please do accommodate any unprofessional or bad code practice as i am a beginner.
I would however appreciate any suggestion, for a better way to implement this code.
namespace CashFlowManagement.Core.Data
{
public class StaffRepository
{
private CashFlowEntities _db;
public StaffRepository()
{
_db = new CashFlowEntities();
}
public void SaveEmployeeToDatabase(IStaff employee)
{
/*Note that Istaff is a data transfer object i created*/
Staff Employee = new Staff();
Employee.FirstNam = employee.FirstName;
Employee.LastName = employee.LastName;
Employee.Username = employee.Username;
Employee.Password = employee.Password;
_db.Staffs.Add(Employee);
_db.SaveChanges();
}
}
}