patterncsharpMinor
Recomendations on where to set an object properties (Repository Pattern)
Viewed 0 times
recomendationspropertieswherepatternrepositoryobjectset
Problem
Simple question:
What would you rather do?
Or...
Assuming you can't use an ORM (NHibernate, L2S, EF) to handle this for you and that you will need to create Foo in other methods of the repository.
Any explanation will be welcome!
What would you rather do?
public class FooRepository
{
public Foo GetById(int id)
{
Foo foo = null;
using (IDataReader data = /* Get Foo's data */)
{
// Let the Foo object set it's own fields using the data object
foo = new Foo(data);
}
return foo;
}
}Or...
public class FooRepository
{
public Foo GetById(int id)
{
Foo foo = null;
using (IDataReader data = /* Get Foo's data */)
{
// Let the Repository class set the Foo object fields
foo = new Foo();
foo.Id = Convert.ToInt32(data["FooID"]);
foo.Name = data["Name"].ToString();
}
return foo;
}
}Assuming you can't use an ORM (NHibernate, L2S, EF) to handle this for you and that you will need to create Foo in other methods of the repository.
Any explanation will be welcome!
Solution
Part of the goal for using the Repository pattern is to separate the storage from the domain. By passing the IDataReader in to a Foo object, I feel like the domain gains too much knowledge of the underlying storage mechanism.
So, between the options listed, I prefer setting the object's properties in the Repository. That being said, if the object doesn't make sense without a certain property, it should still demand it during its creation. However, even in this case, the Repository would still be responsible for setting all the properties requested.
So, between the options listed, I prefer setting the object's properties in the Repository. That being said, if the object doesn't make sense without a certain property, it should still demand it during its creation. However, even in this case, the Repository would still be responsible for setting all the properties requested.
Context
StackExchange Code Review Q#5061, answer score: 3
Revisions (0)
No revisions yet.