Exemple #1
0
        /// <summary>
        /// Deserialises the specified value node into the specified object
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal bool Populate(object obj, ValueNode node)
        {
            // Check the value node
            if (obj == null)
            {
                return(false);
            }
            if (node == null)
            {
                return(true);
            }

            // Check for type overrides
            Type type = obj.GetType();

            type = GetTypeOverride(type, node.TypeName);

            // Find translator
            IModelTranslator translator = FindTranslator(type, TranslateCapability.CanDeserialise);

            if (translator == null)
            {
                return(false);
            }

            // Deserialise
            translator.Deserialise(obj, node);
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Deserialises the specified value node into a certain type
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal bool Deserialise(Type type, ValueNode node, out object obj)
        {
            // Check the value node
            if (node == null)
            {
                if (type.IsValueType)
                {
                    obj = Activator.CreateInstance(type);
                }
                else
                {
                    obj = null;
                }
                return(true);
            }

            // Check for type overrides
            type = GetTypeOverride(type, node.TypeName);

            // Find default
            if (type == null)
            {
                if ((type = GetDefaultType(node)) == null)
                {
                    throw new Exception($"No default type found for {node}");
                }
            }

            // Find translator
            IModelTranslator translator = FindTranslator(type, TranslateCapability.CanDeserialise);

            if (translator == null)
            {
                if (type.IsValueType)
                {
                    obj = Activator.CreateInstance(type);
                }
                else
                {
                    obj = null;
                }
                return(false);
            }

            // Deserialise
            obj = translator.Deserialise(type, node);
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Serialises the specified object into a value node
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal bool Serialise(object obj, out ValueNode node)
        {
            if (obj == null)
            {
                node = null;
                return(true);
            }
            IModelTranslator translator = FindTranslator(obj.GetType(), TranslateCapability.CanSerialise);

            if (translator == null)
            {
                node = null;
                return(false);
            }
            node = translator.Serialise(obj);
            return(true);
        }
Exemple #4
0
 public OwnerPetService(IOwnerPetRepository ownerRepository,
                        IModelTranslator <List <PetsOwner>, List <OwnerGenderPets> > modelTranslator)
 {
     _ownerRepository = ownerRepository;
     _modelTranslator = modelTranslator;
 }
Exemple #5
0
 /// <summary>
 /// Adds the specified translator
 /// </summary>
 /// <param name="translator"></param>
 public void AddTranslator(IModelTranslator translator)
 {
     translators.Add(translator);
 }