I have the below domain model:
public class Company
{
public int Id { get; private set; }
public string Name { get; private set; }
public BlaBla BlaBla { get; private set; }
...
...
public void ChangeName(string newName)
{
//business logic here
Name = newName;
}
}
We have implemented the repository pattern using Entity Framework and it all works very well. For a very specific case (use a legacy database) we need to skip EF and use plain sql queries and do the mapping ourselves.
For the mapping the code (pseudo code) is as follows:
using connection
using command
company = new Company();
while (reader.read())
company.ChangeName(reader["company_name"]);
end while
end using
end using
As you can see all the properties of Company are "private set" since we are practicing Domain Driven Design, so we can't write company.Id = reader["Id"] and we must use the domain methods to "populate" the properties of the Company object.
Is there an elegant way of doing this, like EF? How can I set the Id property without creating a method SetId(int id)?