Serialize DateTime splitting date and time - console-application

Imagine an instance of this class with the time of May 27th 2020, 2:02 PM
public class Example
{
public DateTime When{ get; set; }
}
I want to serialize the property When like this
{
"date": "2020-05-27",
"time": "14:02"
}
How do I serialize this using class attributes ? And how do I deserialize it? Do I need to change my class to work with this json?

Related

Sort a list of different objects with the same property (timestamp) in Flutter/Dart

I want to sort by date a List which contains 2 different objects (ClassA and ClassB), with the same property timestamp "createdAt". I have tried this solution :
_list.sort((a, b) => a.createdAt.compareTo(b.createdAt));
It only works when _list contains a single type of objects (ClassA or ClassB) but not with both.
Anyone has an idea ?
Thank you.
Solution : create an abstract class with createdAt property and implement it on childs
I think the problem is that you have a list with dynamic type. Therefore I would recommend creating an abstract class that contains both information from ClassA and ClassB, so that the dart compiler understands.
List<Parent> _list = [
ClassA(DateTime(2020, 04, 04)),
ClassB(DateTime(2020, 03, 04)),
ClassA(DateTime(2020, 02, 04)),
ClassB(DateTime(2020, 01, 04))
];
_list.sort((a,b)=> b.createdAt.compareTo(a.createdAt));
abstract class Parent {
DateTime createdAt;
}
class ClassA implements Parent {
DateTime createdAt;
ClassA(this.createdAt);
}
class ClassB implements Parent {
DateTime createdAt;
ClassB(this.createdAt);
}
Here is also a CodePen where I could sort the list.
https://codepen.io/md-weber/pen/RwWaMgz

GlassMapper SC nullable boolean fields causing Failed item resolve

We have recently upgraded from Glass.Sitecore.Mapper 2.0.12.0 to Glass.Mapper.Sc 3.3.1.53 (we can't upgrade any higher as we are using Sitecore 6).
We are having problem with saving items with nullable boolean fields
i.e.
[SitecoreType(TemplateId = Templates.Category)]
public class Category : AggregateBase
{
...
[SitecoreField(FieldName = CategoryFields.Requires360)]
public virtual bool? Requires360 { get; set; }
[SitecoreField(FieldName = CategoryFields.RequiresCatwalk)]
public virtual bool? RequiresCatwalk { get; set; }
}
Where AggregateBase contains the standard sitecore fields
e.g.
[SitecoreType]
public class AggregateBase
{
[SitecoreId]
public virtual Guid Id { get; set; }
[SitecoreInfo(SitecoreInfoType.Name)]
public virtual string Name { get; set; }
...
}
If the nullable bool field is null it saves without issue.
If the nullable field has a value we get the following exception
"Failed item resolve - You cannot save a class that does not contain a
property that represents the item ID. Ensure that at least one
property has been marked to contain the Sitecore ID. Type:
System.Boolean"
This configuration worked previously and seems to work with other nullable types e.g. int?
The underlying storage in the template field is "Single-Line Text".
Does anyone have any ideas?
Regards,
Mark

Windows Phone ListView object with list

I have a object which contains 5 list of object, and I would like to display this object in only one listview, I tried a lot of different things, like groups, but I can't find a solution. This is my object :
public class SearchResult
{
public List<User> artist { get; set; }
public List<User> user { get; set; }
public List<Music> music { get; set; }
public List<Album> album { get; set; }
public List<Pack> pack { get; set; }
}
How can I bind 5 list and display the information a need for each one by using a Template.
Thanks.
So you are looking for something like the Search results in the XBox Music app?
It is probably just multiple ListViews, but if you really want all of them in a single ListView, you have to:
Put your lists together in a ListOfLists (List>), so you can use it with CollectionViewSource.
Or: Implement IGrouping on your SearchResult.
Then you can just use the GroupStyle on the Listview and ItemTemplateSelector will help you use different Templates on each Type.
So you will have to change your data structure if you want to bind it.

EF6 Code First: How can I centralize content for separate sections of my site?

I want to store all our site's content in one central Content table but relate it to each section of the site. Something like:
Content (for the actual content byte[] and basic info all sections use)
ResearchArticleContent (basically has the related ContentId from the content table and extra cols for info specific to ResearchArticles)
ResearchArticle
ExecutiveContent (basically has the related ContentID from Content table and extra cols for specific data for Executives)
Executive
...and so on.
I'm having trouble understanding the whole code first approach as it pertains to ForeignKeys and InverseProperties. That's the real issue.
So, say I have these two classes as an example:
public class Content
{
[Key]
public int ContentId { get; set; }
public int ContentType { get; set; }
public byte[] ContentBytes { get; set; }
public DateTime AddedDate { get; set; }
[**`InverseProperty or ForeignKey???`**("ResearchArticleContent")]
public virtual ResearchArticleContent ResearchArticleContent { get; set; }
}
and:
public class ResearchArticleContent
{
[Key]
public int ResearchArticleContentId { get; set; }
[ForeignKey("ContentId")]
public virtual Content Content {get;set;}
public int ResearchArticleId { get; set; }
[ForeignKey("ResearchArticleId")]
public virtual ResearchArticle RelatedArticle { get; set; }
}
Where do I put the ForeignKeys / InverseProperties to relate these correctly. Because ideally, I will have Executivecontent, ResearchArticlecontent and so on for each section of the site. (I am following the precedent already laid out in a Data-First prj that I am mimicking so this is the way I have do this, fyi.)
Entity framework requires a type identifier field when you store compound objects in a single table; however, you can get around this pretty easily using views. To use views, create a single content table and a > base < class. Do not apply the TableAttribute data annotation to the base class. All other data annotations are fine.
public class ContentBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ContentId { get; set; }
public string Content { get; set; }
...
}
Then, you can create derived classes that more closely represent the content and apply the TableAttribute data annotation to those. For example,
[Table("ResearchArticleView")]
public class ResearchArticle : ContentBase
{
...you can add more properties here that are included in the view...
...and not necessarily the underlying table, like from a joined table...
...or just use the class as is, so that you have a better name...
}
To use this, set up a view called ResearchArticleView that includes the columns in the base class, as well as any computed or joined columns you want, then add a DbSet to your context that represents the view.
I recommend having content tables for each type of content and then use the method I've described for derived types for each content type. For example, create a base for research articles and a base for execute content. Because, when your database gets big and full of content, having one monolithic content table may cause you backup and optimization issues.

