Friday, October 28, 2011

Dynamically inject values to object using reflection in type safe manner - DataAccess Layer Map Function

I am sure most of us, who are not using any ORM, have written methods like the following number of times in our life. This is the function that maps the resultant DataGrid with the equivalent object.

protected override UserSession Map(System.Data.IDataReader reader)
  var userSession = new UserSession()
  ID = NullHandler.GetLong(reader, "ID"),
 Browser = NullHandler.GetString(reader, "Browser"),
 BrowserVersion = NullHandler.GetString(reader, "BrowserVersion"),
 IpAddress = NullHandler.GetString(reader, "IpAddress"),
 UserAgent = NullHandler.GetString(reader, "UserAgent"),
 SiteId = NullHandler.GetInt32(reader, "SiteId"),
 StartTimeStamp = NullHandler.GetDateTime(reader, "StartTimeStamp"),
 EndTimeStamp = NullHandler.GetDateTime(reader, "EndTimeStamp"),
 UserName = NullHandler.GetString(reader, "UserName"),
 IsActive = NullHandler.GetBoolean(reader, "IsActive"),
 IsDeleted = NullHandler.GetBoolean(reader, "IsDeleted"),
 CreatedBy = NullHandler.GetString(reader, "CreatedBy"),
 CreatedByDateTime = NullHandler.GetDateTime(reader, "CreateByDateTime"),
 LastModifiedBy = NullHandler.GetString(reader, "LastModifiedBy"),
 LastModifiedByDateTime = NullHandler.GetDateTime(reader, "LastModifiedByDateTime"),
 SystemModstamp = NullHandler.GetDateTime(reader, "SystemModstamp")

  return userSession;

What most of us do to write this function is, we copy one similar function from some other class then, make changes to the function based on our entity. Whatever time this function takes to write, I am sure we all feel bore to do that.

Most of the time,  we always override this map function in every DataAccess class even though this function has a similar pattern of implementatiion.

That is why, I have tried to write a default implementation of this Map function in BaseDataAccess to save our time:

/// Maps and sets the Entity Model from the specified reader.protected virtual TEntity Map(IDataReader reader)
var entity = Activator.CreateInstance();
  foreach (PropertyInfo info in entity.GetType().GetProperties()
.Where(p => p.GetCustomAttributes(true)
 .FirstOrDefault(at => at.GetType() == typeof (ParameterExclusionAttribute)) == null)
object data = NullHandler.GetDataForColumn(info.Name, reader);
if (info.CanRead)
 Type propertyType = info.PropertyType;
 Type underlyingType = Nullable.GetUnderlyingType(propertyType);

 if(data==null || data == DBNull.Value)
if (propertyType == typeof(String))
 data = string.Empty;
 data = propertyType.IsValueType ? Activator.CreateInstance(propertyType) : null;
data = Convert.ChangeType(
 data, underlyingType ?? propertyType);                                
 info.SetValue(entity, data, null);
return entity;

This Map function in BaseDataAccess will automatically map the resultant datareader with your entity. So, it will save our time and energy and we will not need to write this Map functions in every DAL classe anymore. BUT, There is a constraint. To use this default implementation we must follow the practice of “Convention over Configuration”. To  use this, we must have the name of the object’s properties and the database table’s column name same.  I believe this is a very standard practice and we all follow this. Who are not following this yet, should start doing so from now.

Monday, October 10, 2011

SharePoint Server 2010 - Deployment conflicts - Could not delete file

Here is what I did:
1. I created one module in VS 2010.
2. I added one master page inside the module
3. I modified the Elements.xml file of the module as:

4. Then I created a feature for this module.
5. My target was to deploy the module so that I can use my custom master page for my site.

Here is what I Got when trying to deploy from VS 2010:
1. When I 1st time deployed the solution it worked fine. and I found my new master page in place.
2. When I did little modification into the master page and tried to deploy again, I got the following error:
3. I was digging with the problem. I found that if I do not use the master page from any site, I can deploy the solution. So, One solution could be before each deployment change the master page of the site, then deploy and then get the master page selected back for the site. 
4. But the solution was not acceptable and comfortable. So, I continued digging the issue and finally came to a solution.
Here is what I did to solve the issue:
1st of all it seems the issue was caused by a possible bug of VS 2010. If you go to the property window of the specific folder you will see Deployment Conflict Resolution is set to automatic. Which was the expected value for me.

 As it was not working, I changed the value to prompt. Then, I started to get prompt about the conflict. If I click the button to resolve the conflict automatically, I found the issue is solved and it worked.

Still, to me, the solution was not 100% acceptable as getting a prompt in every deploy was frustrating. Later, I change the value to "None". It was not an acceptable value at all to my consideration. but surprisingly it solved the problem. So, ISSUE RESOLVED! 

In Summary: 
1. Right click the folder/list of your concern.
2. Go to properties
3. Set Deployment conflict resolution to "None".

4. Deploy and have fun with SharePoint :)

