/// <summary>
 /// Creates an entity object that is attached to a <see cref="BrightstarEntityContext"/>
 /// and bound to a resource
 /// </summary>
 /// <param name="context">The context that the object will be attached to</param>
 /// <param name="identity">The identity that the object will be bound to</param>
 public BrightstarEntityObject(BrightstarEntityContext context, Uri identity)
 {
     _context   = context;
     DataObject = _context.GetDataObject(identity, false);
     _context.TrackObject(this);
     this.TriggerCreatedEvent(context);
 }
 internal void TriggerCreatedEvent(BrightstarEntityContext context)
 {
     if (context == null || this.DataObject.IsNew)
     {
         OnCreated(context);
     }
 }
 /// <summary>
 /// Creates a domain object
 /// </summary>
 /// <param name="context">The context that the domain object is attached to</param>
 /// <param name="dataObject">The underlying Brightstar data object</param>
 public BrightstarEntityObject(BrightstarEntityContext context, IDataObject dataObject)
 {
     _context   = context;
     DataObject = dataObject;
     _context.TrackObject(this);
     this.TriggerCreatedEvent(context);
 }
Esempio n. 4
0
 /// <summary>
 /// Creates a domain object bound to a specific type
 /// </summary>
 /// <param name="context">The context that the new domain object will be attached to</param>
 /// <param name="entityType">The implementation class for the domain object</param>
 public BrightstarEntityObject(BrightstarEntityContext context, Type entityType)
 {
     _context   = context;
     DataObject = context.CreateDataObject(entityType);
     _context.TrackObject(this);
     TriggerCreatedEvent(context);
 }
Esempio n. 5
0
        /// <summary>
        /// Attaches the object to the specified context
        /// </summary>
        /// <param name="context"></param>
        /// <param name="overwriteExisting"></param>
        public void Attach(EntityContext context, bool overwriteExisting = false)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!(context is BrightstarEntityContext))
            {
                throw new ArgumentException(
                          String.Format("An object of type {0} can only be attached to a context that extends {1}",
                                        GetType().FullName, typeof(BrightstarEntityContext).FullName));
            }
            if (IsAttached)
            {
                if (!context.Equals(_context))
                {
                    _context.UntrackObject(this);
                }
            }
            _context = context as BrightstarEntityContext;
            if (_identity == null)
            {
                AssertIdentity();
            }
            if (DataObject == null && _identity != null)
            {
                DataObject = _context.GetDataObject(new Uri(_identity), false);
                var identityInfo = EntityMappingStore.GetIdentityInfo(GetType());
                if (identityInfo != null && identityInfo.EnforceClassUniqueConstraint && !overwriteExisting)
                {
                    _context.EnforceClassUniqueConstraint(_identity, EntityMappingStore.MapTypeToUris(GetType()));
                }
                foreach (var typeUri in EntityMappingStore.MapTypeToUris(GetType()))
                {
                    if (!String.IsNullOrEmpty(typeUri))
                    {
                        var typeDo = _context.GetDataObject(new Uri(typeUri), false);
                        if (typeDo != null)
                        {
                            DataObject.AddProperty(Client.DataObject.TypeDataObject, typeDo);
                        }
                    }
                }
            }
//            if (DataObject != null)
//            {
            _context.TrackObject(this);
//            }

            if (_currentItemValues != null)
            {
                foreach (var propertyName in _currentItemValues.Keys.ToList())
                {
                    PropertyInfo p = GetType().GetProperty(propertyName);
                    p.SetValue(this, _currentItemValues[propertyName], null);
                }
                _currentItemValues.Clear();
            }
        }
 /// <summary>
 /// Removes the object from its current context
 /// </summary>
 public void Detach()
 {
     if (_context == null)
     {
         return;
     }
     _context.UntrackObject(this);
     _context = null;
 }
 /// <summary>
 /// Creates a new entity collection
 /// </summary>
 /// <param name="context">The context that manages the entities</param>
 /// <param name="parent">The parent entity that contains this collection</param>
 /// <param name="propertyType">The property type that the collection maps to</param>
 /// <param name="isInverse">True if the collection represents the inverse of <paramref name="propertyType"/></param>
 public BrightstarEntityCollection(BrightstarEntityContext context, BrightstarEntityObject parent, string propertyType, bool isInverse = false) :
     base(new EntityFrameworkCollectionQueryProvider(QueryParser.CreateDefault(), new EntityFrameworkQueryExecutor(context)))
 {
     _context         = context;
     _parent          = parent;
     _propertyTypeUri = propertyType;
     _propertyType    = _context.GetDataObject(new Uri(propertyType), false);
     _isInverse       = isInverse;
 }
Esempio n. 8
0
 /// <summary>
 /// Creates a new entity set attached to the specified context
 /// </summary>
 /// <param name="context">The parent context for the entity set. Must be an instance of <see cref="BrightstarEntityContext"/>.</param>
 public BrightstarEntitySet(EntityContext context) : base(context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     _context = context as BrightstarEntityContext;
     if (_context == null)
     {
         throw new ArgumentException("A BrightstarEntitySet must be attached to a BrightstarEntityContext");
     }
 }
        /// <summary>
        /// Attaches the object to the specified context
        /// </summary>
        /// <param name="context"></param>
        public void Attach(EntityContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!(context is BrightstarEntityContext))
            {
                throw new ArgumentException(
                          String.Format("An object of type {0} can only be attached to a context that extends {1}",
                                        GetType().FullName, typeof(BrightstarEntityContext).FullName));
            }
            if (IsAttached)
            {
                if (!context.Equals(_context))
                {
                    _context.UntrackObject(this);
                }
            }
            _context = context as BrightstarEntityContext;
            if (DataObject == null && _identity != null)
            {
                DataObject = _context.GetDataObject(new Uri(_identity), false);
                foreach (var typeUri in _context.Mappings.MapTypeToUris(GetType()))
                {
                    if (!String.IsNullOrEmpty(typeUri))
                    {
                        var typeDo = _context.GetDataObject(new Uri(typeUri), false);
                        if (typeDo != null)
                        {
                            DataObject.AddProperty(Client.DataObject.TypeDataObject, typeDo);
                        }
                    }
                }
            }
            if (DataObject != null)
            {
                _context.TrackObject(this);
            }

            if (_currentItemValues != null)
            {
                foreach (var propertyName in _currentItemValues.Keys.ToList())
                {
                    PropertyInfo p = GetType().GetProperty(propertyName);
                    p.SetValue(this, _currentItemValues[propertyName], null);
                }
                _currentItemValues.Clear();
            }
        }
 /// <summary>
 /// Runs after the entity object is created by the specified context.  It is not necessary to call the base method.
 /// </summary>
 /// <param name="context"></param>
 protected virtual void OnCreated(BrightstarEntityContext context)
 {
 }
Esempio n. 11
0
 /// <summary>
 /// Creates a domain object
 /// </summary>
 /// <param name="context">The context that the domain object is attached to</param>
 /// <param name="dataObject">The underlying Brightstar data object</param>
 public BrightstarEntityObject(BrightstarEntityContext context, IDataObject dataObject)
 {
     _context   = context;
     DataObject = dataObject;
     _context.TrackObject(this);
 }