I wonder what the best way is to send a complex object when it is only for finding data purposes. I have a grid where paging, filtering , sorting etc will be done sever side. So I have to send all that data with each request.
public class DataConfiguratorDto
{
[Required]
public Paging Paging { get; set; }
public Sorting Sorting { get; set; }
public IList<Filtering> FlitersList { get; set; }
}
public enum FilterCondition
{
Equals,
Contains,
MoreThan,
LessThan,
}
public class Paging
{
public int RowNumberStart { get; set; }
[GreaterThan(nameof(RowNumberStart))]
public int RowNumberEnd { get; set; }
}
public class Filtering
{
[Required]
public string ColumnName { get; set; }
[Required]
public object FilterValue { get; set; }
[Required]
public FilterCondition FilterCondition { get; set; }
[Required]
public FilterType FilterType { get; set; }
}
public class Sorting
{
[Required]
public SortOption SortOption;
[Required]
public string ColumnName { get; set; }
}
public enum SortOption
{
Asc,
Desc
}
public enum FilterType
{
And,
Or
}
Now I could use POST but isn't it breaking it's purpose ?
What would be the best option to do this ?
You should use a post, sending the complex object in the body (as JSON for example).
per W3.org
"The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI."
"The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line."
See here for more info.
Related
I am sending a JObject from the frontend to my API, which is divided into First and Last childtokens, as seen in the picture below:
However, when I am trying to use the following code, the last part of childrendtoken is becoming null
var RVoucher = JsonConvert.DeserializeObject<VMReceive>(request.ToString());
This is what I am having in the debugging mode:
Here, the VMReceive is a viewModel that consists of another viewmodel "VMMonth"and an ado.net generated model class "ReceiveVoucher".
Code of the models are given below:
public class VMReceive
{
public List<VMMonth> Month { get; set; }
public ReceiveVoucher receiveVoucher { get; set; }
}
public class VMMonth
{
public int item_id { get; set; }
public string item_text { get; set; }
}
public partial class ReceiveVoucher
{
public int ReceiveVoucherId { get; set; }
public Nullable<int> MonthId { get; set; }
public string ReceivedBy { get; set; }
public string Remarks { get; set; }
public Nullable<int> ReceivedAmount { get; set; }
}
I have also tried putting [JsonProperty("")] over each property of my "ReceiveVoucher" model class, but got the same 'null' issue.
I am not sure about what I am doing wrong here, your suggestion regarding this will be very helpful.
Your JSON property name doesn't match. Your class uses receiveVoucher whereas the JSON is ReceiveAmount. Also, why are you using JObject in the first place, this should work by just using the class name as the action parameter:
public HttpResponse PostReceive([FromBody] VMReceive RVoucher, int userId)
{
...
}
I want to create a generic repository method for the complex data(Data Result of multiple joins in database). Following are the classes which hold the data . The data comes from SQL is the join of three tables(Tables architecture is same as of class)
public class InterfaceHeaderSetting
{
public Guid Id { get; set; }
public string CodaDocCode { get; set; }
public string Company { get; set; }
public string Currency { get; set; }
public string DocDescription { get; set; }
public Screen Screen { get; set; }
public Interface Interface { get; set; }
}
public class Screen
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Interface
{
public Guid Id { get; set; }
public string Name { get; set; }
}
I have the repository methods like
public IEnumerable<T> GetAllDynamic(string sql)
{
return Connection.Query<T>(sql, commandType: CommandType.StoredProcedure).ToList();
}
public T Update(T entity, string sql, object dynamicParameters)
{
return
Connection.Query<T>(sql, dynamicParameters, commandType: CommandType.StoredProcedure).SingleOrDefault();
}
I want one more repository method by which I can fill the objects like InterfaceHeaderSetting object.
I don't think you should reinvent the wheel. For this type of function i think Entity Framework is the solution.
In my projects i let entity framework handle more advanced querys that need joins and let dapper do the simple insert, update and select jobs.
I have domain model like this
public class EntityOne
{
public int EnityOneId { get; set; }
public int EntityOnePropertyOne { get; set; }
public List<EntityTwo> EntityTwos { get; set; }
}
public class EntityTwo
{
public int EntityTwoId { get; set; }
public string EntityTwoPropertyOne { get; set; }
public int EntityThreeId { get; set; }
public int EnityOneId { get; set; }
public virtual EntityOne EntityOne { get; set; }
public virtual EntityThree EntityThree { get; set; }
}
public class EntityThree
{
public int EntityThreeId { get; set; }
public string EntityThreePropertyOne { get; set; }
}
and I have DTO like this
public class EntityDTO
{
public int EntityOnePropertyOne { get; set; }
public string EntityThreePropertyOne_ValueOne { get; set; }
public string EntityThreePropertyOne_ValueTwo { get; set; }
public string EntityThreePropertyOne_ValueThree { get; set; }
public string EntityThreePropertyOne_ValueFour { get; set; }
public string EntityThreePropertyOne_ValueFive { get; set; }
}
I want to configure mapping from DTO to DomainModel and the reverse using AutoMapper but I didnt know how to do that... any suggestion or help
I'm not sure what you're trying to accomplish here.
I get that you want to map to EntityDTO, but from what other type? I will assume you want to use EntityTwo as the source.
In that case,
EntityOnePropertyOne: Will be obtained automatically via Flattening from the source (EntityTwo) - So, no problem here.
EntityThreePropertyOne_ValueOne: This will assume you have a property called EntityThree (which you do), and within that type, a property called PropertyOne_ValueOne of type int (which you don't). Same applies for the rest.
The other way around will get trickier, since I see there will be lots of properties ignored, so you need to tell AutoMapper, that you don't want it to be concerned about all that bunch of properties in your complex type, that don't come from the DTO.
Below is a POST url with content type application/x-www-form-urlencoded
POST
http://partner-site.com/api_implementation/hotel_availability
BODY
api_version=4
&hotels=[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}]
&start_date=2013-07-01
&end_date=2013-07-03
&num_adults=2
&num_rooms=1
&lang=en_US
¤cy=USD
&user_country=US
&device_type=d
&query_key=6167a22d1f87d2028bf60a8e5e27afa7_191_1360299600000_2_2
CONTENT TYPE
application/x-www-form-urlencoded
And i have wrote the class that read like
public class HotelAvailabilityRequest
{
public int api_version { get; set; }
public List<HotelSummary> hotels { get; set; }
public string start_date { get; set; }
public string end_date { get; set; }
public int num_adults { get; set; }
public int num_rooms { get; set; }
public string lang { get; set; }
public string query_key { get; set; }
public string currency { get; set; }
public string user_country { get; set; }
public string device_type { get; set; }
}
public class HotelSummary
{
public int ta_id { get; set; }
public string partner_id { get; set; }
public string partner_url { get; set; }
}
When i use the HotelAvailabilityRequest in my ASP MVC method
public ActionResult Hotel_Availability(HotelAvailabilityRequest request)
{}
I'm getting other parameter like request.api_version, request.device_type except request.hotels
I'm getting request.hotels.Count() equal to zero.
How do i get the request.hotel to bind accordingly?
After a few round of testing, I resort to manually parsing and deserializing my query string. Since primitive types (int, string like request.api_version) are well captured by the default model binder, my focus is to resolve the complex object type (request.hotels) that is problem.
First, I get the full query string with
Request.InputStream.Position = 0;
var queryString = new StreamReader(Request.InputStream).ReadToEnd();
var queryStringCollection = HttpUtility.ParseQueryString(queryString);
Then, I deserialize the query string into the intended object list
var hotelStr = queryStringCollection.Get("hotels");
var requestHotels = (List<HotelSummary>) JsonConvert.DeserializeObject(hotelStr, typeof (List<HotelSummary>));
request.hotels = requestHotels;
Not the best solution but it works. Hope someone workout a better ModelBinder for ASP MVC that resolve complex object type at binding.
I have this model
namespace ProjectTimer.Models
{
public class TimerContext : DbContext
{
public TimerContext()
: base("DefaultConnection")
{
}
public DbSet<Project> Projects { get; set; }
public DbSet<ProjectTimeSpan> TimeSpans { get; set; }
}
public class DomainBase
{
[Key]
public int Id { get; set; }
}
public class Project : DomainBase
{
public UserProfile User { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IList<ProjectTimeSpan> TimeSpans { get; set; }
}
[ComplexType]
public class ProjectTimeSpan
{
public DateTime TimeStart { get; set; }
public DateTime TimeEnd { get; set; }
public bool Active { get; set; }
}
}
When I try to use this action I get the exception The type 'ProjectTimer.Models.ProjectTimeSpan' has already been configured as an entity type. It cannot be reconfigured as a complex type.
public ActionResult Index()
{
using (var db = new TimerContext())
{
return View(db.Projects.ToList);
}
}
The view is using the model #model IList<ProjectTimer.Models.Project>
Can any one shine some light as to why this would be happening?
Your IList<ProjectTimeSpan> property is not supported by EF. A complex type must always be part of another entity type, you cannot use a complex type by itself. If you absolutely need to have ProjectTimeSpan as a complex type, you will need to create a dummy entity type that only contains a key and a ProjectTimeSpan, and change the type of Project.TimeSpans to a list of that new type.