ASP.Net WebAPI IIQueryable covert to POCO Objects - linq

I have been using the WebAPI in ASP.Net to allow access to our Entity Framework Objects.
The problem I am having is converting the objects to a custom POCO for our end customers to use.
I need to convert the Entity Framework Object to a custom POCO.
For example in our SQL Database we have Tbl_Person with the following properties
PersonID
FirstName
SureName
DateOFBirth
AnnualSalary
This table maps to an Entity Framework class Person with the same properties.
But I want to change the properties so that when a end customer accesses it they get a POCO like:
PersonID
Name
Age
SalaryRange
I also want to keep the current features such as JSON & XMLoutput and allow for OData queries.
I have been trying to 'Collect' the Odata Query and applyto my database context but this does not seem to be work correctly
Please see code example below:
Imports System.Net
Imports System.Web.Http
Imports System.Data.Entity
Public Class PeopleData
Inherits DbContext
Public Property People() As DbSet(Of Person)
End Class
Public Class Person
Public Property PersonID() As Integer
Public Property FirstName() As String
Public Property SureName() As String
Public Property DateOFBirth() As Date
Public Property AnnualSalary() As Integer
End Class
Public Class PeopleController
Inherits System.Web.Http.ApiController
Private db As New PeopleData
Function GetPeople(query As OData.Query.ODataQueryOptions(Of Person)) As IQueryable(Of apiPerson)
Dim pep = query.ApplyTo(db.People)
Dim resPep As New List(Of apiPerson)
For Each p In pep
resPep.Add(New apiPerson(p))
Next
Return resPep.AsQueryable
End Function
End Class
Public Class apiPerson
Public Sub New(ByVal p As Person)
PersonID = p.PersonID
Name = p.FirstName & " " & p.SureName
Age = Date.Now.Year - p.DateOFBirth.Year
If p.AnnualSalary > 15000 Then
SalaryRange = "High"
Else
SalaryRange = "Low"
End If
End Sub
Public Property PersonID() As Integer
Public Property Name() As String
Public Property Age() As Integer
Public Property SalaryRange() As String
End Class
I have two problems:
1) The API help pages don't populate and only produce this error: 'Sample not available.'
I like the dynamic help pages & that they pick up code comments, this is a really quick and easy way to maintain documentation. How can I get them to work with the ApiPerson?
2) If I try /api/people?$filter=Age eq 29 I get an error Type 'MvcApiPeople.Person' does not have a property 'Age'.
I understand that the LINQ Query is been passed to the 'Person' and that property does not exist but how can I 'Translate' queries to map to different properties in the actual Database Object?

Your action declaration should be
Function GetPeople(query As OData.Query.ODataQueryOptions(Of apiPerson)) As IQueryable(Of apiPerson)
i.e query parameter should be of type ODataQueryOptions (Of apiPerson) not ODataQueryOptions (Of Person).

Regarding your question 1:
I think your problem is that the apiPerson-class is missing a parameterless constructor.
Also, I found a blog post with some information on how to customize sample generation when the default generation mechanism doesn't work:
http://blogs.msdn.com/b/yaohuang1/archive/2012/10/13/asp-net-web-api-help-page-part-2-providing-custom-samples-on-the-help-page.aspx.

After playing around with many different solutions I decided the simplest way to control the information that is made available on an API and still allow iQueryable is to control the data at database level or at class level with Data Contract annotations.
I actually created views in my database to render the data exactly how I want it to appear for end customers. I felt this had the best benefits for performance and speed of implementation.
Thank you to all the guys who offered suggestions.

Related

Map extra column from stored procedure to Entity Framework code first model

