ANDROID Operating System

Android is a mobile operating system initially developed by Android Inc. Android was bought by Google in 2005. Android is based upon a modified version of the Linux kernel...

What Is WEB 2.0 ?

Web 2.0 is associated with web applications that facilitate participatory information sharing, interoperability, user-centered design,and collaboration on the World Wide Web.

Social Network Services

A social networking service is an online service, platform, or site that focuses on building and reflecting of social networks or social relations among people, who, for example, share interests and/or activities.

Latest Trends N Upcoming Trends

Information technology (IT) is the acquisition, processing, storage and dissemination of vocal, pictorial, textual and numerical information by a microelectronics-based combination of computing and telecommunications...

Tips N Tricks (Various OS)

This Is crazy!! Set Processes Priority Follow this tip to increase the priority of active processes, this will result in prioritisation of processes using the CPU.CTRL-SHIFT-ESC...

Monday, July 18, 2011

Use-Viewmodels-to-Manage-Data-AMP-Organize-Code-in-asp.net-MVC-Applications


The concept of the ViewModel isn't just for ASP.NET MVC, as you'll see references to ViewModels throughout the web in articles and blog posts about the MVC, MVP, and MVVM patterns. Those posts and articles can center around any number of technologies such as ASP.NET, Silverlight, WPF, or MVC... This post will investigate ViewModels as they apply to the world of ASP.NET MVC.

What is an ASP.NET MVC ViewModel?

In ASP.NET MVC, ViewModels allow you to shape multiple entities from one or more data models or sources into a single object, optimized for consumption and rendering by the view. The below image illustrates the concept of a ViewModel:
image
The purpose of a ViewModel is for the view to have a single object to render, alleviating the need for UI logic code in the view that would otherwise be necessary. This means the only responsibility, or concern, of the view is to render that single ViewModel object, aiding in a cleaner separation of concerns (SoC). Concerns are distinct aspects of the application that have a particular purpose (i.e., concern), and keeping these aspects apart means your application is more organized, and the code more focused. Putting data manipulation code in its own location away from the view and controller, enforces SoC.
Using ViewModels in MVC for finer granularity and better SoC leads to more easily maintainable and testable code. Remember, unit testing is about testing small units.
Along with better coding practices, there are many business reasons demonstrating why you might consider using ViewModels:
  • Incorporating dropdown lists of lookup data into a related entity
  • Master-detail records view
  • Pagination: combining actual data and paging information
  • Components like a shopping cart or user profile widget
  • Dashboards, with multiple sources of disparate data
  • Reports, often with aggregate data
The above scenarios are common to a wide variety of applications, and deal with more complex data than basic CRUD forms-over-data page (e.g., a simple 1:1 mapping to the db table). For example, providing a list of states, and ensuring that the state that matches the state of current customer, means that you need to either provide two sets of data or a single set of customer/state data combined, as shown in the image below.
image
Some scenarios such as a lookup table representing states in the USA, could easily work with either ViewModels or a ViewBag/ViewData object, so there is some potential overlap at times. It's up to the application architects and developers to decide what works best with their exact use case.

Creating a ViewModel

Although a ViewModel consists of multiple entities, at its core a ViewModel is still just a class - and one that doesn't even inherit from anything special, as many MVC classes do. 
Physically, ViewModels can exist in different locations, listed below:
  • In a folder called ViewModels that resides in the root of the project. (small apps)
  • As a .dll referenced from the MVC project (any size app)
  • In a separate project(s) as a service layer, for large applications that generate view/content specific data. (enterprise apps)
Since a ViewModel is just a class, the easiest way to get started using one is to create a new folder named ViewModels and add a new code file to it.
To create the CustomerViewModel ViewModel, add the Customer and StatesDictionary types as properties to form one CustomerViewModel class. In the example below, the CustomerViewModel class contains the newly defined properties.
public class CustomerViewModel 
{
    public Customer Customer { get; set; }
    public StatesDictionary States { get; set; }
    public CustomerViewModel(Customer customer)
    {
        Customer = customer;
        States = new StatesDictionary();
    }
}
Generally, ViewModels contain the word "ViewModel" as part of its name; however, the conventions at work here are for consistency in code readability, since other classes in MVC state their intents in their names as well (e.g., names of controllers, action methods, etc...use conventions in their names).
The StatesDictionary class is a simple Dictionary object containing two type parameters of type string. The class also contains the definitions for all the members in the Dictionary (i.e., the state data). The only property in the StatesDictionary class is the StateSelectList, which is an object that Html Helpers use with to render an HTML <select> element that displays a listing of states. The type Dictionary<string, string> in the StateSelectList property maps to the state abbreviation then state name, respectively.
public class StatesDictionary
{
    public static SelectList StateSelectList
    {
        get { return new SelectList(StateDictionary, "Value", "Key"); }
    } 
    public static readonly IDictionary<string, string> 
        StateDictionary = new Dictionary<string, string> { 
      {"Choose...",""}
    , { "Alabama", "AL" }
    , { "Alaska", "AK" }
    , { "Arizona", "AZ" }
    , { "Arkansas", "AR" }
    , { "California", "CA" }
    // code continues to add states...
    }; 
}
Data that lives in small lists and infrequently changes, like the StatesDictionary class, exists in all types of applications. In real world applications, you'll find a variety of methods for dealing with lookup data such as a list of states - often XML files and SQL tables. You can replace the code in the StateDictionary method to use entities from Entity Framework, read data from files, or any data access code that you require.
After creating the ViewModel, the next steps are to instantiate it in a controller and return it to the view.

