I have an Entity called Artist that looks like this:
public class Artist
{
public int Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string UrlFriendly { get; set; }
}
And my Artist View Model looks exactly the same, except it is named ArtistVM.
Notice the UrlFriendly property. I want this property to be generated through code. I would like all letters with accents ... etc to be replaced with their English equivalent and also to be turned into lower case.
For example the name Édith Piaf would become edith-piaf.
Ignoring Automapper and the accented letters, I know how to do the rest like so:
artist.FName.ToLower() + "-" + artist.LName.ToLower();
But is there a way I can configure AutoMapper Mappings to do this for the UrlFriendly property when mapping from ArtistVM to Artist?
Related
I have attributes in a c# file like so, all are gauranteed to be on their own line
[DataType("Checkbox")]
public bool SomeCheckBox { get; set; }
[DataType("Text")]
public string SomeText { get; set; }
Using VSVIM I want to do a search and replace so that the code looks like this
[DataType(Control.Checkbox)]
public bool SomeCheckBox { get; set; }
[DataType(Control.Text)]
public string SomeText { get; set; }
I tested this pattern in regex101 and it seemed to worked, but can't seem to translate it into VSVIM. VSVIM says the pattern didn't match anything.
%s/(?<=\[DataType\(").*?(?="\)\])/[DataType(Control.\1)]/g
Basically I want to remove the quotes and prefix the second capture group with Control..
I am running into an issue while looking at SS.
I am writing a custom Stripe implementation and got stuck on web hooks, this in particular:
https://stripe.com/docs/api#event_object
data->object - this can be anything.
Here is my DTO for it:
public class StripeEvent
{
public string id { get; set; }
public StripeEventData data { get; set; }
public string type { get; set; }
}
[DataContract]
public class StripeEventData
{
[DataMember(Name = "object")]
public object _object { get; set; }
}
My hope is to basically just get that object as a string, and then parse it:
var invoice = (StripeInvoice)JsonSerializer.DeserializeFromString<StripeInvoice>(request.data._object.ToString());
Unfortunately the data that is returned from ToString does not have quotes surrounding each json property's name:
Capture
So, the DeserializeFromString returns an object that has everything nulled out.
Why does SS internally strip the quotes out? Is this the proper way to handle a json member that can be one of many different types? I did try the dynamic stuff, but did not have any luck with that either - basically the same result with missing quotes.
I searched very thoroughly for the use of objects and dynamic within DTOs, but there really was nothing that helped with this question.
Thank you!
The issue is that you should never have an object type in DTOs as the serializer has no idea what concrete type to deserialize back into.
The Stripe documentation says object is a hash which you should be able to use a Dictionary to capture, e.g:
public class StripeEventData
{
public Dictionary<string,string> #object { get; set; }
}
Or as an alternative you could use JsonObject which provides a flexible API to access dynamic data.
This will work for flat object structures, but for complex nested object structures you'll need to create Custom Typed DTOs, e.g:
public class StripeEventInvoice
{
public string id { get; set; }
public StripeEventDataInvoice data { get; set; }
public string type { get; set; }
}
public class StripeEventData
{
public StripeInvoice #object { get; set; }
}
I am using these classes:
public class MasteryPages
{
internal MasteryPages() { }
[JsonProperty("pages")]
public List<MasteryPage> Pages { get; set; }
[JsonProperty("summonerId")]
public long SummonerId { get; set; }
}
[Serializable]
public class MasteryPage
{
internal MasteryPage() { }
[JsonProperty("current")]
public bool Current { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("talents")]
public List<Talent> Talents { get; set; }
}
[Serializable]
public class Talent
{
internal Talent() { }
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("rank")]
public int Rank { get; set; }
}
This is the code I'm using to deserialise the object
//MASTERIES
var jsonMasteries = requester.CreateRequest(string.Format(RootUrl, Region) + string.Format(MasteriesUrl, summonerId));
var objAllMasteryPages = JsonConvert.DeserializeObject<MasteryPages>(jsonMasteries);
The jsonMasteries object is correctly serialized and gives me this:
http://pastebin.com/3dkdDHdx (Rather large, to view easily: go to http://www.jsoneditoronline.org/ and paste it)
The second line is giving me troubles however. Normally my object should be filled with the data. It unfortunately isn't and I have no idea what's wrong.
Anyone could help me out?
Your problem is in this part of serialized JSON: "42177333": { ... }
As I understand - this is some kind of ID and it's dynamic.
Possible solutions are:
One of possible resolutions is here: C# deserialize dynamic JSON
Cut this part of dynamic JSON.
Try to modify the serialization stuff to avoid this dynamic ID.
Thanks to sleepwalker I saw what was wrong. (Dynamic Id (number), first line)
Now, the James Newtonking JSON library has a solution for dynamic id's like this.
I edited my code to this:
var jsonMasteries = requester.CreateRequest(string.Format(RootUrl, Region) + string.Format(MasteriesUrl, summonerId));
var objAllMasteriePages = JsonConvert.DeserializeObject<Dictionary<long, MasteryPages>>(jsonMasteries).Values.FirstOrDefault().Pages;
(First line stays the same, the magic is in the second line)
Now, i use a dictionary with the key being my given Id, and my custom class.
This works wonders
I have a model...
public class PatientACOModel
{
public int EncounterId { get; set; }
public int PatientId { get; set; }
public int EMPIID { get; set; }
public int PopulationPatientID { get; set; }
public string EditAddOrEditCurrent { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
//[UIHint("_PhoneNumFormatter")]
public string Phone { get; set; }
}
I want to specifically format my phone numbers. I just want to put a UIHint over my phone number and possibly other phone numbers. I don't want all strings to be formatted.
I am trying things like this...
#Model String
#if (Model != null) {
String.Format("{0: (###) ###-####}", double.Parse(Model.ModelMetadata.Get));
}
That would be the display template referenced in my UIHint that is commented out.
When I do that, none of my strings show up. What am I doing wrong?
In my display template how do I get the string to parse and then Format?
First I would check if the _PhoneNumFormatter is defined in one of the following locations:
~/Views/Shared/DisplayTemplates/_PhoneNumFormatter.cshtml
~/Views/<Controller>/DisplayTemplates/_PhoneNumFormatter.cshtml
Next I think you wanted to format the string value directly from the Model property:
#model String
#if (!String.IsNullOrWhiteSpace(Model))
{
#String.Format("{0: (###) ###-####}", Convert.ToInt64(Model))
}
This should work fine with the UIHint attribute, I've just checked in a sample application.
Code assumes that numbers in your region don't start with a leading 0 and that the Model property will contain strings containing only numeric characters, if not you should improve the Convert.ToInt64 part.
Hope it helps.
I have a table of links, and some links will be child links, referencing the parent links ID
however i can not get my head around servicestack ormlite and populating a property of children, will all the child links when getting a list of all links.
Here is my model:
public partial class Navigation
{
[Alias("Id"), AutoIncrement]
public int Id { get; set; }
[Alias("ParentId")]
[Display( Name = "ParentId")]
[References(typeof(Navigation))]
public int? ParentId { get; set; }
[Alias("LinkText")]
[StringLength(50, ErrorMessage = " Must be no more than 50 characters long!")]
[Display( Name = "LinkText")]
public string LinkText { get; set; }
[Alias("Action")]
[StringLength(50, ErrorMessage = " Must be no more than 50 characters long!")]
[Display( Name = "Action")]
public string Action { get; set; }
[Alias("Controller")]
[StringLength(50, ErrorMessage = " Must be no more than 50 characters long!")]
[Display( Name = "Controller")]
public string Controller { get; set; }
[Alias("Area")]
[StringLength(50, ErrorMessage = " Must be no more than 50 characters long!")]
[Display( Name = "Area")]
public string Area { get; set; }
[Alias("Visible")]
[Display( Name = "Visible"),Required(ErrorMessage = " is required" )]
public bool Visible { get; set; }
[Alias("Sequence")]
[Display( Name = "Sequence")]
public int? Sequence { get; set; }
[ForeignKey(typeof(Navigation))]
public virtual ICollection<Navigation> Children { get; set; }
}
any ideas ?
You can do that using inheritance. The parent class will contain a reference to the child class. I had to use it to get which user have created each user. Here is a sample:
public class UserCommon
{
[References(typeof(User))] // Self reference workaround for User ;)
public Guid CreatedBy { get; set; }
}
public class User : UserCommon
{
public Guid Uid { get; set; }
public String Username { get; set; }
public String Password { get; set; }
}
Something you need to pay attention to is to include the Id in the child class not the parent. The table that will be generated is as follow. The foreign key is a self reference
Getting the list of children should be as easy as a simple LINQ query that will fetch all children for a certain parent Guid. CreatedBy is also a property of User becuase of inheritance.
db.Select<User>(q => q.CreatedBy == '734FD814-024D-4795-AFD0-34FECF89A13A');
// Just a sample Guid, you should be able to select
// the Guid you need and insert it here.
Tables in OrmLite are strictly a 1:1 mapping with the underlying db tables.
This means all complex type properties are blobbed into a db text field with the property name, they're never used to auto-map to child relations as you're expecting to do here.
Here's an early answer that shows how you could map many to many relations with OrmLite.