Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Created June 8, 2010 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanviscuso/429554 to your computer and use it in GitHub Desktop.
Save germanviscuso/429554 to your computer and use it in GitHub Desktop.
using Db4objects.Db4o;
using System.Linq;
using Db4objects.Db4o.Linq;
using System.Web;
using System.IO;
using System;
using System.Collections.Generic;
namespace NoSQL {
public class Db4oSession : IDisposable, ISession {
private IObjectContainer db;
public IObjectContainer Container {
get {
return db;
}
}
protected Db4oSession(IObjectServer server) {
db= server.OpenClient();
}
/// <summary>
/// Returns all T records in the repository
/// </summary>
public IQueryable<T> All<T>() {
return (from T items in db
select items).AsQueryable();
}
/// <summary>
/// Finds an item using a passed-in expression lambda
/// </summary>
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) {
return All<T>().SingleOrDefault(expression);
}
/// <summary>
/// Saves an item to the database
/// </summary>
/// <param name="item"></param>
public void Save<T>(T item) {
db.Store(item);
}
/// <summary>
/// Deletes an item from the database
/// </summary>
/// <param name="item"></param>
public void Delete<T>(T item) {
db.Delete(item);
}
/// <summary>
/// Deletes subset of objects
/// </summary>
public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) {
var items = All<T>().Where(expression).ToList();
items.ForEach(x => db.Delete(x));
}
/// <summary>
/// Deletes all T objects
/// </summary>
public void DeleteAll<T>() {
var items = All<T>().ToList();
items.ForEach(x => db.Delete(x));
}
/// <summary>
/// Commits changes to disk
/// </summary>
public void CommitChanges() {
//commit the changes
db.Commit();
}
public void Dispose() {
//explicitly close
db.Close();
//dispose 'em
db.Dispose();
}
}
}
using (var session = NoSQL.SessionFactory.Current) {
//create a category and save it
var cat = new Category("cat 1");
var cat2 = new Category("cat 2");
Product p = new Product();
p.SKU = "2354";
p.Name = "Something peanuty";
p.Categories.Add(cat);
p.Categories.Add(cat2);
session.Save(p);
session.CommitChanges();
}
using System;
namespace NoSQL {
public interface ISession:IDisposable {
void CommitChanges();
Db4objects.Db4o.IObjectContainer Container { get; }
void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression);
void Delete<T>(T item);
void DeleteAll<T>();
void Dispose();
T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression);
System.Linq.IQueryable<T> All<T>();
void Save<T>(T item);
}
}
using Db4objects.Db4o;
using System.Linq;
using Db4objects.Db4o.Linq;
using System.Web;
using System.IO;
using System;
using System.Collections.Generic;
namespace NoSQL {
public class SessionFactory {
static ISession _current;
//this needs to stay static - can't have more than
//one server on the file
static IObjectServer _server;
public static ISession CreateSession() {
if (_server == null) {
string _dbPath = System.Configuration.ConfigurationManager
.ConnectionStrings["ObjectStore"].ConnectionString;
//check to see if this is pointing to data directory
//change as you need btw
if (_dbPath.Contains("|DataDirectory|")) {
//we know, then, that this is a web project
//and HttpContext is hopefully not null...
_dbPath = _dbPath.Replace("|DataDirectory|", "");
string appDir = HttpContext.Current.Server.MapPath("~/App_Data/");
_dbPath = Path.Combine(appDir, _dbPath);
}
_server = Db4oFactory.OpenServer(_dbPath, 0);
}
return new Db4oSession(_server);
}
public static Db4oSession Current {
get {
if (_current == null)
_current = CreateSession();
return (Db4oSession)_current;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment