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;
}
}
}
Header
Showing posts with label enterprise. Show all posts
Showing posts with label enterprise. Show all posts
Thursday, October 20, 2011
Entity Framework Tidbit 3
Labels:
.net,
enterprise,
entity framework,
microsoft,
web service
Friday, October 7, 2011
Entity Framework Tidbit 2
I'll be discussing about custom associations in entity framework. You will need this when you want to create a link between two tables which are related but no relation constraints defined. Usually, this happens for tables whose relationship to one another is not really that significant to the system. The relationship is only established on a per need basis only. To create this relationships in entity framework, you need to add a custom association between the tables. I consider it custom since, when you add tables in the entity model which have relation constraints, the association between the tables is automatically created. Here's the steps on how to do this:
1. Right click at the table and select new association.
2. Uncheck the "Add foreign key properties to.."
3. Click on the line created linking the two tables you have associated and go to properties. Then, set the referential constraints for the two tables.
After that, you have created a custom association between two tables! :)
1. Right click at the table and select new association.
2. Uncheck the "Add foreign key properties to.."
3. Click on the line created linking the two tables you have associated and go to properties. Then, set the referential constraints for the two tables.
After that, you have created a custom association between two tables! :)
Labels:
.net,
enterprise,
entity framework,
microsoft,
web service
Saturday, September 3, 2011
Entity Framework Tidbit 1
I'm not going to discuss on how to use the entity framework since there are a number of tutorials available in the web. I'll just share a realization I had while trying to do a sample application using the entity framework. In most examples I've read, I noticed that the entity objects are exposed in all projects where they are used in the solution. For simple applications, this kind of implementation is somehow acceptable. However, in enterprise-level applications, this won't do since there are a lot of issues that come into play such as security, performance and etc. Since my current is somehow an enterprise application, I was instructed to find a workaround so that I won't be exposing the entity objects to the web service projects. I read a number of articles before I came across the solution for this problem. I found out that referencing the auto-generated entity object classes in another project and adding a reference to it in the web service project is the solution for the issue. With this, you won't have to reference the entity model project in the web service project but you are still able to use the data from the entity object which were persisted to the entity classes. I'll be starting to apply the entity framework to the actual application I'm working on next week. Hopefully, everything will go smoothly and I'll find something interesting to share which would help those who are new in using the entity framework.
Labels:
.net,
enterprise,
entity framework,
microsoft,
web service
Subscribe to:
Posts (Atom)



