This project has moved and is read-only. For the latest updates, please go here.

Abstracting the BS EntityContext

May 29, 2014 at 6:29 PM
I'm starting out on Brightstar and it really is interesting. So, I thought of using this for a personal project so that I can learn though it. Coming from EntityFramework and SQL, I usually follow the below interface to abstract my EntityContext. I'd like to know the BrightStar alternatives to the SQL EntityFramework counterparts for the same.
public interface IRepository:IDisposable
    {
        IRepository Add<T>(T item);
        IRepository Update<T>(T item);
        IRepository Delete<T>(T item);
        T Create<T>();
        int SaveChanges<T>(T item);

        IQueryable<T> Store<T>();


    }
  1. For the Add and Delete methods, I used to delegate the call to this.Set<T>().Add and this.Set<T>().Remove(). What about BrightStar?
  2. For Update, I used to delegate it to this.Entry(item).State.
  3. I encountered some NullReferenceException when trying to add items to a navigation property (collection) of a manually created(using new not Create method of context) object. What is the recommended way of doing this? Should I always add everything to context and relate it instead of creating an object graph and trying to add entire thing together to the context?
Jun 1, 2014 at 10:06 AM
threecoins wrote:
  1. For the Add and Delete methods, I used to delegate the call to this.Set<T>().Add and this.Set<T>().Remove(). What about BrightStar?
To add an object that is already constructed, you should delegate to the Add method on the type collection in the entity context (e.g. context.Persons.Add(p))
To delete an object completely use the context.DeleteObject() method.
  1. For Update, I used to delegate it to this.Entry(item).State.
    There isn't really an equivalent for this, you just modify the proxy object, the modifications are persisted to the store when you call SaveChanges()
  2. I encountered some NullReferenceException when trying to add items to a navigation property (collection) of a manually created(using new not Create method of context) object. What is the recommended way of doing this? Should I always add everything to context and relate it instead of creating an object graph and trying to add entire thing together to the context?
You can create the object graph before attaching the objects to a context. However, the object constructors do not automatically instantiate any properties, so they will all have their default value, which in the case of collection properties means null. So, to before adding objects to a navigation property that is a collection you should ensure that property is set. The property type is ICollection<T>, I usually use a List<T> object to instantiate the property.

Hope this helps!
Jun 1, 2014 at 5:30 PM
Thank you for your reply.
For the Add<T>, I was hoping for a generic method as I cannot decide the generated collection to which it is to be added. Is there a way to get a Object collection for a given T where T is an interface marked with Entity?

In case of update, Is there a way to modify a disconnected graph hand created rather than the ones retrieved from the context,(for example, an asp.net model received from a POST to be updated to the data store)?
Jun 2, 2014 at 9:09 AM
There isn't a version of Add that does that, but it sounds like a nice idea to be able to add an object or collection of objects to the context.

In the case of update, modifying a disconnected graph will currently cause you problems I think because it will only add triples for the properties / relationships of the object graph and not remove triples for the old / deleted properties. I think in Microsoft's EF, this is handled by diffing the modified object graph and the stored objects - I'm not sure that approach would work for us. One possibility might be to overload the Attach method that can be used to attach an object to a context so that you can choose what happens with collections and unset (or default) value properties - whether you overwrite the stored values or merge the local values with the stored values. Perhaps it could also be optionally controlled by a callback that allows you to decide on a property-by-property basis. Do you think something like this would work for your use-case ?
Jun 5, 2014 at 1:42 PM
i use repositories with brightstar like this:
    public class SetRepository<TEntity> where TEntity : BrightstarDB.EntityFramework.BrightstarEntityObject
    {
        private readonly ScTEntityContext _dbContext;
        public SetRepository(ScTEntityContext context)
        {
            _dbContext = context;
        }


        public TEntity New()
        {
            var T = _dbContext.CreateObject<TEntity>();
            return T;
        }

        public TEntity Delete(TEntity T)
        {
            T.Unbecome<TEntity>();
            _dbContext.SaveChanges();
            return T;
        }
    }
in uow i have a save method and my add works perfectly :)

i also have a get repository that uses the interfaces