Getting the ViewModel to the view

Starts with the controller...
Sending a ViewModel to the view for rendering will work the same as when dealing with a model. Since it's just a class, the view doesn't know, and doesn't care, where the model or ViewModel came from. You can create the instance of the ViewModel class in the controller, or resolve it if using an IoC container. Remember that just as you would do with views, you should keep controllers clean of unnecessary code, meaning that only code that fetches the model or ViewModel belongs here, and little more.
public ActionResult Edit(int id)
{
    Customer customer = context.Customers.Single(x => x.Id == id);
    var customerViewModel = new CustomerViewModel(customer);
    return View(customerViewModel);
}
Then the view renders the ViewModel...
In order for the view to know what object to use, set the @model keyword to point to the ViewModel, just like you already would with a regular model.
@model FourthCoffee.Web.ViewModels.CustomerViewModel
Because the Customer object is a property of the ViewModel, you'll see the model.Class.Property syntax to access the ViewModel data, similar to the following line of code.
<div class="editor-label">
    @Html.LabelFor(model => model.Customer.FirstName)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Customer.FirstName)
    @Html.ValidationMessageFor(model => model.Customer.FirstName)
</div>
@* ...View code continues rendering properties... *@
Additionally, you can edit the Edit/Create views so that the DropDownList containing a list of the states will display, and display the correct state matching that of the customer.
<div class="editor-field">    
    @Html.DropDownList("state", new SelectList(StatesDictionary.StateSelectList, 
                       "Value", "Text", Model.Customer == null ? "" : Model.Customer.State))
    @Html.ValidationMessageFor(model => model.Customer.State)
</div>
As you might have noticed, using a ViewModel is just as easy as using the ViewBag or ViewData objects. ViewModels, however, provide those extra benefits like being easier to test and optimize.

Checking the results

After a user navigates to the /Customers/Edit/1 URL in the browser, the Razor view engine renders the CustomerViewModel similarly to the following screen shot.
image
The State DropDownList displays the states and the current state for that customer, as expected.

Digging Further into ViewModels

Because ViewModels render pre-manipulated data that no longer have those 1:1 mappings between model classes and database tables, you'll need to do create mappings yourself. You can manually map small ViewModels, but this will quickly become burdensome when mapping larger classes, especially when working with parent-child-grandchild, multi-level, or complex data. This is where a tool such as AutoMapper comes into play. AutoMapper will let you fluently setup mappings between ViewModels and models more easily than doing so manually, or writing your own mapper.
Here are some tips for using ViewModels:
  • Put only data that you'll render in the ViewModel.
  • The view should direct the properties of the ViewModel, this way it fits better for rendering and maintenance.
  • Use a mapper when ViewModels become complex.
Some tools that can help assist you in generating POCOs (Plain Old CLR Objects) for models and ViewModels are:
POCO Generator
EF POCO Templates
In addition to these tools, you can use MvcScaffolding to create actions and views based on ViewModels. MvcScaffolding, invention of ASP.NET team member Steve Sanderson, gives you more power in creating CRUD, repository, unit test and other templates quickly and painlessly. Check out Steve's Multi-part series on MvcScaffolding here. MvcScaffolding works with ViewModels as well as models.
You should always prefer using a ViewModel rather than instantiating multiple models and putting that manipulation code in the controller.

Summary

ViewModels help you organize and manage data in MVC applications when you need to work with more complex data than the other objects allow. Using ViewModels gives you the flexibility to use data as you see fit. ViewModels area generally a more flexible way to access multiple data sources than models + ViewBag/ViewData objects.

Wednesday, July 13, 2011

Best Web Hosting Plans for Blogs !

