0

Right now Im working in a big project, So theres a lot of data and tables going around.

For best practices Im creating a class for every table and object

Its goes like this:

public class Employee
{
    private String Name;
    public String Name
    {
        get
        {
            return Name;
        }
        set
        {
            Name = value;
        }
    }

    public Employee(int EmployeeID) 
    {
        /*
         GET DATA ROW AND ASSIGN IT TO EVERY PROPERTIE
         * 
         * Name = row("name")
         * AND DO THIS FOR EVERY PROPERTIE!
         * 
         */
    }
}

So whats happening here is that I have to assign every propertie from a query in the class constructor.

But imagine a table with like 50+ columns, I have to do this 50+ times and this takes a lot of time.

Theres a way to automate this automate the creation of the 50+ properties and the asignation of the 50+ properties in the class withouth taking a lot of time.

I just wanna find a way to create a class automating the properties assignation from a datarow instead of writting all the columns string to the properties. Something like Entityt Framework but done by me.

Greeting and thanks

Waldi
  • 39,242
  • 6
  • 30
  • 78
NathanWay
  • 139
  • 1
  • 8
  • That code wouldn't even compile, as you're declaring two members with the same name. (It looks like you should just use automatic properties.) Do you have *evidence* that setting these properties is actually a bottleneck? Assuming these are coming from a database, the database access is going to *vastly* outweigh the time taken for trivial assignments. – Jon Skeet Sep 06 '15 at 08:31
  • I wrote the snippet fast hehe. I just wanna find a way to create a class automating the properties assignation from a datarow instead of writting all the columns string to the properties. Something like Entityt Framework but done by me... – NathanWay Sep 06 '15 at 08:38
  • So are you saying the problem isn't execution-time performance, but the annoyance of writing the code? If so, please clarify your post - it gives the impression of being about performance at the moment. – Jon Skeet Sep 06 '15 at 08:39
  • Thanks Jon :) I will.. – NathanWay Sep 06 '15 at 08:42

2 Answers2

0

There are heaps of examples online to make C# classes from dB tables & stored procedures, research that and POCO's, eg:

Generate class from database table

http://www.codeproject.com/Articles/8397/C-Code-Generator-for-Stored-Procedures

You're not the first to encounter this, best to do a quick google next time.

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

you should use better entity framework code first approach, it works even if your database is already created.

use a tool to generate the class model, and create a context like:

  public class MyContext : DbContext
  {
      public MyContext (){}
       public DbSet<MyModel> MyModel { get; set; }
  }
Ariel Batista
  • 144
  • 1
  • 5