Sunday, July 25, 2010

Windows Azure Hypervisor

Windows Azure physically runs in several of Microsoft’s data centers around the world. Data centers are where all the action is as far as Windows Azure is concerned. With all the machines in these data centers, the question is how an OS actually runs on these machines. The key problems to address would be sharing resources and migrating workloads. The industry solution to these problems with running different workloads in the data center is to use Virtualization.

Virtualization is the creation of a virtual (rather than actual) version of something, such as an operating system, a server, a storage device or network resources. It refers to the capability to run several guest operating systems simultaneously on the same machine under a host operating system. Each operating system sees an entire machine and is unaware of the existence of the other operating systems. These virtual machines share the resources of the underlying hardware, and also run in a sandbox to ensure that they can’t access resources there are not supposed to, or affect any of the other operating systems running on the machine.

To create several virtual machines on one physical machine, a thin piece of low level system software called the hypervisor is used. It’s the hypervisor that is responsible for resource allocations. To perform these tasks efficiently, the hypervisor relies on hardware support. Hypervisor makes use of a new privilege level called “Root mode”/ “Ring -1”, which is a relatively new development in the hardware world. The privileged instructions from the guest operating systems to the processor are translated to the hypervisor, and the hypervisor executes the privileged code for the operating. 

Saturday, July 24, 2010

Understanding Cloud Services – IaaS, PaaS & SaaS

Cloud-computing platforms can be differentiated by the kind of services they offer. Cloud computing is broken down into three segments, “Infrastructure “, Platform” and “Software”.
Infrastructure-as-a-Service (IaaS)
Infrastructure as a Service (IaaS) is delivery of the computing infrastructure as a fully outsourced service. Some of the companies that provide infrastructure services are Google, IBM, Amazon Web Services (AWS) etc. Managed hosting and development environments are the services included in IaaS. The user can buy the infrastructure according to the requirements at any particular point of time instead of buying the infrastructure that might not be used for months. IaaS operates on a “Pay as you go” model ensuring that the users pay for only what they are using. Virtualization enables IaaS providers to offer almost unlimited instances of servers to customers and make cost-effective use of the hosting hardware. IaaS users enjoy access to enterprise grade IT Infrastructure and resources that might be very costly if purchased completely. Thus dynamic scaling, usage based pricing, reduced costs and access to superior IT resources are some of the benefits of IaaS. IaaS is also sometimes referred to as Hardware as a Service (HaaS). An Infrastructure as a Service offering also provides maximum flexibility because just about anything that can be virtualized can be run on these platforms. This is perhaps the biggest benefit of an IaaS environment. For a startup or small business, one of the most difficult things to do is keep capital expenditures under control. By moving your infrastructure to the cloud, you have the ability to scale as if you owned your own hardware and data center.
Platform-as-a-Service (Paas)
Providers such as Windows Azure and Google App Engine (GAE) provide a platform that users write to. In this case, the term platform refers to something that abstracts away the lower levels of the stack. This application runs in a specialized environment. PaaS offers a development platform for developers. The end users write their own code and the PaaS provider uploads that code and presents it on the web. SalesForce.com’s Force.com is an example of PaaS. PaaS provides services to develop, test, deploy, host and maintain applications in the same integrated development environment. It also provides some level of support for the creation of applications. Thus PaaS offers a faster more cost effective model for application development and delivery. The PaaS provider manages upgrades, patches and other routine system maintenance. PaaS is based on a metering or subscription model so users only pay for what they use. Users take what they need without worrying about the complexity behind the scenes.
Software-as-a-Service (SaaS)
Software as a Service (SaaS) is the service based on the concept of renting software from a service provider rather than buying it yourself. The software is hosted on centralized network servers to make functionality available over the web or intranet. Also known as “software on demand” it is currently the most popular type of cloud computing because of its high flexibility, great services, enhanced scalability and less maintenance. Yahoo mail, Google docs, CRM applications are all instances of SaaS. With a web-based CRM all that employees need to do is register and login to the central system and import any existing customer data. The service provider hosts both the application and the data so the end user is free to use the service from anywhere. SaaS is very effective in lowering the costs of business as it provides the business an access to applications at a cost normally far cheaper than a licensed application fee which is possible due to its monthly fees based revenue model. With SaaS user need not worry about installation or upgrades.

Monday, July 5, 2010

Data Access Application Block 5.0 – Part 3

Updating data
The Data Access block provides features that support data updates. You can execute update queries (such as INSERT, DELETE, and UPDATE statements) directly against a database using the ExecuteNonQuery method. The ExecuteNonQuery method has a broad set of overloads. You can specify a CommandType (the default is StoredProcedure) and either a SQL statement or a stored procedure name. You can also pass in an array of Object instances that represent the parameters for the query.
The data accessor instance passed as parameter to the update method in the repository class methods are later used to construct the updated entity object. The code sample is as
public class CustomerUpdateAccessorFactory : ISaveAccessorFactory<Customer, long, CustomerUpdateIdentity>
{
    public DataAccessor<CustomerUpdateIdentity> ConstructDataAccessor(Database database)
    {
        return database.CreateSprocAccessor<CustomerUpdateIdentity>("usp_UpdateCustomer", new UpdateCustomerParameterMapper(), ConstructMapper());
    }

