Hi Shane, thanks for taking the time to answer.
Yes, you are talking about an Identity Map I guess, but this solution is harder to implement and tune. It's my last resort though.
Of course, definitely the select="CustomerById" needs to be removed. In fact, the command CustomerById uses a different cache that the command CustomerAll so, if we use cache for both commands we can stop IBatis building different instances.
One approach I used is for resolving this is implementing the CustomerRepository.GetAll() with an IBatis query (cached) and the CustomerRepository.GetById() resolved in memory. But I'm not very happy with this solution either
Of course, the Order ResultMap just get the Customer Id and I resolve the property getter or setter that way.
class Order {
    // This field is mapped in IBatis
    object customerId;
    // this is a transient field
    private Customer customer;
    public Customer Customer
        get {
            if ( customer == null )
                customer = CustomerRepository.GetById( customerId );
            return customer;
        set customerId = value.Id; }

From: Shane Courtrille [mailto:shanecourtrille@gmail.com]
Sent: Lunes, 07 de Mayo de 2007 07:03 p.m.
To: user-cs@ibatis.apache.org; peix-listas@praxia.com.ar
Subject: Re: Kind of architecture question

I'm just learning iBatis.Net now but I have used the repository <-> mapper pattern in the past.  Usually what I have done is have the repository contain a reference to a cache.  It checks the cache before using the mapping layer to retrieve the item.  The problem I see is your Order select="CustomerById".  Without knowing more about iBatis I would suggest you may need to remove that and instead have your repository fill in the reference after it gets the Order.  Definitely not a solution I'm in love with though.


On 5/7/07, Carlos Peix <peix-listas@praxia.com.ar> wrote:
Hi all,
I used IBatis.NET with success in various projects now but I am still not very happy with my implementations. I'll try to explain my concerns.
My environment is .NET 1.1 but I think .NET 2.0 is the same thing. I should say that I try to work guided by the DDD principles, so I access the final store through repositories.
I have, for example, an entity Customer (aggregate root or simply a persistent identifiable object) and a CustomerRepository with the following interface:
Customer CustomerRepository.GetById( object id );
IList CustomerRepository.GetAll();
and I also have a Order and OrderRepository with the following interface
OrderRepository.GetByNumber( int number );
The problem I face all the time with IBatis is the I get different instances if I do:
// implemented with a Mapper.QueryForObject( "CustomerById", id );
Customer customer1 = CustomerRepository.GetById( object id );
// implemented with a Mapper.QueryForList( "CustomerAll" );
Customer customer2 = CustomerRepository.GetAll()[0];
// implemented with a Mapper.QueryForObject( "OrderByNumber", number );
// and Customer mapped this way in the OrderResultMap:
//  <result property="customer" column="CustomerId" select="CustomerById" />
Customer customer3 = OrderRepository.GetByNumber( 100 ).Customer;
But customer1.Id, customer2.Id and customer3.Id are the same.
Ok, this is a situation that need to be controlled, otherwise I could modify or check different instances of the same object. I was told previously that this is not an IBatis problem and I see why (in fact IBatis doesn't know anything about object identity, so it can't control this).
The question is: how are you structuring your code to control that situation? there are some "recommended practices"? I started with some ideas (one of them include IBatis cache configuration) but I'm not happy with any of them.
Sorry about the long post and thanks in advance.
Carlos Peix