Tuesday, July 24, 2012

MVC vs MVP vc MVVM

During one of the discussion internally with practice team members, we talked about few differences between Model View Controller (MVC), Model View Presenter (MVP) and Model View ViewModel (MVVM) patterns. Breifly looked at the web and I could consolidate the final understandings as below :

MVP

- Presenter contains the business logic for View
- Presenter binds data to model
- Presenter is exposed through interface
- View is called directly
- View does not bind directly to the model it mediates through presenter
- This is achievable in ASP.Net web applications

MVC

- Controller determines which view to display
- View is bound to a model
- View is stateless and no code behind required
- This is the pattern used in ASP.Net MVC

MVVM

- View Model is also called presentation model which acts as intermediate for View to talk to.
- View Model will talk to the underlying domain model (like Entity Framework)
- Mostly used in Silverlight and WPF where the control binding to data happens through the presentation model and does not need to have properties in the view and have UI business logic to it.


There are hell lot of blogs and articles about these to help you more but what I thought is to put a simple note about all.

Any change in your view about this blog, please present it to me :).

References :
http://stackoverflow.com/questions/2056/what-are-mvp-and-mvc-and-what-is-the-difference

All in one code framework plugin is introduced

This one from codeplex would be really useful for us‘dayin-dayout’ and it really avoids copying from google. As of now they have just started building the samples and due course of time we may get code sample with project for everything we need.
 
Just visit the http://1code.codeplex.comand download the browser plugin from http://visualstudiogallery.msdn.microsoft.com/4934b087-e6cc-44dd-b992-a71f00a2a6dfto use in visualstudio ( supports till RC 2012) or can be viewed separately in the browser.
 
Once plugin installed, just go to the TOOLS and click on‘Search Code Sample’ and search for the code you need. During coding if you started typing a code for a mail component and you need to see if this code repository can help you find one for you, then just right click on the method name or class name by which the sample will be searched.
 
Try it out….. For sure it’s a useful plugin for all the devs.
 
Note : Coding guideline used for building these samples are also exposed for us to use / review. 
Enjoy… reusing the code.

"403 : Access denied" in windows azure application

While creating an application to deploy into Windows Azure (Cloud Services), I encountered a weird issue that could not be solved by anyone (read a lot of blog).

The issue is "403 : Access denied" was getting shown when I hit the production url (in my case : questionaire.cloudapp.net). I could not find anyreason why it should throw an error as I have just followed the steps in windows.azure.com tutorials and scott's blog. Really there is nothing much to do to get an app to be deployed into cloud.

Finally I found the issue to be the OS type that was allocated to me in cloud services host vm. It was having Windows 2008 SP1 and when I changed it to Windows 2008 R2, it worked.
Hope others do not need to waste their time looking at it as firewall issue, ACL issue, code isssue etc :)

First app in Cloud....

I have been digging through Windows Azure for few days and finally I could get a sample Questionaire application deployed to the cloudapp.net (windows azure - cloud services).


Also took a couple of sessions on Windows azure to the internal dotnet team.

Wednesday, April 29, 2009

CODE ANALYSIS USING VS 2008

I was going through CLR Profiler to use as a first step to check memory utilization on every application to make it as a process while delivering projects to clients. Though, there are better code profiler which can let us know more than what CLR Profiler can, I have taken CLR Profiler as it is Microsoft’s and its free. CLR Profiler does profling to know the memory consumption of your application like freed memory, reused memory, and allocated memory of the whole application. Having said, it includes all the .Net object’s memory consumption too, which is too much to have a look at. There is no way you can omit the .Net namespaces and have only the specific namespaces to be proflied. [Note : We can write our own code as the source code for the CLR profiler is available along with the application download. ]

