How to design model to accept only integer and Guid? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceIs the use of dynamic considered a bad practice?What is the best practice to deserialize a JSON object with field that might be either string or int?Best way to repeat a character in C#How do I make a textbox that only accepts numbers?URL Encoding using C#How to get string objects instead of Unicode from JSON?Get property value from string using reflection in C#How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?How to Create Deterministic GuidsGoogle Gson - deserialize list<class> object? (generic type)Proper way to return JSON using node or ExpressWhy not inherit from List<T>?
Store Dynamic-accessible hidden metadata in a cell
Does Parliament need to approve the new Brexit delay to 31 October 2019?
How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?
TDS update packages don't remove unneeded items
Was credit for the black hole image misappropriated?
Can the Right Ascension and Argument of Perigee of a spacecraft's orbit keep varying by themselves with time?
Drawing vertical/oblique lines in Metrical tree (tikz-qtree, tipa)
Are spiders unable to hurt humans, especially very small spiders?
Sub-subscripts in strings cause different spacings than subscripts
Single author papers against my advisor's will?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
What can I do if neighbor is blocking my solar panels intentionally?
How did passengers keep warm on sail ships?
What happens to a Warlock's expended Spell Slots when they gain a Level?
Solving overdetermined system by QR decomposition
Why doesn't a hydraulic lever violate conservation of energy?
ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Word for: a synonym with a positive connotation?
What does Linus Torvalds mean when he says that Git "never ever" tracks a file?
Nested ellipses in tikzpicture: Chomsky hierarchy
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Define a list range inside a list
Why are PDP-7-style microprogrammed instructions out of vogue?
How to design model to accept only integer and Guid?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceIs the use of dynamic considered a bad practice?What is the best practice to deserialize a JSON object with field that might be either string or int?Best way to repeat a character in C#How do I make a textbox that only accepts numbers?URL Encoding using C#How to get string objects instead of Unicode from JSON?Get property value from string using reflection in C#How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?How to Create Deterministic GuidsGoogle Gson - deserialize list<class> object? (generic type)Proper way to return JSON using node or ExpressWhy not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want my model to accept all 3 different types of value in id(basically Guid and int only)
This is my model-
public class Base
public dynamic Id get; set;
public string Type get; set;
This is my json-
"id": "31367556-cda7-4fce-9d8a-2cd6f46544f9",
"type": "form"
,
"id": "123",
"type": "form"
,
"id": 456,
"type": "form"
c# .net json serialization json.net
add a comment |
I want my model to accept all 3 different types of value in id(basically Guid and int only)
This is my model-
public class Base
public dynamic Id get; set;
public string Type get; set;
This is my json-
"id": "31367556-cda7-4fce-9d8a-2cd6f46544f9",
"type": "form"
,
"id": "123",
"type": "form"
,
"id": 456,
"type": "form"
c# .net json serialization json.net
Probably a duplicate of this stackoverflow.com/a/42334643/224370 with the added Guid option.
– Ian Mercer
Mar 22 at 6:10
add a comment |
I want my model to accept all 3 different types of value in id(basically Guid and int only)
This is my model-
public class Base
public dynamic Id get; set;
public string Type get; set;
This is my json-
"id": "31367556-cda7-4fce-9d8a-2cd6f46544f9",
"type": "form"
,
"id": "123",
"type": "form"
,
"id": 456,
"type": "form"
c# .net json serialization json.net
I want my model to accept all 3 different types of value in id(basically Guid and int only)
This is my model-
public class Base
public dynamic Id get; set;
public string Type get; set;
This is my json-
"id": "31367556-cda7-4fce-9d8a-2cd6f46544f9",
"type": "form"
,
"id": "123",
"type": "form"
,
"id": 456,
"type": "form"
c# .net json serialization json.net
c# .net json serialization json.net
asked Mar 22 at 5:41
tushar parashartushar parashar
1
1
Probably a duplicate of this stackoverflow.com/a/42334643/224370 with the added Guid option.
– Ian Mercer
Mar 22 at 6:10
add a comment |
Probably a duplicate of this stackoverflow.com/a/42334643/224370 with the added Guid option.
– Ian Mercer
Mar 22 at 6:10
Probably a duplicate of this stackoverflow.com/a/42334643/224370 with the added Guid option.
– Ian Mercer
Mar 22 at 6:10
Probably a duplicate of this stackoverflow.com/a/42334643/224370 with the added Guid option.
– Ian Mercer
Mar 22 at 6:10
add a comment |
1 Answer
1
active
oldest
votes
First of all you need to answer on the following questions: why you really need dynamic type? How are you going to use it?. After that you can choose approach to implement. Maybe you need just string.
Another approach, is to use to 2 properties and for example enum which indicate for you which id should be selected.
Make your Data model which accept string
public class Base
public string Id get; set;
public string Type get; set;
And after you received and parse it you need to map this Data Transfer Model to Model and use it in your Application only Model types.
Mapping can be
//your ApplicationModel
public class BaseModel
public Guid GuidIdget;set;
public int Idget;set;
public IdTypeget;set;
public enum IdType
Guid,
Int
public BaseModel Map(Base dto)
var model=new BaseModelType= dto.Type;
var guidResult = Guid.TryParse(dto.Id,out model.GuidId);
if(!guidResult) int.TryParse(dto.Id, out model.Id);
model.IdType = guidResult?IdType.Guid:IdType.Int;
return model
using dynamic type for me is Bad practice. I know only few cases where it acceptable.
More info you can find here:Is the use of dynamic considered a bad practice?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293515%2fhow-to-design-model-to-accept-only-integer-and-guid%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all you need to answer on the following questions: why you really need dynamic type? How are you going to use it?. After that you can choose approach to implement. Maybe you need just string.
Another approach, is to use to 2 properties and for example enum which indicate for you which id should be selected.
Make your Data model which accept string
public class Base
public string Id get; set;
public string Type get; set;
And after you received and parse it you need to map this Data Transfer Model to Model and use it in your Application only Model types.
Mapping can be
//your ApplicationModel
public class BaseModel
public Guid GuidIdget;set;
public int Idget;set;
public IdTypeget;set;
public enum IdType
Guid,
Int
public BaseModel Map(Base dto)
var model=new BaseModelType= dto.Type;
var guidResult = Guid.TryParse(dto.Id,out model.GuidId);
if(!guidResult) int.TryParse(dto.Id, out model.Id);
model.IdType = guidResult?IdType.Guid:IdType.Int;
return model
using dynamic type for me is Bad practice. I know only few cases where it acceptable.
More info you can find here:Is the use of dynamic considered a bad practice?
add a comment |
First of all you need to answer on the following questions: why you really need dynamic type? How are you going to use it?. After that you can choose approach to implement. Maybe you need just string.
Another approach, is to use to 2 properties and for example enum which indicate for you which id should be selected.
Make your Data model which accept string
public class Base
public string Id get; set;
public string Type get; set;
And after you received and parse it you need to map this Data Transfer Model to Model and use it in your Application only Model types.
Mapping can be
//your ApplicationModel
public class BaseModel
public Guid GuidIdget;set;
public int Idget;set;
public IdTypeget;set;
public enum IdType
Guid,
Int
public BaseModel Map(Base dto)
var model=new BaseModelType= dto.Type;
var guidResult = Guid.TryParse(dto.Id,out model.GuidId);
if(!guidResult) int.TryParse(dto.Id, out model.Id);
model.IdType = guidResult?IdType.Guid:IdType.Int;
return model
using dynamic type for me is Bad practice. I know only few cases where it acceptable.
More info you can find here:Is the use of dynamic considered a bad practice?
add a comment |
First of all you need to answer on the following questions: why you really need dynamic type? How are you going to use it?. After that you can choose approach to implement. Maybe you need just string.
Another approach, is to use to 2 properties and for example enum which indicate for you which id should be selected.
Make your Data model which accept string
public class Base
public string Id get; set;
public string Type get; set;
And after you received and parse it you need to map this Data Transfer Model to Model and use it in your Application only Model types.
Mapping can be
//your ApplicationModel
public class BaseModel
public Guid GuidIdget;set;
public int Idget;set;
public IdTypeget;set;
public enum IdType
Guid,
Int
public BaseModel Map(Base dto)
var model=new BaseModelType= dto.Type;
var guidResult = Guid.TryParse(dto.Id,out model.GuidId);
if(!guidResult) int.TryParse(dto.Id, out model.Id);
model.IdType = guidResult?IdType.Guid:IdType.Int;
return model
using dynamic type for me is Bad practice. I know only few cases where it acceptable.
More info you can find here:Is the use of dynamic considered a bad practice?
First of all you need to answer on the following questions: why you really need dynamic type? How are you going to use it?. After that you can choose approach to implement. Maybe you need just string.
Another approach, is to use to 2 properties and for example enum which indicate for you which id should be selected.
Make your Data model which accept string
public class Base
public string Id get; set;
public string Type get; set;
And after you received and parse it you need to map this Data Transfer Model to Model and use it in your Application only Model types.
Mapping can be
//your ApplicationModel
public class BaseModel
public Guid GuidIdget;set;
public int Idget;set;
public IdTypeget;set;
public enum IdType
Guid,
Int
public BaseModel Map(Base dto)
var model=new BaseModelType= dto.Type;
var guidResult = Guid.TryParse(dto.Id,out model.GuidId);
if(!guidResult) int.TryParse(dto.Id, out model.Id);
model.IdType = guidResult?IdType.Guid:IdType.Int;
return model
using dynamic type for me is Bad practice. I know only few cases where it acceptable.
More info you can find here:Is the use of dynamic considered a bad practice?
edited Mar 22 at 7:01
answered Mar 22 at 6:53
Yauhen SampirYauhen Sampir
22329
22329
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293515%2fhow-to-design-model-to-accept-only-integer-and-guid%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Probably a duplicate of this stackoverflow.com/a/42334643/224370 with the added Guid option.
– Ian Mercer
Mar 22 at 6:10