3
\$\begingroup\$

Which is the best practice in getting data from repository? passing just by object or by Id?

public IQueryable<Attendance> GetByPersonId(int id)
    {
        return DbSet.Where(ps => ps.PersonId == id);
    }

or

public IQueryable<Attendance> GetByPerson(Person person)
    {
        return DbSet.Where(ps => ps.Person == person);
    }

Are there any performance implication? Or is it a case to case basis? If so what are the cases that you might want to pass by object and not by Id?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I personally would probably just pass the Id as then the caller does not have to assume knowledge of the method. Otherwise they have to pass an entire person object because who knows what the method might want to use... \$\endgroup\$
    – dreza
    Commented Sep 29, 2013 at 7:59

3 Answers 3

1
\$\begingroup\$

If the return type is IQueryable<Attendance> then use a Person object because it implies that the method could use any of the passed Person object's properties to query other persons or other related objects.

If you want to find one person with Id, change the method's signature to tomething like this:

public Attendance GetByPersonById(int id)
\$\endgroup\$
2
  • \$\begingroup\$ »it implies that the method could use any of the passed Person object's properties to query other persons or other related objects.« Yes - but is that likely to happen? I do not know the framework enough, but I would guess the framework just uses the id. That is what ids are for: to clearly identify one object. \$\endgroup\$ Commented Sep 29, 2013 at 17:12
  • \$\begingroup\$ It is not depending on the framework rather then the developer. \$\endgroup\$
    – Peter Kiss
    Commented Sep 30, 2013 at 10:00
0
\$\begingroup\$

I think you may pass complete entity because you already have it, this allow you do another condition type if you need.

\$\endgroup\$
0
\$\begingroup\$

Think of the simple case:

Is it likely, that the caller of your method has the ID of a person or the complete Person? Most of the times, I suppose is the former the case. Then you have to build a Person-Dummy only with the ID to use your method, which makes no sense. On the other hand, you could if you really really can't decide, which one is better simply use method overloading and offering both. But that would perhaps clutter your API. It's up to you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.