I am using Entity Framework code first with a generic repository pattern with ASP.NET MVC. I have two tables Category and Product.
My model class of product is like this
Public class Product
{
public int ProductID{get;set;}
Public int CategoryID{get;set;}
[ForeignKey("CategoryID")]
public virtual Category Category{get;set;}
[NotMapped]
public string CategoryName{get;set;}
}
The model is binding correctly as long as I am getting data using DBContext.
But I am having a problem when I am getting list of products from stored procedure mapped to Product object. So it is not mapping the Category property of Product object and hence I cannot able to get Category.CategoryName.
So I added a new property with [NotMapped] attribute in product class as CategoryName. But it is also not binding from stored procedure.
And if I remove the [NotMapped] attribute then it is correctly binding from stored procedure but error occurs again when getting product by DbContext (Linq).
Please help me in this regards.
You don't need to add an extra property, use the DbSet.SqlQuery method for queries that return entity types. The returned objects must be of the type expected by the DbSet object, and they are automatically tracked by the database context unless you turn tracking off.
var products= _context.Products.SqlQuery("storedProcedureName",params);
The columns returned by SP should match the properties of your entity type otherwise, it will throw an exception.
After execute your SP, you should be able of get the CategoryName through your Category navigation property:
var catName=someProduct.Category.CategoryName;
On the other hand, the returned data by the Database.SqlQuery isn't tracked by the database context, even if you use this method to retrieve entity types. If you want to track the entities that you get after execute your SP using this method, you can try this:
//Attach the entity to the DbContext
_context.Product.Attach(someProduct);
//The Category navigation property will be lazy loaded
var catName=someProduct.Category.CategoryName;
If you have disabled lazy loading you can load explicitly your navigation property:
//Load the Category navigation property explicitly
_context.Entry(someProduct).Reference(c => c.Category).Load();

How to override the default model construction behavior in WebAPI

I currently have a project where I am utilizing a custom library of business objects that we want to pass over-the-wire through WebAPI/WFC/etc as needed. One of the hurdles I am reaching is dealing with the deserialization of an object through WebAPI.
The objects themselves adhere to the Factory Pattern, and as such, don't have public parameterless constructors (they are marked protected) and use a Factory to create an instance of the object. There are several important reasons for doing this, but we would like to be able to use the objects without the need for intermediary classes/models to be created as well.
Because of this, when the model is being bound to, the WebAPI framework fails to create an instance of the object, citing the "no parameterless constructors" error. I need to find a way to be able to call the factory method and return the new object to either a formatter or binder (or something else which is a part of the deserialization process) somehow.
Is there a clear cut way (and documentation) on how to extend Web API to handle this situation without having to implement another framework on top of it? I would appreciate any help that you can provide.
Edit:
So I eneded up creating a new Model Binder and created the object through the factory class in BindModel(). By assigning the object to bindingContext.Model and then just manually deserializing the object, I was able to achieve what was needed, but I am not sure if its 100% the right way.
See below for my code:
Public Class FactoryModelBinder
Implements IModelBinder
Public Function MindModel(actionContext as HttpActionContext, bindingContext as ModelBindingContext) As Boolean Implements IModelBinder.BindModel
Dim type = bindingModel.ModelType
Dim attributes = type.GetCustomAttributes(FactoryAttribute, False)
If attributes.Length > 0 Then
Dim factoryAttribute As FactoryAttribute = DirectCast(attributes(0), FactoryAttribute)
bindingContext.Model = factoryAttribute.FactoryType.InvokeMember("Create", BindingFlags.InokveMethod Or BindingFlags.Public Or BindingFlags.Static, Nothing, Nothing, Nothing)
Dim data as FormDataCollection = New FormDataCollection(actionContext.Request.Content.ReadAsStringAsync().Result)
Dim dict as NameValueCollection = data.ReadAsNameValueCollection()
For Each item as String in dict.Keys
Dim pi as PropertyInfo = bindingContext.Model.GetType().GetProperty(item)
pi.SetValue(bindingContext.Model, dict(item), Nothing)
Next
Return True
End If
Return False
End Function
This code relies upon a custom attribute (FactoryAttribute) which specifies in the object class, the Type of the Factory so it can be used to call the Create() method.
I appreciate any input on this.

Can mapping the results of a linq query being mapped to a class be simplified?

I've got a class called Customer that has several properties, i.e. FirstName, LastName, City, State
The Customer class has some data annotations to fix naming and other things. (This class is being used as a model for MVC)
I've got a Linq query against an EF5 entity that has those same fields. Is there a simple way to map the query results to the class other than:
customer.FirstName = item.FirstName;
customer.LastName = item.LastName;
customer.City = item.City;
etc.....
I have run across references to automapper (and others) but was wondering of there are other options based on the identical nature of the results and class?
If you are creating the objects at that time, using object initalizers is probably the cleanest coded solution.
var model = new myMvcModel()
{
FirstName = input.FirstName,
LastName = input.LastName,
City = input.City
}
Otherwise using reflection or AutoMapper as Ek0nomik suggest are your only solutions that I could suggest.
Like Ek0nomik I prefer to do it manually rather than AutoMapper. Even created a VS macro to inspect both objects and output some scaffolding.

