/**********************************************************************************/ internal static Crate FromJson(CrateManifestType manifestType, string id, JToken content) { return(new Crate(manifestType, id) { _rawContent = content }); }
public static bool TryResolveManifest(Type type, out CrateManifestType manifestType) { lock (GlobalTypeCacheLock) { if (ManifestsCache.TryGetValue(type, out manifestType)) { return(manifestType != CrateManifestType.Unknown); } var manifestAttr = (CrateManifestTypeAttribute)type.GetCustomAttribute(typeof(CrateManifestTypeAttribute), false); if (manifestAttr == null || manifestAttr.ManifestType == null) { if (typeof(Manifest).IsAssignableFrom(type) && !type.IsAbstract) { var sampleManifest = ((Manifest)Activator.CreateInstance(type)); manifestType = sampleManifest.ManifestType; } else { manifestType = CrateManifestType.Unknown; } } else { manifestType = manifestAttr.ManifestType; } ManifestsCache.Add(type, manifestType); return(manifestType != CrateManifestType.Unknown); } }
/**********************************************************************************/ public bool TryResolveType(CrateManifestType manifestType, out Type type) { lock (_typeMapping) { return(_typeMapping.TryGetValue(manifestType, out type)); } }
/**********************************************************************************/ /// <summary> /// Create new crate from JSON with specified manifest tyype. /// </summary> /// <param name="manifestType"></param> /// <param name="content"></param> /// <returns></returns> public static Crate FromJson(CrateManifestType manifestType, JToken content) { return(new Crate(manifestType) { _rawContent = content }); }
/**********************************************************************************/ public static bool TryResolveManifest(object instance, out CrateManifestType manifestType) { if (instance == null) { manifestType = CrateManifestType.Unknown; return(false); } var type = instance.GetType(); var manifest = instance as Manifest; if (manifest != null) { lock (GlobalTypeCacheLock) { if (!ManifestsCache.TryGetValue(type, out manifestType)) { manifestType = manifest.ManifestType; ManifestsCache.Add(type, manifestType); return(true); } } } return(TryResolveManifest(type, out manifestType)); }
/**********************************************************************************/ /// <summary> /// Convert DTO to Crate instance /// </summary> /// <param name="proxy"></param> /// <returns></returns> public Crate ConvertFromDto(CrateDTO proxy) { var manifestType = new CrateManifestType(proxy.ManifestType, proxy.ManifestId); IManifestSerializer serializer = GetSerializer(manifestType); Crate crate; if (serializer != null) { var content = proxy.Contents != null?serializer.Deserialize(proxy.Contents) : null; if (content != null) { crate = Crate.FromContent(content, proxy.Id); } else { crate = new Crate(manifestType, proxy.Id); } } else { crate = Crate.FromJson(manifestType, proxy.Id, proxy.Contents); } crate.Label = proxy.Label; crate.SourceActivityId = proxy.SourceActivityId; crate.Availability = proxy.Availability; return(crate); }
/**********************************************************************************/ /// <summary> /// Register custom serializer for manifest type. /// You don't have to call this method wile your manifest resides in Data project. /// </summary> /// <param name="manifestType"></param> /// <param name="serializer"></param> public void RegisterSerializer(CrateManifestType manifestType, IManifestSerializer serializer) { lock (_serializers) { _serializers.Add(manifestType, serializer); } }
/**********************************************************************************/ public static bool TryResolveManifest(string manifestTypeName, out CrateManifestType manifestType) { int typeId; if (_nameMap.TryGetValue(manifestTypeName, out typeId)) { manifestType = new CrateManifestType(manifestTypeName, typeId); return(true); } manifestType = CrateManifestType.Unknown; return(false); }
/**********************************************************************************/ public bool Equals(CrateManifestType other) { if (Id == other.Id) { return(true); } if (Id == Unknown.Id || other.Id == Unknown.Id) { return(false); } return(Id == Any.Id || other.Id == Any.Id); }
/**********************************************************************************/ private IManifestSerializer GetSerializer(CrateManifestType type) { lock (_serializers) { IManifestSerializer serializer; if (_serializers.TryGetValue(type, out serializer)) { return(serializer); } Type clrType; if (ManifestDiscovery.Default.TryResolveType(type, out clrType)) { var manifestAttr = clrType.GetCustomAttribute <CrateManifestSerializerAttribute>(); if (manifestAttr == null || manifestAttr.Serializer == null) { serializer = new DefaultSerializer(clrType); } else { if (typeof(IManifestSerializer).IsAssignableFrom(manifestAttr.Serializer) && manifestAttr.Serializer.GetConstructor(Type.EmptyTypes) != null) { serializer = (IManifestSerializer)Activator.CreateInstance(manifestAttr.Serializer); } else { throw new ArgumentException("Invalid serializer was specified for given manifest"); } } serializer.Initialize(this); } _serializers[type] = serializer; return(serializer); } }
public CrateManifestTypeAttribute(int manifestId, string manifestTypeName) { ManifestType = new CrateManifestType(manifestTypeName, manifestId); }
/**********************************************************************************/ // Functions /**********************************************************************************/ /// <summary> /// Crate new crate with specified manifest type and Id. /// </summary> /// <param name="manifestType"></param> /// <param name="id"></param> public Crate(CrateManifestType manifestType, string id) { _manifestType = manifestType; Id = id; }
public bool TryResolveManifestType(string manifestTypeName, out CrateManifestType manifestType) { return(ManifestTypeCache.TryResolveManifest(manifestTypeName, out manifestType)); }
/**********************************************************************************/ public bool TryGetManifestType(Type type, out CrateManifestType manifestType) { return(ManifestTypeCache.TryResolveManifest(type, out manifestType)); }
public CrateManifestTypeAttribute(object manifestId) { ManifestType = CrateManifestType.FromEnum((Enum)manifestId); }
/// <summary> /// Crate new crate with specified manifest type, Id and availability. /// </summary> /// <param name="manifestType"></param> /// <param name="id"></param> /// <param name="availability">Crate availability</param> public Crate(CrateManifestType manifestType, string id, AvailabilityType availability) { _manifestType = manifestType; Id = id; Availability = availability; }
/**********************************************************************************/ /// <summary> /// Crate new crate with specified manifest type and autogenerated Id /// </summary> /// <param name="manifestType"></param> public Crate(CrateManifestType manifestType) { _manifestType = manifestType; Id = Guid.NewGuid().ToString(); }