Hosting a blog on your own custom domain is essential and it's important to find a reliable and affordable web host.  Having the right web host that you can trust is key for any website or blog.  So I've come up with an updated list of the best web hosts for blogs.  Please feel free to ask any questions because I am happy to help!



1) HostGator

HostGator is trusted by over 5,000,000 sites to suit their web hosting needs. I personally host several sites on HostGator and I strongly reccommend them. If you have any questions, please feel free to ask!

2) iPage

iPage provides web hosting at an affordable and competitive price and promises its clients reliable servers and great customer support.

3) JustHost

JustHost is committed to providing their clients with low cost blog hosting along with a promise of excellent customer support.

4) FatCow

FatCow is dedicated to delivering first class customer service to the small business owner in particular.

5) BlueHost

BlueHost has been offering exceptional web hosting solutions to thousands of personal and business sites since the year of 1996.

6) InMotion

InMotion guarantees reliable, affordable, and fast web hosting. This web hosting company promises 100% satisfaction guarantee.

7) DreamHost

DreamHost is a great web hosting company which offers reliable web servers and excellent customer service.

8) HostMonster

HostMonster is a solid company which has been offering web hosting solutions to thousands of personal and business web sites since 1996.

9) GoDaddy

GoDaddy is a secure and reliable web hosting provider which is very well known.






Conclusion

I hope this list has been helpful in your search for a great web host. I personally recommend HostGator as a reliable and trustworthy web hosting company, from my own experience. Also, please feel free to write your own reviews and questions in the comment section below.

Get Your Own Custom .COM Domain for Your Blog !

You may realize it's time to take things seriously and upgrade to your own custom domain (eg:yourblog.com). This post will hopefully help clarify the benefits of getting your own web domain and where to start. 




Why Choose a Custom Domain?

  1. It's Permanent.  You get to keep this domain name forever.  Services such as Blogger can deactivate your blog at any given time without notice.
  2. Promote your own brand.  If your site is a subdomain (such as .blogspot.com) you're promoting Google Blogger. Get your own domain and you'll be promoting your own unique brand. It gives you credibility and more people will trust you and what you have to offer.
  3. More Traffic.  Search engines prefer custom web domains over subdomains.
  4. Name Recognition.  It's much easier to remember and locate a domain name such as NinjaMan.com than NinjaMan.blogspot.com.
  5. Custom Email.  You can get your own custom email (eg: John@YourBlog.com). It looks very professional.
  6. Selling Rights.  Subdomain blogs, such as Blogspot, do not allow you to sell your blog. With your own custom domain you have the right to sell your website if you choose to do so one day.
  7. It's Cheap.  Give up one Starbucks drink a month and you can afford your own (.com, .net, .org, etc.) domain! It's only $3.96 a month with HostGator.

Choosing the Right Web Host

HostGator

If you are searching for a web hosting company that has reliable and fast servers, try HostGator. My friend James and I use them to host several sites, and I have been a customer for over one year without any issues. Our high traffic site TheBlogTemplates.com has been running very smoothly since September of 2009 and HostGator can handle it well.


Option 1 : Setup Your Custom Domain with Blogger

  1. Assuming you have your own hosting plan with Hostgator or another web host, contact your web hosting company about registering a domain. Tell them that you want to register a new domain name such as YourDomain.com.  Once the domain is registered ask them to create a CNAME record for your domain with the DNS directing to: ghs.google.com.  Please note that it may take a few hours to a day for your DNS settings to update correctly.
  2. Now we need to update your Blogger Settings at Blogger.com.  Go to Settings and then click on the Publishing tab.
    1. If you are publishing on Blogspot, you will see a link near the top offering to switch you to a custom domain. Go ahead and click on "custom domain".
    2. Now click "Switch to Advanced Settings".
    3. Fill in the name of your domain. I typed in YourDomain.com as an example.  Once you've done this enter the word verification at the bottom of the page and click "Save Settings".
    4. That's it! Your files should now be hosted at your new domain if your DNS settings have been updated yet.

Option 2 : Set up Your Custom Domain by Migrating from Blogger to WordPress

Wordpress is a free professional blogging platform which most people prefer over Blogger. Assuming once again that you have a web hosting account with one such as Hostgator, go to your cPanel.

  1. Watch this tutorial on how to Install WordPress from Fantastico.
  2. Then in your WordPress Admin area (domain.com/wp-admin), go to Manage → Import → Blogger and follow the directions to complete the import.
  3. Your blog posts should now be imported to Wordpress and should show up on your website!

Related Posts Plugin for WordPress, Blogger...

About Me

My photo
"BLOGGING" is my passion. I am crazy about it. I love to share knowledge with others, So I found this platform is good to share knowledge with everyone. And also I like to surf net a lot to search for the latest technologies and gadgets.