I am very very new to db4o, and c# in general, and am having real trouble getting my head around how to start using it - the tutorial provided isn't helping me a lot - I understand the jist of it, but am having trouble understanding where to put basic code, eg the
using(IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
{
//do something with db4o
}
I'm sure this is down to my lack of c# knowledge, but if somebody could point me in the right direction (even a decent tutorial with full examples so i can see a project with db4o implemented) that would be great :)
I come from a web development background, so the database idea is no problem, but for some reason I can't seem to fully understand the c# 'way of doing things'
It really comes down to what you're trying to build. Are you just playing around to test out the database or do you have a specific project/program you are trying to build? Accessing the database should be treated the same as any other project would treat it.
For example, if you are using the database to store employees and you wish to return all employees with last name "Smith", you may do something like this (very basic example and not good design... just trying to show where code would go to get things working):
public class Employee
{
public FirstName { get; set; }
public LastName { get; set; }
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
public static class EmployeeFactory
{
public static IEnumerable<Employee> GetEmployeesNamedSmith(IObjectContainer db)
{
var employees = from Employee e in db
where e.LastName.Equals("Smith")
select e;
foreach (var emp in employees)
{
yield return emp;
}
}
}
In your main application you could have something like this:
public static void Main()
{
var config = Db4oEmbedded.NewConfiguration();
// code to create and add employees goes here.
// access employees that have Smith as the last name
using (var db = Db4oEmbedded.OpenFile(config, "database.db4o"))
{
foreach (var e in EmployeeFactory.GetEmployeesNamedSmith(db))
{
Console.WriteLine(e.FirstName + " " + e.LastName);
}
}
}
Disclaimer: I have not tried compiling this code and have only dabbled in db4o (it's been about 6 months since I last touched it... though I'd like to try it out again).
Hopefully this helps in some way. I'm not sure if this is what you were looking for.