db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Data Access Layer

Last post 07-18-2008, 08:15 AM by Goran. 11 replies.
Sort Posts: Previous Next
  •  02-28-2007, 10:04 AM 34292

    Data Access Layer

    I would like to have a separate access layer from the business objects, so I could change of database without modifying the BO. I could implement a simple data layer, an object with method who’ll get object to save, update, etc. as parameters, like MyDataAccesObjectLayer.Save(aCustomer).

     

    But how that could work? In order to save aCustomer, MyDataAccesObjectLayer should know the private property of aCustomer? Or the DataLayer object could in a propriety of the object to save; so it would have access to its members?

     

    Do you know any example or framework that does that with db4o?

     

    Thanks :)


    Richard
  •  02-28-2007, 01:57 PM 34299 in reply to 34292

    Re: Data Access Layer

    MyDataAccesObjectLayer.Save does not need to know anything about Customer properties in order to implement it with db4o.

    You can just call IObjectContainer.Set(aCustomer).

    But each class has different data and may need different manipulation when storing/retrieving its instances from a database.

    In db4o case, some fields can use [Transient] (or Your custom one or [NonSerializable] etc.) attribute (.NET) so that they are not saved, You can use db4o object callbacks or external callbacks or db4o object translators - it all depends on what You want to do in a class.

    So sometimes it is even better to work internally with db4o functionality in your class to get better performance etc.


    Best regards
    Marek
  •  03-04-2007, 08:22 PM 34500 in reply to 34292

    Re: Data Access Layer

    Hi Richard,

    Here I make an example of how to build a 3 tiers app in C# with Spring.Net and Db4o, it's just a begining. The app have only the "way" Customer (you can save,delete and retrieve a Customer):
     

    Data Layer:

    • RepositoryBase
    • CustomerRepository

     
    Business Layer:

    • Customer
    • BizComponentBase
    • CustomerBizComponent

    Presentation Layer:

    •  Just a Console for now.

     

    The example show how to configure Spring for this object, and lets the oportunity to replace the db4o repositories and build others (maybe NHibernate) respecting the contracts. 

     

    Greetings 

     Download: http://darioquintana.com.ar/files/dev/SalesDb4oSpring - v0.0.1.zip


    Dario Quintana
    b l o g g i n g
    Filed under: , ,
  •  03-06-2007, 04:41 PM 34570 in reply to 34500

    Re: Data Access Layer

    Thanks for yours answers.

     

    Marek Istvanek:

    MyDataAccesObjectLayer.Save does not need to know anything about Customer properties in order to implement it with db4o.

     

    Agree, but I am looking for a universal solution, who can work with db4o and for another database, like a SQL one.

     

    Dario Quintana:

    Here I make an example of how to build a 3 tiers app in C# with Spring.Net and Db4o, it's just a begining.

    Interesting example, Dario, thanks for it. It does not answer to my question, because using db4o, you can pass the object to the .save() method of the db4o object, it is able to save it with its private properties. But if I use another database like MS SQL, I have to find another solution.

     

    I just realized that I could look in the db4o code, to see how it does to save objects! But someone on another forum gave me another solution. To make an interface in a separate package, to implement this interface in the business object to expose private fields, but with explicit interface, and to make the interface reference available only to the BO assembly and to Data Layer assembly. So other assemblies could not use the interface to access private fields of the BO, but the Data Access Layer could. I think I’ll go with this solution.

     

    Now, I did not know Spring.Net. I see in your AppContext class that it can read config file and create instance from them. That is very interesting. Too bad that my project will start very soon now, I have no time left to study Spring.Dot net, who seems a little complex.

     

     

     

     


    Richard
  •  03-06-2007, 05:10 PM 34572 in reply to 34570

    Re: Data Access Layer

    Maybe I don't undestand what are you looking for but... 

    If you gonna use another provider just lika MSSQL, o even NHibernate,  you only need implement another RepositoryBase and CustomerRepository, 'cause in the app for that implementation. I never call this object in the app, I just call to their interfaces: IRepositoryBase and ICustomerRepository.

    In that way I'm surely that I can use whatever Repository.

     

    Greetings
     


    Dario Quintana
    b l o g g i n g
  •  03-06-2007, 05:32 PM 34574 in reply to 34572

    Re: Data Access Layer

    In your console app, you do :

     

    CustomerBiz.Save(Customer);

     

    My problem is the parameter, Customer. CustomerBiz’s job is to save the object, Customer.  In your application, using db4o, it works, because db4o know how to save a complete object, even with its private fields.

     

    But let’s say I want to use my own home-made text database. I have to make an implementation of RepositoryBase and CustomerRepository able to read the private fields of Customer, and that is the question of this topic, how can I do that?

     

    In my previous answer, I said that someone gave a possible solution to me, the “Separate Interface Pattern”.


    Richard
  •  07-17-2008, 11:01 AM 50180 in reply to 34500

    Re: Data Access Layer

    Hi Darion,

     Thanks for that sample of using Spring with db4o.

     Now i have a question : Using this architecture, how can we manage transactions ?

     Since there is anyway to access directly the IObjectContainer, because it's limited to the Repository classes. 

     

    Thanks in advance.

  •  07-17-2008, 12:52 PM 50181 in reply to 50180

    Re: Data Access Layer

    Hi

    You have very flavors, here I give just 3:

    1) You can expose transactional methods in Repository clases: BeginTransaction(); RollBack(); Commit(); then in Business classes you can use it.

    2) You can also play a little with contexts and create another class called UnitOfWork with those transactional methods to manage this issue. It is, everything in the transactional context it's supposed to be within a transaction, like the TransactionScope approach.

    3) You can use Spring + Spring Modules to manage transactions using AOP.

    Best regards.


    Dario Quintana
    b l o g g i n g
    Filed under: ,
  •  07-17-2008, 05:25 PM 50188 in reply to 50181

    Re: Data Access Layer

    Thanks !

     

    I'll try that :)

     

    Best regards,

  •  07-17-2008, 07:06 PM 50192 in reply to 50181

    Re: Data Access Layer

    Hi,

    I'm currently trying to setup this architecture on a ASP.NET Web Application, but i didn't find a way to use it safely.

    Here is my test code in a sample asp.net page :

     

        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ICustomerBusiness cusBiz = AppContext.GetObject<ICustomerBusiness>();

                Customer cus = new Customer();
                cus.FirstName = "Scott";
                cus.LastName = "Scott";
                cus.Email = "scott@blah.org";

                cusBiz.Save(cus);

                Customer saved = cusBiz.GetByEmail("scott@blah.org");
                if (saved != null)
                    Response.Write(saved.Email);
                cusBiz.Delete(saved);
            }
        }

     

    After making multiple speed refresh "F5" of my page, i got duplicates Customers stored in the database. Maybe the request didn't execute the cusBiz.Delete(saved) method ?

    Does anyone have an idea ?

    Thanks in advance.

     

  •  07-17-2008, 09:29 PM 50197 in reply to 50192

    Re: Data Access Layer

    Just add something to my previous post

    the duplicates Customers appears only when Methods Save & Delete from RepositoryBase automaticaly Commit the Container.

    if i modify theses methods like that it works fine but ... :

            public void Save(TEntity obj)
            {
                    Container.Store(obj);
            }
            public void Delete(TEntity obj)
            {
                    Container.Delete(obj);
            }

    But ... when the Container is Commited ? and isn't dangerous to do that without Commiting ?? ....

     

    Thanks.

  •  07-18-2008, 08:15 AM 50201 in reply to 50192

    Re: Data Access Layer

    Hi!

    Here's a possible scenario:

    1. Thread 1: You create a new Customer C1 and set his email to scott@blah.org
    2. Thread 1: You store the new Customer C1
    3. Thread 2: By pressing refresh another thread creates another Customer C2 and set his email to scott@blah.org
    4. Thread 2: You store the new Customer C2 - now you have two customers in db
    5. Thread 1: Retrieves the first customer C1 that fits the query
    6. Thread 2: Retrieves the first customer C1 that fits the query
    7. Thread 1: Deletes C1
    8. Thread 2: Tries and fails to delete C1
    9. C2 is left in the db.
    Goran
View as RSS news feed in XML