Esempio n. 1
0
        /**********************************************************************************/

        internal static Crate FromJson(CrateManifestType manifestType, string id, JToken content)
        {
            return(new Crate(manifestType, id)
            {
                _rawContent = content
            });
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        /**********************************************************************************/

        public bool TryResolveType(CrateManifestType manifestType, out Type type)
        {
            lock (_typeMapping)
            {
                return(_typeMapping.TryGetValue(manifestType, out type));
            }
        }
Esempio n. 4
0
 /**********************************************************************************/
 /// <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
     });
 }
Esempio n. 5
0
        /**********************************************************************************/

        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));
        }
Esempio n. 6
0
        /**********************************************************************************/
        /// <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);
        }
Esempio n. 7
0
 /**********************************************************************************/
 /// <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);
     }
 }
Esempio n. 8
0
        /**********************************************************************************/

        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);
        }
Esempio n. 9
0
        /**********************************************************************************/

        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);
        }
Esempio n. 10
0
        /**********************************************************************************/

        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);
            }
        }
Esempio n. 11
0
 public CrateManifestTypeAttribute(int manifestId, string manifestTypeName)
 {
     ManifestType = new CrateManifestType(manifestTypeName, manifestId);
 }
Esempio n. 12
0
 /**********************************************************************************/
 // 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;
 }
Esempio n. 13
0
 public bool TryResolveManifestType(string manifestTypeName, out CrateManifestType manifestType)
 {
     return(ManifestTypeCache.TryResolveManifest(manifestTypeName, out manifestType));
 }
Esempio n. 14
0
        /**********************************************************************************/

        public bool TryGetManifestType(Type type, out CrateManifestType manifestType)
        {
            return(ManifestTypeCache.TryResolveManifest(type, out manifestType));
        }
Esempio n. 15
0
 public CrateManifestTypeAttribute(object manifestId)
 {
     ManifestType = CrateManifestType.FromEnum((Enum)manifestId);
 }
Esempio n. 16
0
 /// <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;
 }
Esempio n. 17
0
 /**********************************************************************************/
 /// <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();
 }