I am trying to consume a service using Refit and I have the following problem. I was looking at the documentation but I can't find a solution and I don't know if I should do anything else.
This is my model
public partial class Global
{
[JsonProperty("updated")]
public long Updated { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("countryInfo")]
public List<CountryInfo> CountryInfo { get; set; }
}
My second class:
public partial class CountryInfo
{
[JsonProperty("_id")]
public long? Id { get; set; }
[JsonProperty("iso2")]
public string Iso2 { get; set; }
[JsonProperty("iso3")]
public string Iso3 { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
[JsonProperty("long")]
public double Long { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
Application with Refit
Link : https://corona.lmao.ninja/v2/countries
[Get("/v2/countries")]
Task<CountryInfo> GetCountry();
In my viewModel
private async Task GetTaskInterf()
{
var webApp = RestService.For<ICovidInterface>("https://corona.lmao.ninja");
var result = await webApp.GetGlobalCountry();
}
At the time of initialization
Related
I am currently trying to figure out a data model and struggling as its getting quite complex. What I've got so far is explained below. I'm going to implement this in an ASP.NET CORE MVC app to help with managing the harvest remotely.
The data model is as follows: Farms can have multiple fields, each field can have multiple types of crop. Each farm can also have multiple harvests with different crops to which I need to be able to store information about the total crop yield, for that harvest, that's specific to that farm. Any help would be greatly appreciated!
Thanks!
Attempted data model -
https://imgur.com/a/tgGIRNq
Edit - Sorry didn't specify how I would like an answer. The Entities as below:
I currently have -
public class Farm : IEntityBase<int>
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public ICollection<Field> Fields { get; set; }
public DateTime CreationTime { get; set; }
public ICollection<Harvest> Harvests { get; set; }
public Farm()
{
CreationTime = DateTime.Now;
}
}
public class Crop : IEntityBase<int>
{
public int Id { get; set; }
public DateTime CreationTime { get; set; }
public string CropName { get; set; }
public Crop()
{
CreationTime = DateTime.Now;
}
}
public class Field : IEntityBase<int>
{
public int Id { get; set; }
public string Name { get; set; }
public int FieldId { get; set; }
public DateTime CreationTime { get; set; }
public int FieldSize { get; set; }
public ICollection<Crop> Crops { get; set; }
public Field()
{
CreationTime = DateTime.Now;
}
}
public class Harvest:IEntityBase<int>
{
public int Id { get; set; }
public DateTime CreationTime { get; set; }
public int FarmId { get; set; }
public DateTime HarvestYear { get; set; }
public Crop Crop { get; set; }
public Harvest()
{
CreationTime = DateTime.Now;
HarvestYear = DateTime.Now;
}
}
I have an Exercise entity defined in my ASP.NET MVC4 Web Application.
I'm using the Form Authentication with the default AccountModels.cs class.
I have class which looks like
public class Exercise
{
private DateTime _DateCreated = DateTime.Now;
private UserProfile _Teacher;
public int Id{ get; set; }
public string Question { get; set; }
public int Anwser { get; set; }
public string Category { get; set; }
public int maxNbrOfAttempts { get; set; }
public string Hints { get; set; }
public virtual ICollection<Quiz> Quizzes { get; set; }
public DateTime Date
{
get { return _DateCreated; }
set { _DateCreated = value; }
}
public UserProfile Author
{
get { return _Teacher; }
set { _Teacher = value; }
}
}
Am I using the UserProfile correctly to link between an Exercise and a logged in user?
How can I get the current UserProfile in my controller?
Change it like this:
public class Exercise
{
public Exercise()
{
this.Date = DateTime.Now;
this.Author = User.Identity.Name; //Write this line if you want to set
//the currently logged in user as the Author
public int Id{ get; set; }
public string Question { get; set; }
public int Anwser { get; set; }
public string Category { get; set; }
public int maxNbrOfAttempts { get; set; }
public string Hints { get; set; }
public virtual ICollection<Quiz> Quizzes { get; set; }
public virtual DateTime Date { get; set; }
public virtual UserProfile Author { get; set; }
}
I think, I've read everything about this error and I tried everything. Here are my models:
Main:
public class Trip
{
public int TripId { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string Country { get; set; }
public float BasicPrice { get; set; }
public virtual ICollection<ApartmentType> ApartmentType { get; set; }
public virtual ICollection<TransportMethod> TransportMethod { get; set; }
public virtual ICollection<FeedingType> FeedingType { get; set; }
}
ApartmentType:
public class TransportMethod
{
public int TransportMethodId { get; set; }
public int TripId { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
FeedingType:
public class FeedingType
{
public int FeedingTypeId { get; set; }
public int TripId { get; set; }
public string Description { get; set; }
public float Price { get; set; }
}
TransportType:
public class TransportMethod
{
public int TransportMethodId { get; set; }
public int TripId { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
When serializng the Trip entity I get a circular dependency error. Things i tried:
Disable lazy loading in DbContext.
Adding
json.SerializerSettings.PreserveReferencesHandling=Newtonsoft.Json.PreserveReferencesHandling.All; to GLobal.asax
Adding a decorator [IgnoreDataMember] to TripId in every child entity.
Mapping this entity to a ViewModel which doesn't contain the ICollection members. - This worked ok, but at some point I will want to get those lists to the client.
I really don't know what's going on. What am I missing? I really can't spot any circular dependency.
Have you tried adding the [JsonIgnore] attribute to the TripId to the children entities?
http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm
or setting
json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
I got null value for byte array in json web service when deserializing.but it returns byte array when invoked from browser.
C# Code:
var url = re.DownloadString("XXXX/ListService.svc/lstFooddtl/1/21");
var a = JsonConvert.DeserializeObject<FooddtlResult>(url);
In a i got null for food_photo...when i deserialize...
c# Class:
public class photo
{
public byte[] food_photo { get; set; }
}
public class Food
{
public int food_id { get; set; }
public string food_name { get; set; }
public photo[] food_photo { get; set; }
public string unitcost { get; set; }
}
public class FooddtlResult
{
public Food[] Result { get; set; }
}
{"Result":[{"food_id":"61","food_name":"Idli","food_photo":[255,216,255,224,0,....217],"unitcost":null}]}
Your model does not match the JSON. For correct deserialization change your Food class to
public class Food
{
public int food_id { get; set; }
public string food_name { get; set; }
public byte[] food_photo { get; set; }
public string unitcost { get; set; }
}
and remove the photo class.
Otherwise, if you want to keep the model structure, then correct JSON should be:
{"Result":[{"food_id":"61","food_name":"Idli","food_photo":[{"food_photo":[255,216,255,224,0,....217]},{"food_photo":[255,...]},...],"unitcost":null}]}
How do I model the following using Castle ActiveRecord?
I have two classes, Customer and Task.
I would like to reuse a third class, Note, stored in a Collection in each of the Customer and Task classes.
public class Note
{
public int ID { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
public class Customer
{
public int ID { get; set; }
public IList<Note> Notes { get; set; }
}
public class Task
{
public int ID { get; set; }
public IList<Note> Notes { get; set; }
}
I would then like to be able to pass the Notes collection to a Gridview, Listview or Repeater in the relevant ASP.Net page for the Customer or Task classes.
I think what you need is to implement a type hierarchy. You can read about it here.
We settled on the following pattern:
[ActiveRecord]
public class Note
{
[PrimaryKey]
public int ID { get; set; }
[Property]
public string Subject { get; set; }
[Property]
public string Body { get; set; }
[BelongsTo]
public Customer Customer { get; set; }
[BelongsTo]
public Customer Task{ get; set; }
}
[ActiveRecord]
public class Customer
{
[PrimaryKey]
public int ID { get; set; }
[HasMany]
public IList<Note> Notes { get; set; }
}
[ActiveRecord]
public class Task
{
[PrimaryKey]
public int ID { get; set; }
[HasMany]
public IList<Note> Notes { get; set; }
}