How can I create a LINQ view?

My team is using Entity Framework 4.3.0 - Code Only with POCO classes as our ORM. Right now we use DBSets of Classes to access our 'tables'
Public Property Customers As DbSet(Of Customers)
But often we are doing soft deletes based on a IsDeleted column in LINQ, and filtering our select statements accordingly:
Dim LiveCustomers =
From C In EM.Customers
Where C.DeleteFlag = False
What I would really like to do is, instead of writing every query to include this filter, create some lower level property (possibly at our inherited DbContext level) that provides the filtered set, while maintaining strong type.
I tried this:
Public Property Customers As DbSet(Of Customer)
Public Property Customers_Live As DbSet(Of Customer)
Get
Return From C In Customers
Where C.DeleteFlag = False
End Get
Set(value As DbSet(Of Customer))
Customers = value
End Set
End Property
However that yielded me this error:
Multiple object sets per type are not supported. The object sets 'Customers' and 'Customers_Live' can both contain instances of type '__.Customer'.
A quick check on google yielded this promising result (How to: Query Objects with Multiple Entity Sets per Type) But after updating my Connection String, I'm still getting the same error.
<add name="EntityManager"
providerName="System.Data.SqlClient"
connectionString="
Data Source=xxxxxx;
Initial Catalog=xxxxxx;
User Id=xxxxxx;
Password=xxxxxx;
MultipleActiveResultSets=True"/>
So my question is, how could I effectively create a LINQ view that allows me to apply filtering, without impacting the upstream usage too drastically?
Change your property like this:
Public Property Customers As DbSet(Of Customer)
Public Property Customers_Live As IQueryable(Of Customer)
Get
Return From C In Customers
Where C.DeleteFlag = False
End Get
End Property
This is slightly different, as you won't have things like Add() or Remove(), but for a view you typically wouldn't expect to have that kind of functionality. If you want to add a new one, or remove one you should use the normal Customers property.
You could have your POCO classes inherit from a new class that has a new method that would do the filtering for you. Add something like this to the new class
--PSEUDO CODE!--
Public Function Filtered() as IEnumerable(Of Out T)
Return (From x In Me Where x.DeleteFlag).ToList()
End Function
and you could call it like:
Dim LiveCustomers =
From C In EM.Customers.Filtered
Or you could create an Interface and do a dependancy injection when you call your linq query. You'll have to google that one :)

linq to sql OnLoaded() with SQL View?

I am trying to extend my Linq-to-Sql entity with a few extra properties. These are "calculated" properties based on data from the underlying SQL View. For example, think of having a Date of Birth field, which is used to calculate an extended Age field.
I tried to extend my entity class by extending the OnLoaded() method.
I get a compile time error however stating that I cannot create it. I checked the designer code for my LTS entity class, and it doesn't have a partial definition for any of the expected extension points.
I checked a few of my other LTS entity classes and they do have these extension points. The only difference I see is that the one without is loaded from a SQL View, rather than a table. Is there a way to hook into a "Loaded" event when loading from a SQL View?
TIA!
I found that I did not have a PrimaryKey specified for my Linq-to-Sql entity class. I believe without a Primary Key specified, no extension methods generated in the entity class. Once I specified a Primary Key on my LTS entity class definition (through the designer), I was able to extend the OnLoaded() event.
You can do this by means of a property. Just create a partial class with the same name as your entity. Any properties or methods that you add will automatically be part of the entity and allow to use any of its members.
Here's an example of the pattern:
public partial class [The Name of the Entity]
{
public int Age
{
get
{
return CalculateAge(this.DateOfBirth);
}
}
}
Here's some logic on how to calculate the Age (Source: Geekpedia)
public static int CalculateAge(DateTime BirthDate)
{
int YearsPassed = DateTime.Now.Year - BirthDate.Year;
// Are we before the birth date this year? If so subtract one year from the mix
if (DateTime.Now.Month < BirthDate.Month ||
(DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
{
YearsPassed--;
}
return YearsPassed;
}

Resources