In what conditions , if I have an “C:\andrei” folder with a single file , the output will be the name of the file ?
And where you want to be aware of this behaviour ?
Hint : The output is : \andrei\New Text Document.txt
So far , nothing special. Just open the url, read into an XML , return first nodes. Then I wanted more – next nodes. Nothing easier – why bother with recursion or proper variable names ? Just copy and paste :
Many times you will program against a table that contains something like an enum , like Status( open=1 , close=2, sent=3, approved=4 ) .
It is peculiar to wrote those status as text in the other tables – and you do not like also to have update their codes in the C# (VB.NET) code source each time you will add another one.
Rather , it is convenient to auto-generate from database at once.
But how to do it in Visual Studio ? The answer is .tt files – the files that generates also POCO
So here it is my own template for such enum from database .
To use ,unzip, add to your project that contains the edmx and do what is says below- and you will see as many .cs file as tables want to put.
<#
//*********************************************************
//
// NO Copyright .Use at your own risk.
// Please modify :
// 1) the names of tables to generate enums : string nameforenum
// 2) the connection to the database : string connectionstring
// 3) the name of the model : string inputFile
// Then save the file and you will have an enum …
//*********************************************************
#>
I have written a small post about dropdownlist template in ASP.NET MVC here : http://msprogrammer.serviciipeweb.ro/2010/05/30/mvc-helper-templates/
I think that the dropdownlist should be explained more – aand the example will be :
First, let’s say we have Employee and Department. And we have Employee that has a field, named IDDepartment.
When edit/create a user we want to display a dropdownlist for Department in order for the user to choose the department.
Step 1 : obtain from database a list of departments and transform into List<KeyValuePair<string,string>> – where the first string is DepartmentID and the second is Department Name.
Let’s say there is a method to do that : deptList.Values
Step 2 : display into the aspx/ascx file with List_KVP template
Here is the weak part: the “IDDepartment” is not strongly typed. You can transform that …but it requires writing another extension. However, when you modify the code for
Oh, and if you ask how to add a description , nothing more simple : Step1 : add to dropdown an onchange event : onchange=’javascript:funcDepartmentRetrieve(this)” Step 2: create a java script function that retrieves the long description from the id
One of recurring questions in MVC is how to share data between views and master. The question must be reformulated : how to share data between ACTION and master.
The short answer is : Model of the View returned from Action have to put some data to the Model of the Master
The long answer here in 4 steps
Step 1: ensuring error.aspx page works fine
a)copy \Views\Shared\Site.Master into siteerror.master( the error.aspx inherits from a specialized model)
There are some moments when you want to fast edit a property ( like a status or a name) and you do not want to load the entire “Edit” form for this.More, you are in an edit formula and do not want to add a form.
So here is the solution in ASP.NET MVC with jquery-1.4.2.min , jquery-ui-1.8.1.custom.min for a modal dialog and some controllers. The most difficult were the javascript, so I let to the final.
We will be editing the name for an employee in 4 steps.The most difficult to understand is the step 4(javascript) , however, it can be re-used with any other object- so you can put it in a common js file.
Step 1
create a view for editing the name , named FastEditEmp
One of the biggest challenges in programming was write once- GUI everywhere ( Ok, ORM impedance mismatch is another story)
I mean by that re-using the logic from an application in another application. ASP.NET MVC , with the commitment to strongly viewmodels, make me think that it will be now easier to transfer the viewmodels to an console application.
Let’s see the usual Employee-Department and creation.
First the Database :
Then the ViewModel for creating an employeeand for list of employees
Usually the data of the tables should be tracking for who modified it.
Think about inserting/updating/deleting an employee : you must know who did those actions and when. So you create another table, identically as structure, and you add another 3 fields , such as [ModifiedDate](when), [ModifiedBy](who), [ModifiedType] (what : insert, update, delete).
There are several methods to do it :
from database :
you can use triggers and insert data into new table
from programming code – every time you modify an object, you remember to modify the history object with appropiate data.
The drawback with the database approach is that you can not retrieve who done the modifications ( usually the applications connect under a single account and have a roles table)
The drawback with the programming approach is that the programmer must REMEMBER doing so…If he does not(and does not wrote tests for history), you are stuck…
In the following I propose an automatically history – that maps convention over configuration in my template, but it is easy for you to modify.
The solution works with Entity Framework 4.0 and, for more easily spearation of concerns , with POCO generators.
Let’s say you have the following tables :
As you see we have a Employee and a employee_history, an Department and Department_history
The conventions are:
the history table name = “object” table name + “_history” suffix
the history table fields = “object” table name fields +[ModifiedDate], [ModifiedBy], [ModifiedType]
(if you change those conventions , please change the modelhistory.tt file)
If you want to see in action , please download code history and do the following
1. create database tests
2. run history.sql
3. run project
4. if necessay, re-create the model1.edmx with the same name and replace the console application app.config with the new connection string
After works, please add any fields to department table and to department_history table(same field names/type) . Re-compile the application and modify the new field in department. You will see the modifications in the department_history table.
Ok,now how we do the magic :
We create two new tt file that points to the model.edmx .
The first one ModelHistory.tt , takes care of creating the constructor for history entities by taking a parameter from the original entity :
Ok, and then how to create the history entity ? I wish that the POCO template has had an event “Database saving” – but the only thing I can have is SaveChanges from the ObjectContext – so I create a new ObjectContext , derived from the default one that comes with the project, and creates a new history object :