SharePoint Server 2010 - Error occured in deployment step 'retract solution' cannot start service SPUserCodeV4 on computer

I was creating a simple feature in a SharePoint 2010 server project. While I was trying to deploy it using visual studio 2010 I got the following error:
error occured in deployment step 'retract solution' cannot start service SPUserCodeV4 on
computer ....
Later I found the solution of this issue. Here is what I had to do:

1. Go to Central Administration
2. Open "System Settings"
3. Open "Manage services on server"
4. Locate the service “Microsoft SharePoint Foundation User Code Service”
5. Start the service  “Microsoft SharePoint Foundation User Code Service”

Wednesday, October 5, 2011

Sharepoint Server 2010 - Learning Steps and Resources

When I have learned that, my new project is going to be a sharepoint 2010 project, I got excited. The reason is, I have been in software development for more than 7 years and gone through different technologies, but sharepoint is really a cutting edge item that I never looked into. However, when I started my learning, I discovered, there are lot of articles, books, tutorials, videos and other resources available for SharePoint over WWW both from Microsoft and 3rd parties. In first few days, I found my self in middle of a big ocean and was searching to reach some kind of land. Gradually, I started to get familiarize with Sharepoint and I felt, I should share the learning steps and resources with the community so that, new guys no more needs to swim across Sharepoint ocean in WWW. 
Here, I have sorted out the questions that came into my mind when I started learning SharePoint initially and I have tried to provide resources that were helpful to answer my questions.

1.     What are the minimum hardware requirements for a development computer to develop in SharePoint? What are the requirements for production machines?
2.     What is the history of SharePoint? How did this evolve?
3.     What are the options and versions of SharePoint available and what are their capabilities?
What are the limitations of SharePoint Foundation 2010 comparing with SharePoint Server 2010?
4.     What are the features of SharePoint server 2010?
5.     How should be the development environment for SharePoint development in a team?
6.     What are the tools available to develop in SharePoint 2010?
7.     How is the Architecture of share point?
8.     How does authentication managed in SharePoint 2010?
a.     Claim based authentication
b.    Forms based authentication
9.     What are Business connectivity services in SharePoint? What it can do?
10.  How to manage deployment of SharePoint project?
11.  How to manage source control of a SharePoint project?
12.  How to use Unit tests for a SharePoint project?

Here are some How To’s for SharePoint Server 2010 as I went through learning phase.

How To’s:

1.       Creating SharePoint 2010 Web Parts That Can Read and Write Data to External Data 
2.      Configuring a profile sync connection to an external sql database
3.      Getting Started with SharePoint 2010 Development Tools in Visual Studio 2010
4.      Basic SharePoint 2010 Feature Development
5.      Introducing SharePoint Designer 2010
6.      SharePoint 2010 master page as feature
7.      Enterprize Search
8.      Taxonomy/Managed Metadata
9.      Using LINQ to SharePoint in SharePoint 2010
10.    LINQ to SharePoint using Extension methods with Lambda expressions. (Codeplex solution)
11.    Use Dotnet Client Object Model in SharePoint 2010
12.    Use Javascript Client Object Model in SharePoint 2010
13.    Create a list, columns and view programmatically


And this is great tool from Microsoft to research, learn and implement the best practices in SharePoint 2010 server development and deployment:
Learning Tools:
1.       Developing Applications for SharePoint 2010