DataAccessAdapter and my Services classes design

Posts   
 
    
methodman
User
Posts: 194
Joined: 24-Aug-2009
# Posted on: 13-May-2010 16:14:01   

I'm curious what would be the best practice for using the DataAccesAdapter.

Would be this class design acceptable ? The class is used in a windows service application.


public class CustomerService
{
    private IDataAccessAdapter dataAccessAdapter;
    
    public CustomerService(IDataAccessAdapter dataAccessAdapter)
    {
        this.dataAccessAdapter = dataAccessAdapter;
    }

    public CustomerEntity GetItem(int Id)
    {
        var customer = new CustomerEntity(Id);

        this.dataAccessAdapter.FetchEntity(customer);

        return customer;
    }

    public EntityCollection<CustomerEntity> GetList()
    {
        var coll = new EntityCollection<CustomerEntity>();

        this.dataAccessAdapter.FetchEntityCollection(coll,null);

        return coll;
    }
}

I looked up in the doc, but there isn't much info about how dataaccessadapter is working behind the scenes.

I saw often this pattern


    public CustomerEntity GetItem(int Id)
    {
        var customer = new CustomerEntity(Id);

        using(var adapter = new DataAccessAdapter())
        {
            adapter.FetchEntity(customer);  
        }

        return customer;
    }

but when there gonna be like 10 - 15 method in the class, the code would starts get messy.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 13-May-2010 18:09:15   

The "using" clause is essential to ensure that the database connection is closed and resources are released. You could put this outside your class, but how would that be any less messy than having it inside ?

FWIW, my manager classes tend to look very much like yours, but with the "using" statements.

If you are using C#3.0 you can use generics to create a single method to retrieve entities


public T GetEntity<T>(int Id) where T : EntityBase2
{

 using(var adapter = new DataAccessAdapter())
        {
            T entity = new T;
            (entity as EntityBase2).PrimaryKeyFields[0].Value = Id;
            adapter.FetchEntity(entity);    
        }

}

usage



CustomerEntity cust = GetEntity<CustomerEntity>(1);


et voila - one single method as opposed to one per entity type. You can do something similar with entity collections as well.

rboarman
User
Posts: 83
Joined: 01-Feb-2005
# Posted on: 13-Mar-2012 00:06:56   

I realize that this is an old thread, but I wanted to point out that the PrimaryKeyFields property is private which means that your code will not compile.

I am trying to delete an entity based on its primary key. Any suggestions?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Mar-2012 21:24:49   

rboarman wrote:

I am trying to delete an entity based on its primary key. Any suggestions?

That property comes from the IEntity2 interface, so it's public. Anyway I think you already had this working (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=20763) simple_smile

David Elizondo | LLBLGen Support Team