/// <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); }
/// <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); }
/// <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); }
/// <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> /// 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> /// 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); }