Web API - Return some fields from model - c#

I'm having this model :
public class Quiz
{
public int Id { get; set; }
public string Title { get; set; }
public int CurrentQuestion { get; set; }
[JsonIgnore]
public virtual ICollection<Question> Questions { get; set; }
}
There is [JsonIgnore] which tells JSON Serializer to ignore this field(Questions). So,I'm having an action which returns serialized Quiz without Questions. I have to implement another action which will return all fields (Questions inclusive). How can I do this ? I need both actions.

It's very good practice not to return your domain models from and API. Better way is to create view model classes and return them instead.
So in your example you'd simply create:
public class QuizViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public int CurrentQuestion { get; set; }
}
and use it to return data from your API.
Obviously in some bigger classes it would be nightmare to crate the code copying all the properties, but don't worry - Automapper (http://automapper.org/) comes to rescue! :)
//Best put this line in app init code
Mapper.CrateMap<Quiz, QuizViewModel>();
//And in your API
var quiz = GetSomeQuiz();
return Mapper.Map<QuizViewModel>(quiz);
You then create another view model class with Questions field in the same way.

You need to slightly change your code as below although its very simple :)
[Serializable]
public class Quiz
{
public int Id { get; set; }
public string Title { get; set; }
public int CurrentQuestion { get; set; }
}
[Serializable]
public class QuizWithQuestions : Quiz
{
public ICollection<Question> Questions { get; set; }
}
Now when you want to include Collection of Questions as well use QuizWithQuestions class.

I had the similar problem. And my solution is:
By default I have DBContext with disable LazyLoading:
public EFDbContext()
:base("EFDbContext")
{
this.Configuration.ProxyCreationEnabled = true;
this.Configuration.LazyLoadingEnabled = false;
}
So, all navigation property (like Questions) will be NULL. Then in my WebAPIConfig I configure formatter to hide null values:
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling
= Newtonsoft.Json.NullValueHandling.Ignore;
When I need a result with all fields from model then I just turn on the LazyLoading in controller:
repository.SwitchLazyLoading(true);
The method in repository:
public void SwitchLazyLoading(bool value)
{
this.context.Configuration.LazyLoadingEnabled = value;
}
I do not use [JsonIgnore] I use only [IgnoreDataMember]
Please look at the Switches for LazyLoading with Repository pattern

Related

Send a limited/reduced class to frontend

