Translate
Friday, December 23, 2011
Sharepoint Interview Questions Books
Sharepoint interview Questions
Tuesday, August 9, 2011
Implementing Rhino Mocks Using Visual Studio with Repository and Service Test Cases
Implementing Rhino Mocks Using Visual Studio with Repository
and Service Test Cases
This is an Example used for to explain about how to create Repository and Service Test cases using Rhino Mocks
Common Objects
EmployeeFilter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversityEmployeeInformation.CommonObjects
{
public class EmployeeFilter
{
public int EmployeeId { get; set; }
public string EmployeeDept { get; set; }
}
}
MessageException
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
namespace UniversityEmployeeInformation.CommonObjects
{
public class MessageException : Exception
{
public MessageException(string message)
: base(message)
{
}
}
}
ServiceFault
using System.Runtime.Serialization;
namespace UniversityEmployeeInformation.CommonObjects
{
[DataContract]
public class ServiceFault
{
[DataMember]
public MessageException FaultException { get
}
}
UniversityEmployeeDo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversityEmployeeInformation.CommonObjects
{
public class UniversityEmployeeDo
{
public int EmployeeId { get; set; }
public string EmployeeName{get;set;}
public string EmployeeDepartment { get; set; }
}
}
UniversityEmployeeDTO
using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
namespace UniversityEmployeeInformation.CommonObjects
{
public class UniversityEmployeeDTO
{
private List
public List
{
get { return _employees; }
set { _employees = value; }
}
}
}
UniversityEmployeeRepository
Interface
UniversityEmployeeRep
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
namespace UniversityEmployeeInformation.UniversityEmployeeRepository.Interface
{
public interface IUniversityEmployeeRep
{
List
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo);
}
}
UniversityEmployeeRep
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using System.Data.SqlClient;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;
namespace UniversityEmployeeInformation.UniversityEmployeeRepository
{
public class UniversityEmployeeRep : IUniversityEmployeeRep
{
public List
{
List
SqlConnection con =
new SqlConnection("Data Source=XYZ;Initial Catalog=EmployeeDB;Integrated Security=True");
con.Open();
string commandText = "select * from dbo.Employee where EmployeeId=" + employeeFilter.EmployeeId;
SqlCommand cmd = new SqlCommand(commandText, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
UniversityEmployeeDo employeeDo = new UniversityEmployeeDo();
employeeDo.EmployeeId = Convert.ToInt32(dr["EmployeeId"]);
employeeDo.EmployeeDepartment = dr["EmployeeName"].ToString();
employeeDo.EmployeeName = dr["EmployeeDepartment"].ToString();
employees.Add(employeeDo);
}
return employees;
}
public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo)
{
throw new NotImplementedException();
}
}
}
UniversityManager
Interfacce
IUniversityEmployeeManager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
namespace UniversityEmployeeInformation.Manager.Interface
{
public interface IUniversityEmployeeManager
{
List
}
}
UniversityEmployeeManager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;
using UniversityEmployeeInformation.Manager.Interface;
namespace EmployeeInformation.Manager
{
public class UniversityEmployeeManager:IUniversityEmployeeManager
{
private IUniversityEmployeeRep _employee;
public IUniversityEmployeeRep Employee
{
set { _employee = value; }
}
public List
{
_employee = new UniversityEmployeeRep();
return _employee.GetEmployees(employeeFilter);
}
}
}
Let me create the service
UniversityEmployeeService
Interface
IUniversityEmployeeService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
namespace EmployeeInformation.EmployeeService.Interface
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IUniversityEmployeeService
{
[OperationContract]
[FaultContract(typeof(ServiceFault))]
List
[OperationContract]
[FaultContract(typeof(ServiceFault))]
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
}
UniversityEmployeeService
using System;
using System.Collections.Generic;
using System.ServiceModel;
using UniversityEmployeeInformation.CommonObjects;
using EmployeeInformation.EmployeeService.Interface;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;
using UniversityEmployeeInformation.Manager.Interface;
namespace UniversityEmployeeInformation.UniversityEmployeeService.Service
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class UniversityEmployeeService : IUniversityEmployeeService
{
private IUniversityEmployeeRep _employeeRepository;
private ServiceFault _fault;
public UniversityEmployeeService()
{
_employeeRepository = new UniversityEmployeeRep();
}
public UniversityEmployeeService(IUniversityEmployeeRep employee)
{
_employeeRepository = employee;
}
private IUniversityEmployeeManager _employeeManager;
public List
{
return _employeeRepository.GetEmployees(employeeFilter);
}
public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter)
{
try
{
if (employeeFilter.EmployeeId <= 0) { throw new MessageException("Invalid EmployeeId"); } UniversityEmployeeDo employee = _employeeRepository.GetEmployees(employeeFilter)[0]; UniversityEmployeeDo employeeDo = _employeeRepository.UpdateEmployee(employeeFilter, employee); return employeeDo; } catch (Exception ex) { _fault = new ServiceFault(){FaultException = (MessageException) ex}; throw new FaultException
}
}
}
}
Let us create Service Test Project
UniversityEmployeeServiceTest
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using System.Data.SqlClient;
using UniversityEmployeeInformation.UniversityEmployeeService.Service;
using NUnit;
using NUnit.Framework;
using Rhino.Mocks;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;
namespace UniversityEmployeeServiceTest
{
[TestFixture, System.ComponentModel.Description("EmployeeRepository Test")]
public class UniversityEmployeeServiceTest
{
private MockRepository _mockRepository;
private UniversityEmployeeService _employeeService;
private ServiceFault _fault;
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
}
[SetUp]
public void Initialize()
{
_employeeService = new UniversityEmployeeService { };
_mockRepository = new MockRepository();
}
[Test]
public void GetEmployeeServiceIntegrationTest()
{
EmployeeFilter employeeFilter = new EmployeeFilter();
employeeFilter.EmployeeId = 1223;
_employeeService = new UniversityEmployeeService();
List
Assert.IsNotNull(employees);
}
[Test]
public void GetEmployee_ReturnsExpected_Employee()
{
var employees = new List
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo();
employeeDo1.EmployeeId = 1223;
employeeDo1.EmployeeName = "ABC";
employeeDo1.EmployeeDepartment = "XYZ";
employeeDo2.EmployeeId = 123443;
employeeDo2.EmployeeName = "XYZ";
employeeDo2.EmployeeDepartment = "HIK";
employees.Add(employeeDo1);
employees.Add(employeeDo2);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employees);
}
_mockRepository.ReplayAll();
List
_mockRepository.VerifyAll();
Assert.AreSame(employees, employeesResult);
}
[Test]
public void GetEmployee_ReturnsNotExpected_Employee_ServiceMockTest()
{
var actualemployees = new List
var returnemployees = new List
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo();
employeeDo1.EmployeeId = 1223;
employeeDo1.EmployeeName = "ABC";
employeeDo1.EmployeeDepartment = "XYZ";
employeeDo2.EmployeeId = 123443;
employeeDo2.EmployeeName = "XYZ";
employeeDo2.EmployeeDepartment = "HIK";
returnemployees.Add(employeeDo1);
actualemployees.Add(employeeDo2);
actualemployees.Add(employeeDo2);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = 123443 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(returnemployees);
}
_mockRepository.ReplayAll();
List
_mockRepository.VerifyAll();
Assert.AreNotEqual(actualemployees, employeesResult);
}
[Test]
public void UpdateEmployee_Will_Return_EmployeeDo_with_Updates()
{
var inputEmployeeDo = new UniversityEmployeeDo();
List
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = 123 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);
}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();
Assert.AreSame(updatedEmployeeDo.EmployeeDepartment, employee.EmployeeDepartment);
}
[Test]
[ExpectedException(typeof(MessageException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{
try
{
var inputEmployeeDo = new UniversityEmployeeDo();
List
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = -1235 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);
}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();
}
catch (Exception ex)
{
throw ((System.ServiceModel.FaultException
FaultException;
}
}
}
}
This is a different kind of architecture where there will be Repository,Manager ,Comon Objects,DTOs and Service,
I used all these and created advance Mock Test cases for Service ,I used an example called UniversityEmployee Information
First part explains about the Domain Common Objects UniversityEmployeeDo which has the three properties
public int EmployeeId { get; set; }
public string EmployeeName{get;set;}
public string EmployeeDepartment { get; set; }
And also a EmployeeFilter Do with two Properties
public int EmployeeId { get; set; }
public string EmployeeDept { get; set; }
i also added MessageException and a DTO
UniversityEmployeeRepository
This is the Repository where we access the database or Update the database and so
I created an interface IUniversityEmployeeRep where i declared two methods
List
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo);
If you Observed the two Methods in implementation
The first one
public List
{
List
SqlConnection con =
new SqlConnection("Data Source=ABRAR-PC;Initial Catalog=EmployeeDB;Integrated Security=True");
con.Open();
string commandText = "select * from dbo.Employee where EmployeeId=" + employeeFilter.EmployeeId;
SqlCommand cmd = new SqlCommand(commandText, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
UniversityEmployeeDo employeeDo = new UniversityEmployeeDo();
employeeDo.EmployeeId = Convert.ToInt32(dr["EmployeeId"]);
employeeDo.EmployeeDepartment = dr["EmployeeName"].ToString();
employeeDo.EmployeeName = dr["EmployeeDepartment"].ToString();
employees.Add(employeeDo);
}
return employees;
}
Here, I am returning the employee list fetching from the database,there is another method
public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo)
{
throw new NotImplementedException();
}
UpdateEmployee which is not yet implemented and it has NotImplementedException throw Exception
I am using these two repository methods directly in Service
I created Service called UniversityEmployeeService which is a WCF Service and has two methods
Interface:
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IUniversityEmployeeService
{
[OperationContract]
[FaultContract(typeof(ServiceFault))]
List
[OperationContract]
[FaultContract(typeof(ServiceFault))]
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter);
}
And In Service
private IUniversityEmployeeRep _employeeRepository;
private ServiceFault _fault;
public UniversityEmployeeService()
{
_employeeRepository = new UniversityEmployeeRep();
}
public UniversityEmployeeService(IUniversityEmployeeRep employee)
{
_employeeRepository = employee;
}
private IUniversityEmployeeManager _employeeManager;
public List
{
return _employeeRepository.GetEmployees(employeeFilter);
}
public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter)
{
try
{
if (employeeFilter.EmployeeId <= 0) { throw new MessageException("Invalid EmployeeId"); } UniversityEmployeeDo employee = _employeeRepository.GetEmployees(employeeFilter)[0]; UniversityEmployeeDo employeeDo = _employeeRepository.UpdateEmployee(employeeFilter, employee); return employeeDo; } catch (Exception ex) { _fault = new ServiceFault(){FaultException = (MessageException) ex}; throw new FaultException
}
}
In Service I created Service constructor where I am going to inject the Mock Repository
In this Service I have two methods
1. GetEmployees
2. UpdateEmployee
Since i added Repository code in GetEmployee but i have not yet added and any logic in Updated Employee Repository method,but i create the update service method,i know what validations i need to put and check for the employee in Mock service Test case
Let us now work around the mock test cases
[Test]
public void GetEmployee_ReturnsExpected_Employee()
{
var employees = new List
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo();
employeeDo1.EmployeeId = 1223;
employeeDo1.EmployeeName = "ABC";
employeeDo1.EmployeeDepartment = "XYZ";
employeeDo2.EmployeeId = 123443;
employeeDo2.EmployeeName = "XYZ";
employeeDo2.EmployeeDepartment = "HIK";
employees.Add(employeeDo1);
employees.Add(employeeDo2);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employees);
}
_mockRepository.ReplayAll();
List
_mockRepository.VerifyAll();
Assert.AreSame(employees, employeesResult);
}
In this test case GetEmployee_ReturnsExpected_Employee will returns me the expected employee
I declare two Employees
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo(); and added to a list
and in Mock test i am asking return me only these two employees
and it will return me only these two employees without fetching it from the database because i mock the employee Repository Using
var mockEmployeeRepository = _mockRepository.StrictMock
In this my test case will be always pass ,it will be failed if someone changes the logic in Service
There is another benefit of using mock is that I explain in the next test case
[Test]
public void UpdateEmployee_Will_Return_EmployeeDo_with_Updates()
{
var inputEmployeeDo = new UniversityEmployeeDo();
List
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = 123 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);
}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();
Assert.AreSame(updatedEmployeeDo.EmployeeDepartment, employee.EmployeeDepartment);
}
In this test case _ UpdateEmployee_Will_Return_EmployeeDo_with_Updates()
I am askign to update the employee and return me updated UniversityEmployeeDo
For Exception Test case we can write like this I created one test case
[Test]
[ExpectedException(typeof(MessageException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{
var inputEmployeeDo = new UniversityEmployeeDo();
List
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = -1235 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);
}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();
}
This Test case say throw MessageException if i passed a Invalid employee Id
Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero
Working Solution will be provide based on your demand
on comments
Sunday, August 7, 2011
Sharepoint Object Model
Introduction to Sharepoint Object Model
Assert and Descriptions,in Implementing Rhino Mocks Using Visual Studio
Ordered and Unordered
Ordered--> it will check for same order which you specify in you mock test case with your actual method calling methods and objects
UnOrdered:->you no need to specify the order of the methods in mock test and it will will execute by default order
Expect.Call
Expect.Call(mockStudentRepository.GetStudentDetails()).Return(student1).IgnoreArguments();
here i am asking my test case to see in my service method it calls the GetStudentDetails() method,
It will return me student1 object
How to use for Call Repetition
Repeat.Any is mainly every important when you have test case where you are using foreach
for example let us say you have method call insertnew student record
and you have service where you are inserting multiple records at one time
public string submitStudnetRecors(List
{
foreach(studnet in students)
{
StudentBal student=new StudentBal();
studentbal.InsertNewstudent((studnet)
}
}
In this your repository would be like this
Expect.Call(mockStudentRepository.InsertNewstudent((studnet)).Repeat.Any.Return(message)
and other
Repeat.Once() ==> the default
Repeat.Any() ==> for number of times
Repeat.Never()
Repeat.AtLeastOnce()
Repeat.Twice()
Repeat.Times(13)
Repeat.Times(2, int.MaxValue)
Mock test with Void Methods
example:
public void sendEmail(emailDO)
{
your code here;
}
in this case you need to declare like this
Expect.Call(() => mailRepository.sendEmail(emailDO))
Ignore Argument Constraints
Here IgnoreArguments() is the keyword used here it will just ignore any arguments we passed ,and it just return whatever we mentioned in the Return, example here it just returns the EmployeeObject Return(EmployeeObject)
Some More Useful Assert statements
Is.Equal(3) ,Is.NotEqual(42) (this is applicable even for objects but need to write equal override for the object
)
Is.Same(obj) for objects
Is.NotSame(obj) ) for objects
Is.Null() it Checks for NULL values
Is.NotNull()
Is.GreaterThan(3)
Is.GreaterThanOrEqual(3)
Is.LessThan(3)
Is.LessThanOrEqual(3)
Text.StartsWith("hi Rhino Mock");
Text.EndsWith("Good Bye");
Text.Contains("Sachin Tendulkar");
For Exception Type Testing for Rhino Mock Test cases
[ExpectedException(typeof(BusinessException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{
//Test Case code here where exception occurs and will check for exception type and message if any
}
Saturday, August 6, 2011
Creating SharePoint 2010 Visual Web Parts in Visual Studio 2010
Create SharePoint 2010 Visual Web Parts in Visual Studio 2010
This is my first post on sharepoint 2010,I am creating a sharepoint 2010 visual webpart using visual studio 2010
1.Select Visual Studio 2010
figure 1
As shown in the figure 1
Give some name say HelloWorldVisualWebpart and click on for next screen,the screen will be similar like this as shown in figure 2
Figure 2
It is noticed that Deployed as Sandboxed Solution is Gray Out since visual webpart cannot be deployed as sandboxed solution.
Click Next solution Exloper will be displayed similar to shown in figure 3
.
Figure 3
You could Noticed that it creates files
1. Elements.xml
2. VisualWebPart1.cs
3. VisualWebPart1.webpart
4. VisualWebPart1UserControl.asx controls
It is also noticed that
The ascx control will be deployed in the following following folder
_CONTROLTEMPLATES/HelloWorldVisualWebpart/VisualWebPart1/VisualWebPart1UserControl.ascx
Let us write the code in VisualWebpart .cs file
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebC
ontrols;
namespace HelloWorldVisualWebpart.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/HelloWorldVisualWebpart/VisualWebPart1/VisualWebPart1UserControl.ascx";
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
Label lbl = new Label();
lbl.Text = "MY FIRST,WEBPART FROM HELLO WORLD FROM VISUAL WEBPART";
Controls.Add(lbl);
Controls.Add(control);
}
}
}
I added the code in create Child Controls method which is similar to page load method in normal Asp.net Page,
I declared the Control variable and load the Ascx control,and also i created lable
Label lbl = new Label();
This statement will create a lable and i added the text to lable text and finally i added lable to contol.
Deploy webpart and Add a Webpart in Home Page
In Galleries Section Click on Webparts which will displayed list of Webparts already deployed in the Portal.
Now we are deploying our webpart in the Home Page i used Home page in this example you can deploy the webpart on any page as you like,depends on the requirements,by default the webpart will be in Custom Category,if you want to make the changes like Title and description of webpart can also be possible by editing the webpart properties.
Now i deploye the webpart in Home Page it looks similar to like this
Wednesday, August 3, 2011
Best Practices using Visual Studio 2010 --http://www.microsoft.com/india/msdn/visualstudio2010/bestpractices.aspx
Best Practices using Visual Studio 2010 --http://www.microsoft.com/india/msdn/visualstudio2010/bestpractices.aspx
Saturday, July 30, 2011
Implementing Rhino Mocks Using Visual Studio .Net
Part One
Implementing Rhino Mocks Using Visual Studio .Net
Working with Rhino Mocks Using Visual Studio .Net 2010
(Recently added part3)
Implementing Mock Test cases for Repository in WCF Service,Please read it if you have time to know more about Repository Test cases ,Click here
How to implement Repository Mock Test cases in WCF Service
Introduction to Rhino Mock.
- using System.Collections.Generic;
- using BAL;
- using Services.StudentService;
- using Student = CommonObjects.Student;
-
- namespace Services.StudentService
- {
- public class Service : IService
- {
- private IStudent _student;
- public Service()
- {
- _student =new BAL.Student();
- }
- public Service(IStudent student)
- {
- _student = student;
- }
- public List
GetStudentList() - {
- List
students = new List (); - CommonObjects.Student student = _student.GetStudentDetails();
- students.Add(student);
- return students;
- }
-
- }
- }