At the beginning of my previous project, I was sending entity framework objects over the web service. After some time, I realized that it wasn't a good implementation since you had to reference the entity framework objects in the web service. In addition, the performance isn't great but good enough considering the convenience it brings to us developers. To address these issues, I decided to use business objects with my current implementation. This will remove the entity framework object references in the web services because I will sending business objects instead. The entity framework will only be present in my repository class which contains all the methods to the data layer of the application. More so, new data contracts are needed for every business object I decide to send in the web service. Here's some sample code to illustrate this implementation:
Business Object
namespace EntityDTO
{
[DataContract(Namespace = "http://sb.services.betsson.com")]
public class Customer
{
[DataMember]
public int CustId {get; set;}
[DataMember]
public string CustName {get; set;}
}
}
Repository
namespace EntityRepository
{
public class CustomerRepo
{
private SBCustomerEntities _db;
public CustomerRepo()
{
_db = new SBCustomerEntities();
_db.ContextOptions.LazyLoadingEnabled = false;
}
public List<Customer> GetAllCustomers()
{
List<Customer> lCustomers = new List<Customer>();
var customers = from c in _db.tbl_customer select c;
lCustomers.addRange(from customer in customers select new Customer{CustId=customer.customerID, CustName=customer.customerName});
return lCustomers;
}
}
}
Web Service Interface
namespace Services
{
public interface ISampleService
{
[OperationContract]
List<Customer> GetCustomers();
}
}
Web Service Implementation
namespace Services
{
public sealed class SampleService : ISampleService
{
List<Customer> GetCustomers()
{
CustomerRepo customerRepo = new CustomerRepo();
var customers = customerRepo.GetAllCustomers();
return customers;
}
}
}
No comments:
Post a Comment