Esempio n. 1
0
        private IDisposable GivenAMap(
            MapSource source = MapSource.Class,
            MapWith target   = MapWith.DerivedA)
        {
            switch (source)
            {
            case MapSource.Class:
                switch (target)
                {
                case MapWith.DerivedA:
                    return(GivenAMapping <BaseClass, DerivedA>());

                case MapWith.DerivedB:
                    return(GivenAMapping <BaseClass, DerivedB>());
                }
                break;

            case MapSource.Interface:
                switch (target)
                {
                case MapWith.DerivedA:
                    return(GivenAMapping <INamedClass, DerivedA>());

                case MapWith.DerivedB:
                    return(GivenAMapping <INamedClass, DerivedB>());
                }
                break;
            }
            return(null);
        }
Esempio n. 2
0
        private void WhenThreadMapsToXThenThreadResolvesToX(MapSource sourceType, MapWith target)
        {
            var instance = WhenInstanceIsResolved(sourceType);

            ThenInstanceMustBeTheBaseType(instance);

            Console.WriteLine($"mapping to {target}");
            using (GivenAMap(source: sourceType, target: target))
            {
                Thread.Sleep(100);
                Console.WriteLine($"resolving for {target}");
                instance = WhenInstanceIsResolved(sourceType);
                ThenTheInstanceMustBe(instance, target);
            }
        }
