public string SaveTypedObject(object instance, string id, IStoredObjectFields fieldConfig)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var record = NewRecord(fieldConfig.RecordType);
            //think will serialise it
            var    type       = instance.GetType();
            var    serializer = new DataContractJsonSerializer(type);
            string jsonString = null;

            using (var stream = new MemoryStream())
            {
                serializer.WriteObject(stream, instance);
                jsonString = Encoding.Default.GetString(stream.ToArray());
            }
            var nameField = this.GetPrimaryField(fieldConfig.RecordType);

            if (!nameField.IsNullOrWhiteSpace())
            {
                record.SetField(nameField, instance.ToString(), this);
            }
            record.SetField(fieldConfig.ValueField, jsonString, this);
            record.SetField(fieldConfig.AssemblyField, type.Assembly.GetName().Name, this);
            record.SetField(fieldConfig.TypeQualfiedNameField, type.FullName, this);
            record.SetField(fieldConfig.TypeField, type.Name, this);
            if (id.IsNullOrWhiteSpace())
            {
                return(Create(record));
            }
            else
            {
                record.Id = id;
                Update(record, null);
                return(id);
            }
        }
 public LookupConnectionFor(string property, IStoredObjectFields fieldConfig)
     : base(property)
 {
     FieldConfig = fieldConfig;
 }
Exemplo n.º 3
0
        public static object LoadObject(this IRecordService recordService, Lookup lookup, IStoredObjectFields objectConfig)
        {
            var record         = recordService.Get(lookup.RecordType, lookup.Id);
            var assemblyString = record.GetStringField(objectConfig.AssemblyField);
            var typeString     = record.GetStringField(objectConfig.TypeQualfiedNameField);
            var jsonString     = record.GetStringField(objectConfig.ValueField);
            var assembly       = Assembly.Load(assemblyString);
            var type           = assembly.GetType(typeString);

            return(JsonHelper.JsonStringToObject(jsonString, type));
        }
Exemplo n.º 4
0
        public static IRecordService LoadService(Lookup lookup, IRecordService recordService, IStoredObjectFields objectConfig, IResolveObject objectResolver)
        {
            var connection        = recordService.LoadObject(lookup, objectConfig);
            var type              = connection.GetType();
            var connectionConfigs = objectResolver.ResolveType(typeof(ConnectionConfigs)) as ConnectionConfigs;

            if (connectionConfigs == null)
            {
                throw new NullReferenceException(string.Format("Error loading {0}. The resolved value is null", typeof(ConnectionConfigs).Name));
            }
            var matchingConfig = connectionConfigs.ConnectionTypes.Where(c => type == c.ConnectionType);

            if (!matchingConfig.Any())
            {
                throw new NullReferenceException(string.Format("Error loading {0}. No {1} has ConnectionType of {2}. Need to add the {1} to the {3} object", typeof(IRecordService).Name, typeof(ConnectionConfig).Name, type.Name, typeof(ConnectionConfigs).Name));
            }
            return(LoadServiceForConnection(connection, matchingConfig.First().ServiceType));
        }