예제 #1
0
        public override Property ReadJson(JsonReader reader, Type objectType, Property existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            IReferenceResolver resolver = serializer.Context.Context as IReferenceResolver;

            Property target = null;
            string   id     = string.Empty;

            JObject json = JObject.Load(reader);

            foreach (JProperty property in json.Properties())
            {
                if (property.Name == "$ref")
                {
                    target = (Property)serializer.Deserialize(json.CreateReader());
                }
                else if (property.Name == "$id")
                {
                    id = (string)property.Value;
                }
                else if (property.Name == "identity")
                {
                    Guid identity = new Guid((string)property.Value);
                    IReferenceObjectFactory factory = MetadataPersistentContext.Current.Factory;
                    target = factory.New <Property>(identity);
                    resolver.AddReference(null, id, target);
                }
            }
            return(target);
        }
예제 #2
0
            public static Entity Select(Guid identity)
            {
                Entity                  entity  = null;
                IPersistentContext      context = MetadataPersistentContext.Current;
                IReferenceObjectFactory factory = MetadataPersistentContext.Current.Factory;

                using (SqlConnection connection = new SqlConnection(context.ConnectionString))
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        connection.Open();
                        command.CommandType = CommandType.Text;
                        command.CommandText = SelectCommandText;
                        SqlParameter parameter = new SqlParameter("key", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Input,
                            Value     = identity
                        };
                        command.Parameters.Add(parameter);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                entity = (Entity)context.Factory.New(typeof(Entity), identity);
                                // check if nothing was found in IdentityMap - in-memory cash
                                if (entity.State == PersistentState.New)
                                {
                                    entity.State = PersistentState.Loading;
                                    Guid guid;
                                    entity._namespace = factory.New <Namespace>((Guid)reader[0]);
                                    guid              = (Guid)reader[1];
                                    entity.owner      = (guid == Guid.Empty) ? null : factory.New <Entity>(guid);
                                    guid              = (Guid)reader[2];
                                    entity.parent     = (guid == Guid.Empty) ? null : factory.New <Entity>(guid);
                                    entity.name       = (string)reader[3];
                                    entity.code       = (int)reader[4];
                                    entity.version    = (byte[])reader[5];
                                    entity.alias      = reader.GetString(6);
                                    entity.isAbstract = reader.GetBoolean(7);
                                    entity.isSealed   = reader.GetBoolean(8);
                                    entity.State      = PersistentState.Original;
                                }
                            }
                        }
                    }
                return(entity);
            }
 public DataMapper(string connectionString, IReferenceObjectFactory factory)
 {
     ConnectionString = connectionString;
     Factory          = factory;
 }