<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Programmer with Microsoft tools &#187; DOS</title>
	<atom:link href="http://msprogrammer.serviciipeweb.ro/category/howto/dos/feed/" rel="self" type="application/rss+xml" />
	<link>http://msprogrammer.serviciipeweb.ro</link>
	<description>A programmer journey through code, books and tools</description>
	<lastBuildDate>Mon, 14 May 2012 06:59:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Asp.NET MVC and DOS &#8211; re-using the ViewModels</title>
		<link>http://msprogrammer.serviciipeweb.ro/2010/07/05/asp-net-mvc-and-dos-re-using-the-viewmodels/</link>
		<comments>http://msprogrammer.serviciipeweb.ro/2010/07/05/asp-net-mvc-and-dos-re-using-the-viewmodels/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 04:17:04 +0000</pubDate>
		<dc:creator>Andrei Ignat</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[DOS]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[ViewModel]]></category>

		<guid isPermaLink="false">http://msprogrammer.serviciipeweb.ro/?p=326</guid>
		<description><![CDATA[(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 [...]]]></description>
			<content:encoded><![CDATA[<p>(Please read first : <a href="http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/" target="_blank">http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/</a> )</p>
<p>One of the biggest challenges in programming was write once- GUI everywhere ( Ok, ORM impedance mismatch is another story)</p>
<p>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.</p>
<p>Let&#8217;s see the usual Employee-Department and creation.</p>
<p>First the Database :</p>
<p><img src="http://msprogrammer.serviciipeweb.ro/wp-content/uploads/ASP.NETMVCORMandViewModels_135E/image.png" alt="" /></p>
<p>Then the ViewModel for creating an employeeand for list of employees</p>
<pre class="brush: csharp;">

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();
 }
 }
</pre>
<p>And now the magic :</p>
<table width="100%">
<tbody>
<tr>
<td>ASP.NET MVC</td>
<td>DOS</td>
</tr>
<tr>
<td valign="top">
<pre class="brush: csharp;">

[HttpPost]
 public ActionResult Create(Employee emp,long DepartmentID)
 {
 try
 {

 emp.Department = new Department() { ID = DepartmentID };
 ViewModelEmployeeCreate.SaveNew(emp);

 return RedirectToAction(&quot;Index&quot;);
 }
 catch(Exception ex)
 {
 ModelState.AddModelError(&quot;&quot;, ex.Message);
 return View(new ViewModelEmployeeCreate(){emp=emp});
 }
 }
</pre>
</td>
<td valign="top">
<pre class="brush: csharp;">

Console.WriteLine(&quot;choose department&quot;);
 var listdep=ViewModelEmployee.ListOfDepartments;
 foreach (var item in listdep)
 {
 Console.WriteLine(item.ID + &quot;)&quot; + item.Name);

 }
 string s=Console.ReadLine();
 long DepartmentID;
 if (!long.TryParse(s, out DepartmentID))
 {
 Console.WriteLine(&quot;exit : not a long :&quot; + s);
 return;
 }
 if (!listdep.Exists(item =&gt; item.ID == DepartmentID))
 {
 Console.WriteLine(&quot;not a valid id:&quot; + s);
 return;
 }
 Employee emp = new Employee();
 emp.Department = new Department() { ID = DepartmentID };
 Console.Write(&quot;employee name ?&quot;);
 emp.Name= Console.ReadLine();
 ViewModelEmployeeCreate.SaveNew(emp);
</pre>
</td>
</tr>
</tbody>
</table>
<p>Now for listing employees:</p>
<table width="100%">
<tbody>
<tr>
<td>ASP.NET MVC</td>
<td>DOS</td>
</tr>
<tr>
<td valign="top">
<pre class="brush: csharp;">
public ActionResult Index()
        {
            ViewModelEmployeeList vmel = new ViewModelEmployeeList();
            vmel.Load();
            return View(vmel);
        }
</pre>
</td>
<td valign="top">
<pre class="brush: csharp;">
  ViewModelEmployeeList vmel = new ViewModelEmployeeList();
            vmel.Load();
            foreach (var item in vmel.employees)
            {
                Console.WriteLine(item.Name);
            }
</pre>
</td>
</tr>
</tbody>
</table>
<p>As you can see , the codes are really similar ( although the console application is filled with first verification and the MVC is not )</p>
<p>Please download the application from <a href="http://msprogrammer.serviciipeweb.ro/wp-content/uploads/2010/07/testaddropdownlist.zip">testaddropdownlist</a></p>
<p>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.</p>
<p>Summary : This simple application shows how to re-use ViewModels from an ASP.NET MVC and a DOS Console Application</p>
<p>Homework : Add  WindowsForms application and do the same.</p>
]]></content:encoded>
			<wfw:commentRss>http://msprogrammer.serviciipeweb.ro/2010/07/05/asp-net-mvc-and-dos-re-using-the-viewmodels/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