What I want
I want to send a limited/reduced class/object to frontend (as JSON). I use .NET Core 5.
What I have
I have a model class like this:
namespace Tasks.Models
{
public class Resources
{
public Guid Id { get; set; }
public string Type { get; set; }
public string Url { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime? Createdon { get; set; }
public Guid Userid { get; set; }
public Guid Taskid { get; set; }
public int Clicked { get; set; }
public byte Active { get; set; }
+++ many more properties
}
}
Now depending on the which controller that calls this model I want to have different "kind" of models. So if the resource is file I maybe want the properties Id,Type,Name. But if the resource is URL I want Id, Url, Name.
I tried setting up a method that "initialized the fields I wanted, but that also returned all properties
public static Responses FileResponse()
{
var response = new Responses()
{
Id = new Guid(),
Name = "",
Type = "File",
};
return response;
}
Now, when I call the Resources class or this method I get all properties, and returning it to the view presents all properties, but mostly as null, because I only set the three fields in the method.
What is the recommended way of solving this?
If you want to remove the field if it's null instead of showing in json with null value.
public class Resources
{
public Guid Id { get; set; }
public string Type { get; set; }
// if null, dont show it in JSON output
[JsonIgnoreAttribute(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Url { get; set; }
// if null, dont show it in JSON output
[JsonIgnoreAttribute(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Name { get; set; }
public string Description { get; set; }
public DateTime? Createdon { get; set; }
public Guid Userid { get; set; }
public Guid Taskid { get; set; }
public int Clicked { get; set; }
public byte Active { get; set; }
}
PS: Fiddle https://dotnetfiddle.net/hiMAci
It is just limiting the Resource class I am not able to do
Yep, side effect of C# being strongly typed, with object X definitely having properties Y and Z. You need differently shaped objects - either full on classes or records - that name the reduced set of properties because the serializer is going to look a tthe object and ser every property it can find.
You could make a new class for every variation - quick and easy with records, and easy to pass around inside your C#:
public record FileThing(string Id, string Type, string Name);
//make a new one and return it
new FileThing(someResources.Id, someResources.Type, someResources.Name);
Or can consider using an anonymous type if you're literally looking to put a few properties into some json, down a socket to a consuming front end (I can't quite decide what you mean by "view" - it doesn't seem to be an MVC View) that only cares about a few props out of many
So if the resource is file I maybe want the properties Id,Type,Name. But if the resource is URL I want Id, Url, Name.
public ActionResult SomeControllerMethod(){
if(isFile)
return Ok(new { someResources.Id, someResources.Type, someResources.Name });
else if(isUrl)
return Ok(new { someResources.Id, someResources.Url, someResources.Name });
}
Anonymous types are a bit harder to work with because the compiler writes the class for you, so it's tricky to do things like declare return types from methods if the method is returning an AT.. But if you're using it as some fill-in all within one method, such as a "make this and serialize it", they work well..
I think your approach is not the right one here. I tend to follow more general OO guidelines in this situation (note, some consider these a bit dated, and other solutions exist. But they are still commonly used)
You write against an interface. So let's see what you want... A guid, type and name. All other deatils aren't important.
public interface IResourceDetails
{
public Guid Id { get; }
public string Name { get; }
public string Type { get; }
}
And you can have multiple of these interfaces.
You could then implement the interfaces per type. But I would probably combine them in a base class
public abstract class ResourceBase : IResourceDetails
{
public Guid Id { get; } = new ();
public string Name { get; init; }
public string Type { get; }
public ResourceBase(string type)
{
Type = type;
}
}
Each resource type would have it's own implementation
public class FileResource : ResourceBase
{
public FileResource() : base("File") { }
// File-specific properties.
public string Description { get; init; }
public DateTime? Createdon { get; init; }
}
The response method then could be made generic and look like this
public static IActionResult Response(IResourceDetails resource)
{
return Ok(new
{
resource.Id,
resource.Name,
resource.Type,
});
}

Converting infinitely nested objects in .NET Core

EDIT: I originally worded this question very poorly, stating the problem was with JSON serialization. The problem actually happens when I'm converting from my base classes to my returned models using my custom mappings. I apologize for the confusion. :(
I'm using .NET Core 1.1.0, EF Core 1.1.0. I'm querying an interest and want to get its category from my DB. EF is querying the DB properly, no problems there. The issue is that the returned category has a collection with one interest, which has one parent category, which has a collection with one interest, etc. When I attempt to convert this from the base class to my return model, I'm getting a stack overflow because it's attempting to convert the infinite loop of objects. The only way I can get around this is to set that collection to null before I serialize the category.
Interest/category is an example, but this is happening with ALL of the entities I query. Some of them get very messy with the loops to set the relevant properties to null, such as posts/comments.
What is the best way to address this? Right now I'm using custom mappings that I wrote to convert between base classes and the returned models, but I'm open to using any other tools that may be helpful. (I know my custom mappings are the reason for the stack overflow, but surely there must be a more graceful way of handling this than setting everything to null before projecting from base class to model.)
Classes:
public class InterestCategory
{
public long Id { get; set; }
public string Name { get; set; }
public ICollection<Interest> Interests { get; set; }
}
public class Interest
{
public long Id { get; set; }
public string Name { get; set; }
public long InterestCategoryId { get; set; }
public InterestCategory InterestCategory { get; set; }
}
Models:
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
public List<InterestModel> Interests { get; set; }
}
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
public InterestCategoryModel InterestCategory { get; set; }
public long? InterestCategoryId { get; set; }
}
Mapping functions:
public static InterestCategoryModel ToModel(this InterestCategory category)
{
var m = new InterestCategoryModel
{
Name = category.Name,
Description = category.Description
};
if (category.Interests != null)
m.Interests = category.Interests.Select(i => i.ToModel()).ToList();
return m;
}
public static InterestModel ToModel(this Interest interest)
{
var m = new InterestModel
{
Name = interest.Name,
Description = interest.Description
};
if (interest.InterestCategory != null)
m.InterestCategory = interest.InterestCategory.ToModel();
return m;
}
This is returned by the query. (Sorry, needed to censor some things.)
This is not .NET Core related! JSON.NET is doing the serialization.
To disable it globally, just add this during configuration in Startup
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}));
edit:
Is it an option to remove the circular references form the model and have 2 distinct pair of models, depending on whether you want to show categories or interests?
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
public List<InterestModel> Interests { get; set; }
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
}
}
public class InterestModel
{
public long Id { get; set; }
public string Name { get; set; }
public InterestCategoryModel InterestCategory { get; set; }
public class InterestCategoryModel
{
public long Id { get; set; }
public string Name { get; set; }
}
}
Note that each of the models has a nested class for it's child objects, but they have their back references removed, so there would be no infinite reference during deserialization?