Getting an Internal Link with Glass.Mapper

I've got an Internal Link set up in Sitecore, and I'm trying to map the field using Glass.Mapper, but it just keeps coming back empty, and I'm not sure what I'm doing wrong.
The template in Sitecore is pretty simple:
The Source of the link is set to a folder that only allows content based on the 'System' template to be created.
In my code, I have an object set up:
namespace Playground.GlassObjects
{
public partial class Status
{
public virtual string Description { get; set; }
public virtual string StatusCode { get; set; }
public virtual Glass.Mapper.Sc.Fields.Link System { get; set; }
}
}
Which is being used basically like this:
public void DoStuff(Sitecore.Data.Items.Item item)
{
var status = item.GlassCast<Status>();
this.DoOtherStuff(status);
}
What I'm running into is glassObj.Description, and glassObj.StatusCode are being wired up exactly like I want/expect, but glassObj.System is not.
Can anyone tell me what I'm doing wrong here? I'm at a loss right now, with all the magic that's going on behind the scenes.
The Glass.Mapper.Sc.Fields.Link class is designed to work with the General Link field. The internal link field stores values as paths e.g /sitecore/content/home/events. This means it isn't compatible with the Link class.
Instead you should map it to another class you have created.
public partial class Status
{
public virtual string Description { get; set; }
public virtual string StatusCode { get; set; }
public virtual MySystem System { get; set; }
}
public class MySystem{
public virtual string Url { get; set; }
public virtual string MyField { get; set; }
}
Fast forward to 2022 Internal Link field seems to be working with Glassmapper without any extra effort. All you have to do is add Internal Link case to GlassGenerator.tt file on the project where you will generate the template.
This will ensure your model will have Link field like this:
[SitecoreField(FieldId = "{D2CF138A-0A1C-4766-B250-F56E9458B624}")]
Link InternalLinkField{ get; set; }
It will have some info populated and most of the other properties will be null. The ones that will help you are:
Url (full path to the internal link item)
TargetId (ID of the internal link item)
Here are the available properties:
There is an alternative to that, you can get the link from fields like this:
yourGlassItem.Item.Fields["InternalLinkFieldName"]
You will get the entire Internal link Item. You can use Value or InheritedValue property to get the path of the linked item.