    public IRowMapper<CustomerUpdateIdentity> ConstructMapper()
    {
        IRowMapper<CustomerUpdateIdentity> rowMapper = MapBuilder<CustomerUpdateIdentity>.MapAllProperties()
            .Map(x => x.Version).WithFunc(dataRecord =>
            {
                int index = dataRecord.GetOrdinal("VersionNew");
                if (!dataRecord.IsDBNull(index))
                {

                    byte[] version = new Byte[(dataRecord.GetBytes(index, 0, null, 0, int.MaxValue))];
                    dataRecord.GetBytes(index, 0, version, 0, version.Length);
                    return version;
                }
                return null;
            })
            .Build();
        return rowMapper;
    }

    public object[] LoadParameters(Customer entity)
    {
        return new object[] { entity.Id, entity.FirstName, entity.LastName, entity.Address, entity.Version };
    }
}

The repository method for update
public TIdentity Save(ISaveAccessorFactory saveAccessorFactory, T entity)
{
    return saveAccessorFactory.ConstructDataAccessor(___Database)
        .Execute(saveAccessorFactory.LoadParameters(entity))
        .FirstOrDefault();
}

[TestMethod]
public void Can_updated_values_into_db()
{
    var baseRepository = new BaseRepository<Customer, long>();
    Customer existingCustomer = baseRepository.FindOne<long>(new GetCustomerAccessor(), 288);
    var customerUpdateIdentity = baseRepository.Save<CustomerUpdateIdentity>(new CustomerUpdateAccessorFactory(), existingCustomer);
    Assert.IsNotNull(customerUpdateIdentity.ChangedDate);
    Assert.IsNotNull(customerUpdateIdentity.Version);
}

Sunday, July 4, 2010

Data Access Application Block 5.0 – Part 2

Using DataAccessor Factory methods to retrieve data using data access application block.
 The data access application block contains features that allow you to extract data using a SQL statement or a stored procedure as the query, and have the data returned to you as a sequence of objects that implements the IEnumerable interface. This allows you to execute queries, or obtain lists or arrays of objects that represent the original data in the database.
The block provides two core classes for performing this kind of query: the SprocAccessor and the SqlStringAccessor. You can create and execute these accessors in one operation using the ExecuteSprocAccessor and ExecuteSqlAccessor methods of the Database class, or create a new accessor directly and then call its Execute method.
Accessors use two other objects to manage the parameters you want to pass into the accessor (and on to the database as it executes the query), and to map the values in the rows returned from the database to the properties of the objects it will return to the client code.

The accessor will attempt to resolve the parameters automatically using a default mapper if you do not specify a parameter mapper. However, this feature is only available for stored procedures executed against SQL Server and Oracle databases. It is not available when using SQL statements, or for other databases and providers, where you must specify a custom parameter mapper that can resolve the parameters.
If you do not specify an output mapper, the block uses a default map builder class that maps the column names of the returned data to properties of the objects it creates. Alternatively, you can create a custom mapping to specify the relationship between columns in the row set and the properties of the objects.
The below code sample shows an example of creating and executing a customer accessor.
public class GetCustomerAccessor : IValueAccessorFactory<Customer, long, long>
{
    public DataAccessor<Customer> ConstructDataAccessor(Database database)
    {
        return database.CreateSprocAccessor<Customer>("usp_GetCustomerById", new GetCustomerParameterMapper(), ConstructMapper());
    }

    public IRowMapper<Customer> ConstructMapper()
    {
        IRowMapper<Customer> customerMapper = MapBuilder<Customer>.MapAllProperties()
            .Map(x => x.Id).ToColumn("CustomerIntId")
            .Map(x => x.Version).WithFunc(dataRecord =>
            {
                int index = dataRecord.GetOrdinal("Version");
                if (!dataRecord.IsDBNull(index))
                {

                    byte[] version = new Byte[(dataRecord.GetBytes(index, 0, null, 0, int.MaxValue))];
                    dataRecord.GetBytes(index, 0, version, 0, version.Length);
                    return version;
                }
                return null;
            })
            .Build();
        return customerMapper;
    }

    public object[] LoadParameters(long id)
    {
        return new Object[] { id };
    }
}

This accessor is called from a BaseRepository implementation to construct the required object
public IEnumerable Find(IAccessorFactory accessorFactory)
{
     return accessorFactory.ConstructDataAccessor(___Database)
                .Execute();
}

Test Methods
[TestMethod]
public void Can_findone_method_return_entity_for_specified_parameter_values()
{
    var baseRepository = new BaseRepository<Customer, long>();
    var customer = baseRepository.FindOne<long>(new GetCustomerAccessor(), 288);
    Assert.IsNotNull(customer, "Failed to retrieve customer");
    Assert.AreEqual<string>(customer.FirstName, "Prajeesh");
}