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

  • Home
  • SEARCH
  • 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 8920809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:22:18+00:00 2026-06-15T06:22:18+00:00

I´m implementing Identity map pattern and DataMapper pattern in my project. But I have

  • 0

I´m implementing Identity map pattern and DataMapper pattern in my project. But I have some problems.

When I´m getting data from database by Gateway I´m creating new instance of object and return List …

What is the best way to implement getting data from database with regard to Identy Map ?
I want to get patients from database and show these data to user..

What about ID of objects in Identity map ? My solution (in my mind) :
1) Create new object with some default id
2) Save object to databse
3) Get id of last object in database
4) Set new id of object
5) Save to cache with new id ….

It is good solution ?

Thanks for your help!

My identy map object(singleton):

public class PacientMap
{

    //Cache obsahující jednotlivé instance entiti

    private static PacientMap _instance;
    private Dictionary<int, Pacient> _cache; // Slouží k ukládání objektů pacient

    //Privátní konstruktor, (zaručí mi, že nedojde k vytvoření další instance) => metoda PacientGetInstance ho zavolá jen jednou
    private PacientMap()
    {
            _cache = new Dictionary<int, Pacient>();
    }

    //Metoda pro vytvoreni objektu jedinacek
    public static PacientMap PacientGetInstance()
    {   
        //Je-li promenna instance null, tak se vytvori objekt
        if (_instance == null)
        {
            _instance = new PacientMap();
        }

       //Vratime jedinacka
        return _instance;
    }



    /***********************************
     Vrátí jedinečnou instanci pro zadaný klíč. Pokud instance neexistuje, bude
     vytvořena a uložena do cache.
    ***************************************/
    public Pacient GetPacient(int klic)
    {
        // Pokud je v cache objekt s daným klicem
        if (_cache.ContainsKey(klic))
        {
            //Vrátím objekt
            return _cache[klic];
        }
        // Pokud jsem ho v cache nenasel
        // Zkusím ho najít v databázi
        if (SerachInDb(klic))
        {
            // Pokud byl nalezen v DB, byl automaticky uložen do cahe a jen si ho pod jeho id v cahe nejdu
            return _cache[klic];
        }

        // Pokud nebyl nikde nalezen, vyhodíme chybu
        throw new Exception();
    }

    /// <summary>
    /// Najde Pacienta v databází a pokud ho najde ulozí ho do cache a vrací TRUE
    /// Pokud ho nenejde vrací FALSE
    /// </summary>
    /// <param name="klic"></param>
    /// <returns></returns>
    private bool SerachInDb(int klic)
    {

        Pacient pac = PacientDataGateway.GetInstance().SelectByPacientId(klic);

        if (pac == null)
        {
            return false;
        }

        _cache.Add(klic, pac);

        return true;
    }

    /// <summary>
    /// Vytvoření nového pacienta
    /// </summary>
    /// <param name="klic"></param>
    /// <param name="pacient"></param>
    /// <returns></returns>
    public bool Add(int IdPacienta, string jmeno, string prijmeni, DateTime datumNarozeni, string cisloPojisteni, string telefon, int zubarId)
    {
        if(SerachInDb(zubarId))
        {
            return true;
        }

        Pacient pacient = new Pacient(0, jmeno, prijmeni, datumNarozeni, cisloPojisteni, telefon, zubarId);

        if(PacientDataGateway.GetInstance().insert(pacient))
        {
            // TODO: Selectnu si púoslední záznam z DB a zjistím so jeho id
        }

        return true;
    }

}

And there is Pacient Mapper:

      public List<Pacient> SelectPacient()
    {
        Database db = new Database();
        db.Connect();

        SqlCommand command = db.CreateCommand("SELECT ID,jmeno,prijmeni,datumNarozeni,cisloPojisteni,telefon,Zubarid FROM pacient");

        SqlDataReader reader = db.Select(command);

        List<Pacient> pacienti = new List<Pacient>();

        Pacient pacient = null;
        while (reader.Read())
        {
            pacient = new Pacient();

            pacient.Id = reader.GetInt32(0);
            pacient.Jmeno = reader.GetString(1);
            pacient.Prijmeni = reader.GetString(2);
            pacient.DatumNarozeni = reader.GetDateTime(3);
            pacient.CisloPojisteni = reader.GetString(4);
            pacient.Telefon = reader.GetString(5);
            pacient.idZubare = reader.GetInt32(6);

            pacienti.Add(pacient);
        }
        reader.Close();
        db.Close();

        return pacienti;
    }
  • 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-06-15T06:22:19+00:00Added an answer on June 15, 2026 at 6:22 am

    You must check your IdentityMap for the presence of an object before getting it from the database, this prevents you getting multiple copies of the same object and subsequent update conflicts. When you ask for a single object by Id, you can check the IdentityMap for the object first and then only call the database if you don’t already have the object. For more than one object, this cannot be done as the results are non-deterministic. You have to query the database and then check the map, filling in the gaps.

    Ultimately, the IdentityMap’s job is to ensure that only one in-memory copy of an object exists for a give scope at any time. If yours is achieving this, then it’s job done.

    If your map is keyed by Id, you will need an Id before you add it to the map. This can be done on the client or via in insert query. I prefer client-side allocation of keys as you don’t have to make a round-trip to get an Id. However, your proposed approach seems ok.

    My own implementation of an IdentityMap is keyed by Type and then Id using nested Dictionaries. This is handy if your keys are table-scoped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing the Fowler's Accountability Pattern. I have Party with subclasses User, and
I'm looking at implementing some LINQ to SQL but am struggling to see how
I'm implementing an MVC solution that has some Web API endpoints for various data
I am implementing Omniauth- Identity and I have seen the Railcast by Ryan Bates
We are implementing federated identity management and have a scenario where users need to
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
When implementing the Strategy Pattern, where does one put the code that determines which
I have a bare bones ORM implementation, consisting of data mappers which load and
We are implementing a Secure Token Service (STS) which uses Windows Identity Foundation to
I am implementing the SAML2 Single Log Out Protocol. My Identity Provider uses the

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.