wchiching
wchiching

Reputation: 23

how does your custom class relate to the database

Okay, so i've studied c# and asp.net long enough and would like to know how all these custom classes i created relate to the database. for example.

i have a class call Employee

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string EmailAddress { get; set; }
}

and i have a database with the following 4 fields:

ID
Name
EmailAddress
PhoneNumber

it seems like the custom class is my database. and in asp.net i can simple run the LINQ to SQL command on my database and get the whole schema of my class without typing out a custom class with getter and setter.

so let's just say that now i am running a query to retrieve a list of employees. I would like to know how does my application map to my Employee class to my database?

Upvotes: 2

Views: 575

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1063864

by itself, it doesn't. But add any ORM or similar, and you start to get closer. for example, LINQ-to-SQL (which I mention because it is easy to get working with Visual Studio), you typically get (given to you by the tooling) a custom "data context" class, which you use as:

using(var ctx = new MyDatabase()) {
    foreach(var emp in ctx.Employees) {
        ....
    }
}

This is generating TSQL and mapping the data to objects automatically. By default the tooling creates a separate Employee class, but you can tweak this via partial classes. This also supports inserts, data changes and deletion.

There are also tools that allow re-use of your existing domain objects; either approach can be successful - each has advantages and disadvantages.

If you only want to read data, then it is even easier; a micro-ORM such as dapper-dot-net allows you to use our type with TSQL that you write, with it handling the tedious materialisation code.

Upvotes: 1

Premraj
Premraj

Reputation: 7912

Your class cannot be directly mapped to the database without ORM tool, The ORM tool will read your configuration and will map your class to DB row as per your mappings automatically.
That means you don't need to read the row and set the class fields explicitly but you have to provide mapping files and have to go through the ORM framework to load the entities, and the framework will take care of the rest

You can check nHibernate and here is getting started on nHibernate.

Upvotes: 0

Ryan Gibbons
Ryan Gibbons

Reputation: 3601

Your question is a little vague, imo. But what you are referring to is the Model of the MVC (Model-View-Controller) architecture.

What the Model , your Employee Class, manages data of the application. So it can not only get and set (save / update) your data, but it can also be used to notify of a data change. (Usually to the view).

You mentioned you where using SQL, so more then likely you could create and save an entire employee record by sending an Associative Array of the table data to save it to the database. Your setting for the Class would handle the unique SQL syntax to INSERT the data. In larger MVC Frameworks. The Model of your application inherits several other classes to handle the proper saving to different types of backends other than MS SQL.

Models will also, normally, have functions to handle finding records and updating records. This is normally by specify a search field, and it returning the record, of which would include the ID and you would normally base this back into a save / update function to make changes to record. You could also tie into this level of the Model to create revision of the data you are saving

So how the model directly correlates to your SQL structure is dependent on how you right it. Or which Framework you decide to use. I believe a common one for asp.net is the Microsoft's ASP.Net MVC

Upvotes: 0

Related Questions