While I was struggling to use it in Vista, I happen to see a new feature called Code Analysis in VS 2008. I was curious to know more about it and visited the Somesegar’s blog (http://blogs.msdn.com/somasegar/archive/2007/10/04/code-analysis-features-in-vs-2008.aspx).

Using Code Analysis feature in VS 2008, we can analyze the code, get the code metrics and also profiler the application to view the memory consumption. Let us see visit each of them.

Code Analysis

There is nothing new about this for people who have used FxCop or StyleCop already. In Visual Studio 2008 Developer Edition and Team Edition, we have a new menu item ‘Analyze’ which will help us in configuring, running and setting the Code analysis to run while code check-in.

Code analysis allows us to check our code against the Design rules, Usage rules, Naming rules etc which are designed to meet the Microsoft code standards and recommendation. We can enable this feature by doing a right click -> Properties -> Code Analysis -> selecting the checkbox ‘Enable Code Analysis on Build’ and choose the specific rules if needed. In case of website, we can choose the menu ‘Website’ in the VS IDE and choose to ‘Code Analysis Configuration’ or choose Analyze -> Code Analysis Configuration. Simillarly, use Website menu, Analyze menu or use the context menu in the solution explorer to Run the Code Analysis on Web site. For libraries, we get to see ‘Run the Code Analysis’ on the context menu (right click on class library in solution explorer).

Code Metrics

This feature in VS 2008 helps us in checking the Code Metrics of our projects / solutions. Please note that this feature will not give us the Code Metrics for website created in C#, Visual Basic projects. But it works fine for libraries.

To do this, choose the ‘Calculate Code Metrics’ from the context menu in the solution explorer by right-clicking on the Solution. The same can be done using the Analyze menu item in the VS 2008 Developer edition / Team edition.

This feature currently supports five different metrics; Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Coupling of Class and Lines of Code. The explanation for each of these are available in the blogs http://blogs.msdn.com/fxcop/archive/2007/02/28/announcing-visual-studio-code-metrics.aspx and http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx

Code Profiling

This reduces the burden of using CLR Profiler to know the memory consumption of the whole application which throws up the heap memory usage of even the .Net objects. The Code profiling in VS 2008 lets us know the Types with Most Instances created, Types with Most Memory allocated, and Function allocating most memory.

To do a code profiling, choose ‘Launch Performance Wizard’ from the ‘Analyze’ menu. Follow the wizard to choose the target application to profile and the profile type (Sampling or Instrumentation). Once the Wizard is finished, we will get to see a Performance Explorer window having the performance session created with Target and Report as the sub items under the session tree.

Choose the properties of the session and set the desired properties to profile for Performance counters. We can now launch the profile and get a performance report. We can click on any of the rows to drill down further to check the function calls (stack trace) with memory consumption. The data view once we click on the row will open up to show memory consumption by Functions, Objects, Call Tree, Caller / Callee, Object Life time etc.

References :

Code Profiler

http://msdn.microsoft.com/en-us/library/ms182372(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms182372.aspx

http://blog.maartenballiauw.be/post/2008/02/Code-performance-analysis-in-Visual-Studio-2008.aspx

Code Metrics

http://blogs.msdn.com/fxcop/archive/2007/02/28/announcing-visual-studio-code-metrics.aspx

http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx

VS 2008 Edition Comparison

http://www.microsoft.com/visualstudio/en-us/products/teamsystem/default.mspx#compare_products)

Wednesday, March 25, 2009

Why no "read only" "write only" automatic properties

We all know how automatic properties feature of .Net 3.5 left us improving our development time. Although we had the snippet facility to create properties earlier, it is too boring to keep creating private variable and a public property using each variable. Automatic property helps us in those situation. But there is a constraint that we cannot create automatic property as"read only" or "write only" property. Let's see why?

Automatic properties are defined as follows.


public int Width

{
get;
set;

}


It might look simillar to declaring a property in the abstract class in .Net 2.0. The difference between them is just the abstract keyword. Moving on, automatic properties create a private variable internally. It may look like

private int width

public int Width

{
get
{
return width;
}
set
{
width = value;
}
}

this look similar to .Net 2.0 way of creating properties.


Automatic property creates a private variable by itself for its use. What it exposes is only the property which is fair enough for us to work on. But again, why no "readonly" or "writeonly"?
If we create a property for readonly purpose, we have to do it as follows

public int Width
{
get;
}

In this case, we dont know the private variable created by the .Net for us as it is created at runtime. So, we cannot set the value of the .Net created variable through our program so that it returns the value when the Width property is referred.


So, the only way is to create a variable by ourselves and use it in our program to return it. That's why readonly and writeonly properties cannot be created automatic. We should create it explicitly as we do it in .Net 2.0 or earlier.

Few insights on partial methods

I am reading the Andrew Troelsen's book "Pro C# 2008 and the .Net 3.5 platform" bit un-regularly. I am finding this book bit interesting as it covers the chapters in a naive way. I came across two new features (Partial Methods and Automatic properties) in .Net 3.5 which are not complex to understand. But there are some constraints in using these features which Andrew left us to think through by ourselves. I thought through them and compiled my understanding as below. This blog will have about the Partial methods and will write on Automatic properties in a day.

Partial Methods - allows only void return type

Partial methods allow us to write the method implementation in a different class (partial class) by keeping just the method definition in a partial class. This feature though has few constraints which you will get to know by reading the above book. One of the constraint is that partial methods allow only void return type. Why is that?

Possibly, because until the partial method is implemented, your disassembled code will not show up the partial method definition. To explain further, please follow the example.

For ex :
namespace MyDotNet35
{
public partial class TestHarness
{
public bool myTestMethod()
{
.....do something ....
myPartialMethod() ; //call the partial method
....do something ....
}

partial void myPartialMethod();
}
}

If you look at the compiled IL of the above class, you will not get to see the myPartialMethod() call inside the myTestMethod until it is implemented in another partial class as below.

namespace MyDotNet35
{
public partial class TestHarness
{
partial void myPartialMethod()
{
//actual implementation of the partial method is here.
}
}
}

If my partial method returns an integer, then its possible for me to write the code above as


namespace MyDotNet35
{
public partial class TestHarness
{
public bool myTestMethod()
{
.....do something ....
int retVal = myPartialMethod() ; //call the partial method
if (retVal == someValue){
....do something ....
}
}

partial void myPartialMethod();
}
}

Imagine what would be the compiled version of IL....?

Yes, the compiler cannot leave the myPartialMethod call in this case (though the implementation is not done yet) as it is required to store the output to a variable and also some validation takes place based on the return value of the partial method.

Now, that's why the partial methods should have only void return type....got it.