As we are good in developing Stored procedures having joins and using multiple tables and good hand at ADO.Net, we have not chosen EF. Now stuck in a case where we are looking a need of ORM but still trying to find simple solution:
We have one Class Message and its child class with list of object as Class URL. Now, we are looking to generate JSON like this:
{
messegid:1,
messege:’Hello’,
messegeurl:
[
{url:’http://example.com/1.jpg’},
{url:’http://example.com/2.jpg’}
]
}
We understand it would be easy to get it done using EF but what is we don't use it. We are using below method to generate objects:
public List<T> CreateObject<T>(List<MySqlParameter> parameters, string SPName)
{
List<T> t = new List<T>();
try
{
using (MySqlConnection con = new MySqlConnection(connString))
{
using (MySqlCommand cmd = new MySqlCommand(SPName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter param in parameters)
{
cmd.Parameters.Add(param);
}
con.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
T obj = default(T);
while (reader.Read())
{
obj = Activator.CreateInstance<T>();
foreach (PropertyInfo prop in obj.GetType().GetProperties())
{
if (prop.PropertyType.Equals(System.Type.GetType("System.String")) || prop.PropertyType.Equals(System.Type.GetType("System.Int32")) || prop.PropertyType.Equals(System.Type.GetType("System.Int64")) || prop.PropertyType.Equals(System.Type.GetType("System.Byte")) || prop.PropertyType.Equals(System.Type.GetType("System.DateTime")) || prop.PropertyType.Equals(System.Type.GetType("System.Boolean")))
{
if (!object.Equals(reader[prop.Name], DBNull.Value))
{
prop.SetValue(obj, reader[prop.Name], null);
}
}
}
t.Add(obj);
}
con.Close();
}
}
}
}
catch (Exception ex)
{
throw (ex);
}
return t;
}
So, I will get two list of objects one for master and another for parent and then loop through to add objects of child class to relevant parent class.
From these objects I will get json.
Now, so far I understand there is a performance issue in this approach as if I have 1000s of message and 10s of URL I will end up having bulk of loops.
Can you please suggest any way to generate direct from Mysql Stored Procedures and sort of this.