Esempio n. 3
0
        private void ThenTheInstanceMustBe(INamedClass instance, MapWith expected = MapWith.DerivedA)
        {
            Assert.IsNotNull(instance);
            switch (expected)
            {
            case MapWith.DerivedA:
                Assert.IsTrue(instance.GetType() == typeof(DerivedA));
                break;

            case MapWith.DerivedB:
                Assert.IsTrue(instance.GetType() == typeof(DerivedB));
                break;

            default:
                throw new NotImplementedException();
            }
        }
        /// <summary>
        /// Map data from DataReader to an Object
        /// </summary>
        /// <typeparam name="T">Object to map</typeparam>
        /// <param name="DataRead">Datareader</param>
        /// <param name="UseElementMapper">Map maked with ElementMapper Attribute</param>
        /// <returns>List of Object with data contains in DataReader</returns>
        public static List <T> MapToList <T>(this DbDataReader DataRead, MapWith MappedWith = MapWith.Propertie) where T : new()
        {
            List <T> ListReturn = null;
            // Define object type
            var Entity = typeof(T);
            // Define dictionary
            var PropDict = new Dictionary <string, PropertyInfo>();
            var AttrDict = new Dictionary <string, PropertyInfo>();

            try
            {
                if (DataRead != null && DataRead.HasRows)
                {
                    ListReturn = new List <T>();
                    // Get each properties of the Object
                    var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                    // Get each attribute of properites
                    foreach (PropertyInfo PropInfo in Props)
                    {
                        object[] AttrCollection = PropInfo.GetCustomAttributes(true);

                        foreach (object Attr in AttrCollection)
                        {
                            if (Attr is ElementMapper)
                            {
                                // Get the ElementMapper name
                                ElementMapper Element = Attr as ElementMapper;
                                if (Element != null)
                                {
                                    AttrDict.Add(Element.ElementName.ToUpper(), PropInfo);
                                }
                            }
                        }
                    }

                    while (DataRead.Read())
                    {
                        T newObject = new T();
                        for (int Index = 0; Index < DataRead.FieldCount; Index++)
                        {
                            if ((MappedWith == MapWith.Propertie) || (MappedWith == MapWith.All))
                            {
                                // Search reader name in Dictionary properties
                                if (PropDict.ContainsKey(DataRead.GetName(Index).ToUpper()))
                                {
                                    var Info = PropDict[DataRead.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        // Set value in Object
                                        var Val = DataRead.GetValue(Index);

                                        try
                                        {
                                            if (Info.PropertyType == Val.GetType())
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                            }
                                            else if (Info.PropertyType == typeof(string))
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val.ToString(), null);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            throw;
                                        }
                                    }
                                }
                            }

                            if ((MappedWith == MapWith.Attribute) || (MappedWith == MapWith.All))
                            {
                                // Search reader name in Dictionary attributes
                                if (AttrDict.ContainsKey(DataRead.GetName(Index).ToUpper()))
                                {
                                    var Info = AttrDict[DataRead.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        // Set value in Object
                                        var Val = DataRead.GetValue(Index);

                                        try
                                        {
                                            if (Info.PropertyType == Val.GetType())
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                            }
                                            else if (Info.PropertyType == typeof(string))
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val.ToString(), null);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            throw;
                                        }
                                    }
                                }
                            }
                        }
                        // Add current object in the list
                        ListReturn.Add(newObject);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DataReaderExtension : " + ex.Message);
                throw;
            }

            return(ListReturn);
        }
        /// <summary>
        /// Map data from List Dictionnary to an Object
        /// </summary>
        /// <typeparam name="T">Object to map</typeparam>
        /// <param name="ListDict">List Dictionary</param>
        /// <param name="UseElementMapper">Map maked with ElementMapper Attribute</param>
        /// <returns>List of Object with data contains in List Dictionary</returns>
        public static List <T> MapToList <T>(this List <Dictionary <string, string> > ListDict, MapWith MappedWith = MapWith.Propertie) where T : new()
        {
            List <T> ListReturn = null;
            // Define object type
            var Entity = typeof(T);
            // Define dictionary
            var PropDict = new Dictionary <string, PropertyInfo>();
            var AttrDict = new Dictionary <string, PropertyInfo>();

            try
            {
                if (ListDict != null && ListDict.Count > 0)
                {
                    ListReturn = new List <T>();
                    // Get each properties of the Object
                    var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                    // Get each attribute of properites
                    foreach (PropertyInfo PropInfo in Props)
                    {
                        object[] AttrCollection = PropInfo.GetCustomAttributes(true);

                        foreach (object Attr in AttrCollection)
                        {
                            if (Attr is ElementMapper)
                            {
                                // Get the ElementMapper name
                                ElementMapper Element = Attr as ElementMapper;
                                if (Element != null)
                                {
                                    AttrDict.Add(Element.ElementName.ToUpper(), PropInfo);
                                }
                            }
                        }
                    }

                    foreach (Dictionary <string, string> Dict in ListDict)
                    {
                        T newObject = new T();

                        for (int Index = 0; Index < Dict.Count; Index++)
                        {
                            if ((MappedWith == MapWith.Propertie) || (MappedWith == MapWith.All))
                            {
                                if (PropDict.ContainsKey(Dict.ElementAt(Index).Key.ToUpper()))
                                {
                                    var Info = PropDict[Dict.ElementAt(Index).Key.ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = Dict.ElementAt(Index).Value;
                                        if (Info.PropertyType == Val.GetType())
                                        {
                                            Info.SetValue(newObject, Val);
                                        }
                                        else if (Info.PropertyType == typeof(string))
                                        {
                                            Info.SetValue(newObject, Val.ToString());
                                        }
                                    }
                                }
                            }

                            if ((MappedWith == MapWith.Attribute) || (MappedWith == MapWith.All))
                            {
                                if (AttrDict.ContainsKey(Dict.ElementAt(Index).Key.ToUpper()))
                                {
                                    var Info = AttrDict[Dict.ElementAt(Index).Key.ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = Dict.ElementAt(Index).Value;
                                        if (Info.PropertyType == Val.GetType())
                                        {
                                            Info.SetValue(newObject, Val);
                                        }
                                        else if (Info.PropertyType == typeof(string))
                                        {
                                            Info.SetValue(newObject, Val.ToString());
                                        }
                                    }
                                }
                            }
                        }

                        ListReturn.Add(newObject);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DataReaderExtension : " + ex.Message);
                throw;
            }

            return(ListReturn);
        }