Beispiel #1
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);
        }
        /**********************************************************************************/

        public static Crate <T> Add <T>(this ICrateStorage storage, string label, T content)
        {
            var crate = Crate.FromContent(label, content);

            storage.Add(crate);

            return(crate);
        }
        /**********************************************************************************/
        /// <summary>
        /// Replaces all crates that have label mathching to passed predicate with passed crate
        /// </summary>
        /// <returns></returns>
        public int Replace(Predicate <Crate> predicate, Crate crate)
        {
            int affected_items = 0;

            foreach (var key in _crates.Keys.ToArray())
            {
                if (predicate(_crates[key]))
                {
                    _crates.Remove(key);
                    affected_items++;
                }
            }

            Add(crate);
            return(affected_items);
        }
Beispiel #4
0
        /**********************************************************************************/
        /// <summary>
        /// Convert crate to DTO
        /// </summary>
        /// <param name="crate"></param>
        /// <returns></returns>
        public CrateDTO ConvertToDto(Crate crate)
        {
            IManifestSerializer serializer = GetSerializer(crate.ManifestType);
            CrateDTO            crateDto   = new CrateDTO
            {
                Id               = crate.Id,
                Label            = crate.Label,
                ManifestId       = crate.ManifestType.Id,
                ManifestType     = crate.ManifestType.Type,
                Availability     = crate.Availability,
                SourceActivityId = crate.SourceActivityId
            };

            if (serializer != null)
            {
                crateDto.Contents = serializer.Serialize(crate.Get <object>());
            }
            else
            {
                crateDto.Contents = crate.GetRaw();
            }

            return(crateDto);
        }
 /**********************************************************************************/
 /// <summary>
 /// Add new crate to storage
 /// </summary>
 /// <param name="crate"></param>
 public void Add(Crate crate)
 {
     _crates.Add(crate.Id, crate);
 }
Beispiel #6
0
 /**********************************************************************************/
 /// <summary>
 /// Create new create from content. Manifest type is deduced from the content. This method guaranties than manifest type can be correctly deduced
 /// </summary>
 /// <param name="label"></param>
 /// <param name="content"></param>
 /// <returns></returns>
 public static Crate <T> FromContent <T>(string label, T content)
 {
     return(Crate <T> .FromContent(label, content));
 }
Beispiel #7
0
 public Crate(Crate crate)
     : base(crate.ManifestType, crate.Id, crate.Availability)
 {
     Label        = crate.Label;
     KnownContent = crate.Get <T>();
 }
 /**********************************************************************************/
 /// <summary>
 /// Removes the crate. Crate is removed by Id.
 /// </summary>
 /// <param name="crate"></param>
 /// <returns></returns>
 public static bool Remove(this ICrateStorage storage, Crate crate)
 {
     return(storage.Remove(x => x.Id == crate.Id) != 0);
 }
        /**********************************************************************************/
        /// <summary>
        /// Replaces all crates that have label mathching to passed crate label with passed crate
        /// </summary>
        /// <returns></returns>
        public static int ReplaceByLabel(this ICrateStorage storage, Crate crate)
        {
            var predicate = new Predicate <Crate>(x => x.Label == crate.Label);

            return(storage.Replace(predicate, crate));
        }