Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 773129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:54:58+00:00 2026-05-14T18:54:58+00:00

What is considered good practice for querying entites from a database using a simple

  • 0

What is considered good practice for querying entites from a database using a simple string as a query parameters. Who should implement the method that “converts” the string to an Entity, and who should call this method? The repository, the entity, or some other object, like the controller in an MVC application?

For a more concrete example I have an ASP.NET MVC 2 application, using Sharp Architecture’s IRepository and SharpModelBinder. The latter is configured so, that if a user submits a form, that has an ID in the place of an object, then it will query the appropriate repository for an object that has the same ID. I’d like to add to this example to not only be able to use an ID, but a custom string as well. Of course the method that gets the concrete ID from a generic query string is different for each Repository/Entity. How should I do this?

To be even more concrete. Suppose I have two entities:

class Doctor : Entity {
    public virtual Patient ActualPatient { get; set; };
    // some other properties, constructor, etc.
}

class Patient : Entity {
    // some properties, constructor, etc.
}

Now, if I have an editor for the Doctor entity (like a form), I could enter an ID in place of the Doctor.ActualPatient, and the model binder would take care of assigning the appropriate object. But what if the user enters a custom string (like the name of the Patient)?

Here are my current options:

  1. The Controller should take care of this custom string. So after the ModelBinder fails to bind the Patient to the Doctor (=returns null for the Patient), the controller takes the string and using some algorythm it tries to get a Patient that is represented by that string (or it fails too)
  2. The Controller takes care of this, but calls a method that is defined somewhere else not in the Controller class (look at next question)
  3. The ModelBinder should be configured to use this external method
  4. You should never do something like this, this is always considered bad practice
  5. Something else (suggestions are welcome)

And here are my suggestions on where this method should be implemented:

  1. The Repository should have a GetEntityFromAString(string) method, and the subclasses for this Repository should redefine this method.
  2. The Entity should have an Attribute that if present takes care of this conversion, and the controller/modelbinder/etc checks for this attribute and calls a method like GetEntityFromAStringUsingThisRepository(string,repository)
  3. I’ve already said that this is a bad practice
  4. Something else

Although the concrete example is for Sharp Architecture/ASP.NET MVC2/NHibernate feel free to answer it generally.

Thanks for the answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:54:58+00:00Added an answer on May 14, 2026 at 6:54 pm

    I’d manage this explicitly from the controller.

    If you bind by ID you can guarantee that you’ll either get the instance or null if not found. But binding by an arbitrary property is not so simple: what if there are multiple matches? Should the binder throw an exception? Should it only return the first result? As you can see, it’s easy to run into unexpected results, thus violating the principle of least astonishment.

    If you really know what you’re doing and you want this, I’d create a custom binder with the associated attribute, which would have the name of the property to use to find the entity, e.g.

    class Patient {
      int Id {get;set;}
      string PatientName {get;set;}
    }
    ...
    ActionResult SomeAction([MyCustomBinder("PatientName")] Patient patient) {...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 425k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just use the + operator to add a string before… May 15, 2026 at 12:18 pm
  • Editorial Team
    Editorial Team added an answer You can't test whether a specific object has been deallocated… May 15, 2026 at 12:18 pm
  • Editorial Team
    Editorial Team added an answer No, it doesn't. At least, not yet. But given the… May 15, 2026 at 12:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.