Umbraco - Map strong-typed object to IPublishedContent

I'm 'conjoining' Umbraco with another web application and using Elasticsearch to store data from both applications in one index. So far, I've been only searching through Umbraco content, getting its ID and then using UmbracoHelper to retrieve the proper IPublishedContent item, which then I mapped to a strong-typed object [Class1], which inherits from PublishedContentModel abstract class, using Ditto.
Currently in the Umbraco application, the Class1 object is being serialized with JSON.Net and output and I can't change the structure of it. With addition of the items coming from another, non-Umbraco application I wanted to completely omit the step of retrieving the content from Umbraco and instead hold all of the relevant data directly in the index. That way I could just create strong typed objects based on the content of the index. However, after remapping the item to it's DTO [Class1DTO], I found myself unable to map those onto the Class1.
Obviously, I cannot use Ditto anymore, as it only works when mapping from IPublishedContent and it's derivatives. I was thinking about using AutoMapper, but the problem is, I cannot instantiate my Class1, without passing IPublishedContent object (because of the need to implement the PublishedContentModel constructor).
I know I could duplicate all of my strong-typed objects without the PublishedContentModel inheritance, but it feels really wrong doing that. Is there a way to somehow fake the IPublishedContent object, so that I could use AutoMapper? Or is there any other way of achieving the same output?
Example classes below:
[JsonObject(MemberSerialization.OptIn)]
public class Class1 : PublishedContentModel
{
public Class1(IPublishedContent content)
: base(content)
{
}
[JsonProperty]
public string type { get; set; }
[JsonProperty]
public override int Id { get { return base.Id; } }
[JsonProperty]
public override string SomeData { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class Class1DTO
{
[JsonProperty]
public string type { get; set; }
[JsonProperty]
public int Id { get; set; }
[JsonProperty]
public string SomeData { get; set; }
[JsonProperty]
public SomeFilter FilterForSearch { get; set; }
}
Couldn't you create your own version of the PublishedContentModel that doesn't have a reliance on IPublishedContent? Something like this:
public abstract class Base
{
public Int32 Id { get; set; }
public String Name { get; set; }
public String Path { get; set; }
public IList<Int32> PathIds
{
get
{
return !String.IsNullOrEmpty(Path) ? Path.Split(',').Select(x => Convert.ToInt32(x)).ToList() : null;
}
}
public Int32 ParentId
{
get
{
if (PathIds.HasItemsAndNotNull() && PathIds.Count >= 2)
return PathIds[PathIds.Count - 2];
return -1;
}
}
public Int32 Level { get; set; }
public DateTime CreateDate { get; set; }
public DateTime UpdateDate { get; set; }
public String WriterName { get; set; }
public String DocumentTypeAlias { get; set; }
}
Unless you have a need of very specific fields that are on PublishedContentModel.

WebAPI not serializing inherited class on POST?

Here is my controller method:
[System.Web.Http.HttpPost]
[System.Web.Http.Route("api/exercise")]
public HttpResponseMessage CreateExercise(ExerciseDto exercise) {
Here are my classes:
public class Exercise {
[Key]
[Required]
public int ExerciseId { get; set; }
[StringLength(300, ErrorMessage = "The value cannot exceed 300 characters. ")]
public string Title { get; set; }
}
[NotMapped]
[Serializable]
public class ExerciseDto : Exercise {
public ExerciseDto(Exercise exercise) {
ExerciseId = exercise.ExerciseId;
Title = exercise.Title;
UserHasExercise = true;
}
public bool UserHasExercise { get; set; }
public List<int> SomeIds { get; set; }
}
If I use type Exercise in the API controller, the object comes through. I created the DTO to extend the POCO with some more properties, but if I use this ExerciseDto class, I get null whenever I send the same data I was sending before. What is happening?
WebAPI Config:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
config.Formatters.Remove(config.Formatters.XmlFormatter);
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter();
json.SerializerSettings.Converters.Add(enumConverter);
Update:
My solution for the time being is to scrap the idea of DTOs altogether and just extend the POCO with [NotMapped] properties:
public class Exercise {
[Key]
[Required]
public int ExerciseId { get; set; }
[StringLength(300, ErrorMessage = "The value cannot exceed 300 characters. ")]
public string Title { get; set; }
[NotMapped]
public bool UserHasExercise { get; set; }
[NotMapped]
public List<int> SomeIds { get; set; }
}
This keeps everything super simple, but I have a feeling it's not a best practice for more complicated models. Am very interested in seeing the proper way to handle this.
ExerciseDto needs a parameterless constructor in order for the Post body deserialize properly.
Update: your update is the way I would do it.
I believe the problem is that you've declared ExerciseDto as [Serializable] but you're not passing all ExerciseDto model parameters in your request (you mentioned that you pass the same data as you did for Exercise). You can either remove the [Serializable] attribute or, alternatively, add [JsonIgnore] to your model's UserHasExercise and SomeIds parameters. If you're passing the data as xml and not json, then use [IgnoreDataMember] instead of [JsonIgnore].

how to map from a domain model to a view model from MVC controller with AutoMapper

I am trying to figure out how AutoMapper works in creating a map from the domain model to the view model with a complex collection.
Within my domain model (Search.Domain) ,
I have the following:
namespace Search.Domain.Model
{
public class Result
{
public int SearchTime { get; set; }
public List<ResultDetails> Context { get; set; }
}
public class ResultDetails
{
public string Entity { get; set; }
public string Jurisdiction { get; set; }
public DateTime DateReported { get; set; }
public string Description { get; set; }
public DateTime DateEntered { get; set; }
public string AssociatedLink { get; set; }
public int Relevance { get; set; }
}
}
with the MVC project (Search.WebUI) I have the following:
namespace Search.WebUI.Models
{
public class ResultViewModel
{
public int SearchTime { get; set; }
public List<ResultDetails> Context { get; set; }
}
public class ResultDetails
{
public string Entity { get; set; }
public string Jurisdiction { get; set; }
public DateTime DateReported { get; set; }
public string Description { get; set; }
public DateTime DateEntered { get; set; }
public string AssociatedLink { get; set; }
public int Relevance { get; set; }
}
}
Within the controller (HomeController.cs)
namespace Search.WebUI.Controllers
{
public class HomeController : Controller
{
private ISearchResultManager sr = new ResultManager();
public ActionResult Index()
{
ResultViewModel searchresults;
var results = sr.GetSearchResults(5);
Mapper.CreateMap<Search.Domain.Model.Result, ResultViewModel>();
searchresults = Mapper.Map<Search.Domain.Model.Result, ResultViewModel>(results);
return View("Home", searchresults);
}
}
}
The error message that is being generated when run is:
Missing type map configuration or unsupported mapping.
Mapping types:
ResultDetails -> ResultDetails
Search.Domain.Model.ResultDetails -> Search.WebUI.Models.ResultDetails
Destination path:
ResultViewModel.Context.Context.Context0[0]
Source value:
Search.Domain.Model.ResultDetails
In looking at this it appears that the nested List<ResultDetails> is causing an issue but I don't know what I am supposed to do to handle this type of mapping.
Is it correct to go all the way back into the domain for reference to the type? This seems as if I am pulling the domain into the UI which I would not want to do?
Is there another option for mapping domain models to view models in the UI? Basically I was hoping to have a view model within the UI that I could extend beyond the domain model for UI purposes and not put a reference to the domain model.
I am new to autoMapper so this entire thing may be wrong? I would appreciate any suggestions or guidance.
AutoMapper doesn't look at all potential child mapping when mapping a containing class. You need to explicitly add a mapping for the ResultDetails as well:
Mapper.CreateMap<Search.Domain.Model.Result.ResultDetails,
Search.WebUI.Models.ResultDetails>();

Categories