Infragistics NetAdvantage for WPF!

CONNECT WITH US

Featured Post

Dividing ASP.NET MVC Components in Separate Projects

Get article update by Email

Whenever we create new ASP.NET MVC 3 project, it follow pre defined directory convention and adds few files and directory to the project. However it is not compulsory to follow this default directory structure, but it help us to keep application clean & maintainable by grouping application component based on functionality i.e. controller, model, view etc. But when it comes to large project, it is very hard to maintain all components in one project.

In such scenario, it is advisable to divide different components in different library projects. i.e. all model classes goes in separate library project, all controller goes in another library project and so on. Apart from model, and controller, it is also advisable to encapsulate business logic in separate library project rather than placing it in controller. By this way we can reuse business logic to expose additional services for e.g. it may happen application also provide API for third party integration. In this case we can reuse business logic to build API services because generally business logic remain same regardless of consumer (here web portal or API services).

So here we will see how we can divide model, controller, and view in separate project. Once we done with dividing MVC component in different project, it is quite easier to encapsulate business logic in separate project.

To get started, create blank solution in Visual Studio and add one library project to it. Let give it name ModelLib. We need to add reference to Entity Framework as ModelLib is responsible to deal with data source. To add reference of Entity Framework via NuGet type following in Package Manager Console in Visual Studio.

Now we can add model classes in this project. So far we have done successfully but it may require that we need to add more references to the project as and when it is needed. For e.g. reference to System.ComponentModel.DataAnnotations to use Data Annotations Attribute to validate property or field of model.

Now add one more class library project named ControllerLib and add following references to the project.

  • Entity Framework
  • ASP.NET MVC 3 assembly which is by default installed at C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
  • ModelLib project

Now we can create new controller within this project as we were creating it with default project template by inheriting class from System.Web.Mvc.Controller and use the same naming convention i.e. NameController.cs where name is replaced with controller name.

NOTE: While adding new model or controller classes, verify that its access modifier is public. Because here model and controller classes are placed in different assembly and might be in different namespace also.

So far we are ready with controller and model classes, now its turn to build views. To build view, add empty MVC 3 project template (or we can even use Phil Haack’s Really Empty MVC 3 template) and add references to the ControllerLib project. Now we are just one step away to finish! Now we can add view as we were adding it in default project template within sub directory of view directory.

To run application set MVC 3 project, which we added in last, as a startup project and run by pressing F5.

One great thing is that in this pattern also Add View dialog populate with model classes from ModelLib project as displayed in below image so we can even create strongly typed view based on model!!

Sample project can be downloaded from here.

Hope this would be helpful. You can follow me @NandipMakwana to get latest update via twitter.

Read More

Installing and Running node.js application with iisnode

Get article update by Email

Over a last few days, all are talking about asynchronous programming especially node.js and signalr. Microsoft also included AsyncCTP within ASP.NET 4.5. But it seems node.js and SignalR is winning the race. Anyway we are not going to compare AsyncCTP with node.js or SignalR. In this post we will see how to install and run node.js in IIS 7 with iisnode.

Get IIS ready to run node.Js application

To run node.js application under IIS, we need to install node.js engine on server which can be download from http://www.nodejs.org/#download. Installing node.js engine will enable us to run node.js application on windows server. But still we require configuring IIS so it can route .js requests to the node.js engine. This can be easily done with the help of iisnode module for IIS 7.x. Once we install iisnode module, we can configure web.config file to route .js or particular file to iisnode module. Iisnode module can be downloaded from https://github.com/tjanczuk/iisnode/archives/master once you install iisnode, you will able to find iisnode module in IIS Manager under modules.

Iisnode will also install sample application to get started with node.js. Sample application is copied within iisnode installation directory. If you installed iisnode with default settings, then sample can be found in C:\Program Files\iisnode\www directory. To run this sample application, create new application within default website for e.g. let say nodejsapp which points to sample application directory mentioned above. Now go to http://localhost/nodejsapp/ and you should able to see different sample link on your browser.

Now let examine HelloWorld sample. Go to http://localhost/nodejsapp/helloworld/hello.js to run server side JavaScript with the help of node.js engine.

Ohhh!! It looks there are some issue with application pool write permission. Scott Hanselman already posted issue here. There are two temporary workaround to deal with this permission issue. One is disable logging by adding following key in web.config.

Another one is create one application pool with identity as LocalSystem and assign that application pool to the nodejsapp application.

Now you should able to run node.js sample shipped with iisnode.

Read More