Asp.NET MVC and DOS – re-using the ViewModels
(Please read first : http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/ )
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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class ViewModelEmployee { public static DepartmentList ListOfDepartments { get { //TODO : put this into cache to not load every time DepartmentList dl = new DepartmentList(); dl.Load(); return dl; } } } public class ViewModelEmployeeCreate : ViewModelEmployee { public Employee emp = new Employee(); public static void SaveNew(Employee emp) { emp.SaveNew(); } } public class ViewModelEmployeeList : ViewModelEmployee { public EmployeeList employees; public void Load() { employees = new EmployeeList(); employees.Load(); } } |
And now the magic :
ASP.NET MVC | DOS | ||||
|
|
Now for listing employees:
ASP.NET MVC | DOS | ||||
|
|
As you can see , the codes are really similar ( although the console application is filled with first verification and the MVC is not )
Please download the application from testaddropdownlist
To install : run the emp.sql file, change in the app.config/web.config the connection string to point to the real database/sql server.
Summary : This simple application shows how to re-use ViewModels from an ASP.NET MVC and a DOS Console Application
Homework : Add WindowsForms application and do the same.
link for sample code broken…
Thank you. Fixed now