locked
Convert an Entity Framework class to a Generic Class RRS feed

  • Question

  • User283528319 posted

    Hi all,

    I am using the code below to get data from <g class="gr_ gr_51 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="51" data-gr-id="51">mysql</g> via entity framework. It is written for 1 table but I want to convert it to a generic one to use it for all tables. Could you help me <g class="gr_ gr_207 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="207" data-gr-id="207"><g class="gr_ gr_206 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="206" data-gr-id="206">please</g>.</g>

         public List<Gonderenler> GetAllGonderens()
            {
                List<Gonderenler> list = new List<Gonderenler>();
    
                using (MySqlConnection conn = GetConnection())
                {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand("select * from Gonderenler where gonderenler_id < 1000", conn);
    
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            list.Add(new Gonderenler()
                            {
                                GonderenlerID = Convert.ToInt32(reader["gonderenler_id"]),
                                GonderenMakam = reader["GonderenMakam"].ToString()
                            });
                        }
                    }
                }
                return list;
            }

    Sunday, January 20, 2019 6:59 AM

All replies

  • User1120430333 posted

    I am using the code below to get data from mysql via entity framework. It is written for 1 table but I want to convert it to a generic one to use it for all tables. Could you help me please.

    There is no such thing as a generic class. The best you can do is make a dynamic object.

    https://www.oreilly.com/learning/building-c-objects-dynamically

    http://lvasquez.github.io/2013/08/15/return-dynamic-result-from-database-asp-mvc/

    Sunday, January 20, 2019 9:59 AM
  • User283528319 posted

    I am using the code below to get data from <g class="gr_ gr_16 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="16" data-gr-id="16">mysql</g> via entity framework. It is written for 1 table but I want to convert it to a generic one to use it for all tables. Could you help me <g class="gr_ gr_18 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="18" data-gr-id="18"><g class="gr_ gr_17 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="17" data-gr-id="17">please</g>.</g>

    There is no such thing as a generic class. The best you can do is make a dynamic object.

    What about this?

    https://docs.microsoft.com/tr-tr/dotnet/csharp/programming-guide/generics/generic-classes

    Sunday, January 20, 2019 10:50 AM
  • User-2054057000 posted

    Suppose you have a Generic class called MyGenericClass:

    public class MyGenericClass{
    
    public string Name{get;set;};
    public string Description{get;set;};
    
    }

    Then you can fill this Generic Class values from EF Core like this:

    List<MyGenericClass> mgc = new List<MyGenericClass>();
                 
    mgc = context.Blog.Where(x => x.Status == true)
    .Select(x => new MyGenericClass{ Name = x.Name, Description = x.Description }).ToList();

    Blog is filled by Entity Framework from Database.

    Sunday, January 20, 2019 11:37 AM
  • User1120430333 posted

    DA924

    I am using the code below to get data from mysql via entity framework. It is written for 1 table but I want to convert it to a generic one to use it for all tables. Could you help me please.

    There is no such thing as a generic class. The best you can do is make a dynamic object.

    What about this?

    https://docs.microsoft.com/tr-tr/dotnet/csharp/programming-guide/generics/generic-classes

    I don't know what that is since it is not English.

    Sunday, January 20, 2019 12:15 PM
  • User1120430333 posted

    yogyogi

    Suppose you have a Generic class called MyGenericClass:

    public class MyGenericClass{
    
    public string Name{get;set;};
    public string Description{get;set;};
    
    }

    Then you can fill this Generic Class values from EF Core like this:

    List<MyGenericClass> mgc = new List<MyGenericClass>();
                 
    mgc = context.Blog.Where(x => x.Status == true)
    .Select(x => new MyGenericClass{ Name = x.Name, Description = x.Description }).ToList();

    Blog is filled by Entity Framework from Database.

    yogyogi

    Suppose you have a Generic class called MyGenericClass:

    public class MyGenericClass{
    
    public string Name{get;set;};
    public string Description{get;set;};
    
    }

    Then you can fill this Generic Class values from EF Core like this:

    List<MyGenericClass> mgc = new List<MyGenericClass>();
                 
    mgc = context.Blog.Where(x => x.Status == true)
    .Select(x => new MyGenericClass{ Name = x.Name, Description = x.Description }).ToList();

    Blog is filled by Entity Framework from Database.

    . MyGenericClass is class, a type,  and all you are doing is a Linq projection  of a known type into a generic List<T> making a strong typed collection. 

    Sunday, January 20, 2019 12:23 PM
  • User283528319 posted

    Suppose you have a Generic class called MyGenericClass:

    public class MyGenericClass{
    
    public string Name{get;set;};
    public string Description{get;set;};
    
    }

    Then you can fill this Generic Class values from EF Core like this:

    List<MyGenericClass> mgc = new List<MyGenericClass>();
                 
    mgc = context.Blog.Where(x => x.Status == true)
    .Select(x => new MyGenericClass{ Name = x.Name, Description = x.Description }).ToList();

    <g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="15" data-gr-id="15">Blog</g> is filled by Entity Framework from Database.

    <g class="gr_ gr_24 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="24" data-gr-id="24">may be</g> I asked wrong, this is not the way I wanted to implement

    I need to use variables in here

    List<VariableClass> <g class="gr_ gr_253 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="253" data-gr-id="253">mgc</g> = new List<VariableClass>();

    and want to change that var class with the class I want

    Sunday, January 20, 2019 1:15 PM
  • User753101303 posted

    Hi,

    Yes, seems https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-classes is what you are looking for.

    The purpose is to write code with a minimal amount of knowledge about a class and you can then reuse the same code with other classes.

    List<VariableClass> mgc = new List<VariableClass>(); is precisely using the actual class you want. In the the declaration you are using a <T> parameter which represents the class you want to use.

    Have you tried something? It could be easier if knowing the path you have taken and which problem you have. This is basically what https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbset-1?view=entity-framework-6.2.0 does

    Edit: or you mean you want "VariableClass" to be stored in  a variable ? Whenever you try unusual stuff it's best IMO to start by explaining which problem you are trying to solve.

    Sunday, January 20, 2019 1:27 PM
  • User1120430333 posted

    yogyogi

    Suppose you have a Generic class called MyGenericClass:

    public class MyGenericClass{
    
    public string Name{get;set;};
    public string Description{get;set;};
    
    }

    Then you can fill this Generic Class values from EF Core like this:

    List<MyGenericClass> mgc = new List<MyGenericClass>();
                 
    mgc = context.Blog.Where(x => x.Status == true)
    .Select(x => new MyGenericClass{ Name = x.Name, Description = x.Description }).ToList();

    Blog is filled by Entity Framework from Database.

    may be I asked wrong, this is not the way I wanted to implement

    I need to use variables in here

    List<VariableClass> mgc = new List<VariableClass>();

    and want to change that var class with the class I want

    You can't do it. And if you ever tried to present such code for a professional review before it went into a production status, you would be stopped and told to come up with something that works and is clean. The whole thing here of some common generic class IMO is total nonsense being used in some data mapping .situation.

    Sunday, January 20, 2019 9:24 PM
  • User283528319 posted

    DA924

    yogyogi

    I am just trying to say. It is obvious that C. D operations (not read and update) can be done with only one method.

    and you can change the parameters of the code (entity framework model name and id) and use the same method for all insert /create and delete method for all of your tables.

    Like Delete(Table Name (model name),ID)

    Monday, January 21, 2019 9:49 AM
  • User753101303 posted

    It's unclear if you want to use generics or even pass the actual type as a string (you would use a single controller with a route parameter ?).

    Do you have generics up and working for now ?

    Monday, January 21, 2019 9:55 AM
  • User1120430333 posted

    DA924

    yogyogi

    I am just trying to say. It is obvious that C. D operations (not read and update) can be done with only one method.

    and you can change the parameters of the code (entity framework model name and id) and use the same method for all insert /create and delete method for all of your tables.

    Like Delete(Table Name (model name),ID)

    No, you don't start playing games with the ORM's persistence model, unless you are using a proven inheritance strategy .

    http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

    Monday, January 21, 2019 11:57 AM
  • User283528319 posted

    fatihbarut

    DA924

    yogyogi

    I am just trying to say. It is obvious that C. D operations (not read and update) can be done with only one method.

    and you can change the parameters of the code (entity framework model name and id) and use the same method for all insert /create and delete method for all of your tables.

    Like Delete(Table Name (model name),ID)

    No, you don't start playing games with the ORM's persistence model, unless you are using a proven inheritance <g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="21" data-gr-id="21">strategy .</g>

    http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx

    So you say if I have 40 tables I have to create 160 views for CRUD? am I wrong?

    Monday, January 21, 2019 12:28 PM
  • User1120430333 posted

    So you say if I have 40 tables I have to create 160 views for CRUD? am I wrong?

    40 tables means 40 ORM model objects for persistence. I don't know where 160 views come into play anywhere. And if you are using EF DB first or code first for an existing database, then you don't have to make anything,  because EF, an ORM,  will make the ORM virtual object model the persistence model.

    Don't confuse the ORM's persistence model with the domain model or a MVC model that is used in the view. 

    https://blog.sapiensworks.com/post/2012/04/07/Just-Stop-It!-The-Domain-Model-Is-Not-The-Persistence-Model.aspx

    Learn how to use a viewmodel in ASP.NET MVC.

    https://www.tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example

    Monday, January 21, 2019 4:55 PM
  • User1520731567 posted

    Hi fatihbarut,

    So you say if I have 40 tables I have to create 160 views for CRUD? am I wrong?

    If so, you've lost the loose coupling benefits of interfaces.

    In general, the underlying functions, functions as single as possible, try to avoid modifying the underlying functions.

    Don't design too much function for a interface in order to reduce the amount of code.

    Best Regards.

    Yuki Tao

    Tuesday, January 22, 2019 8:16 AM