コード例 #1
0
        /**********************************************************************************/
        /// <summary>
        /// Convert DTO to Crate instance
        /// </summary>
        /// <param name="proxy"></param>
        /// <returns></returns>
        public Crate ConvertFromDto(CrateDTO proxy)
        {
            if (proxy == null)
            {
                return(null);
            }

            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;
            return(crate);
        }
コード例 #2
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);
     }
 }
コード例 #3
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
     });
 }
コード例 #4
0
        /**********************************************************************************/

        internal static Crate FromJson(CrateManifestType manifestType, string id, JToken content)
        {
            return(new Crate(manifestType, id)
            {
                _rawContent = content
            });
        }
コード例 #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));
        }
コード例 #6
0
        /**********************************************************************************/

        public bool TryResolveType(CrateManifestType manifestType, out Type type)
        {
            lock (_typeMapping)
            {
                return(_typeMapping.TryGetValue(manifestType, out type));
            }
        }
コード例 #7
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);
            }
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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);
            }
        }
コード例 #11
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();
 }
コード例 #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;
 }
コード例 #13
0
        /**********************************************************************************/

        public bool TryGetManifestType(Type type, out CrateManifestType manifestType)
        {
            return(ManifestTypeCache.TryResolveManifest(type, out manifestType));
        }
コード例 #14
0
 public bool TryResolveManifestType(string manifestTypeName, out CrateManifestType manifestType)
 {
     return(ManifestTypeCache.TryResolveManifest(manifestTypeName, out manifestType));
 }
コード例 #15
0
 public CrateManifestTypeAttribute(int manifestId, string manifestTypeName)
 {
     ManifestType = new CrateManifestType(manifestTypeName, manifestId);
 }
コード例 #16
0
 public CrateManifestTypeAttribute(object manifestId)
 {
     ManifestType = CrateManifestType.FromEnum((Enum)manifestId);
 }