Dasic
Dasic
CC#
Created by Dasic on 3/18/2023 in #help
❔ Web API, ASP.NET and Vue.js
Please, can anyone explain for what I should use Web API? I cannot figure out when I should use MVC Controllers and API Controllers. Can I build a whole project without MVC Controllers at all or is it a code smell? I mean something like Vue.js + ASP.NET back-end
8 replies
CC#
Created by Dasic on 3/13/2023 in #help
❔ How can I pass model data to my controller if I have another POCO class in the model?
Let's imagine that I have the classes:
public class Address
{
public int Id { get; set; }
public string? Street { get; set; }
public string? BuildingNumber { get; set; }
public string? Description { get; set; }
public Entrance[]? Entrances { get; set; }
public DateTime LastEdit { get; set; }
}

public class Entrance
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public Floor[]? Floors { get; set; }
}

public class Floor
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
}
public class Address
{
public int Id { get; set; }
public string? Street { get; set; }
public string? BuildingNumber { get; set; }
public string? Description { get; set; }
public Entrance[]? Entrances { get; set; }
public DateTime LastEdit { get; set; }
}

public class Entrance
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public Floor[]? Floors { get; set; }
}

public class Floor
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
}
How can I create a simple editor for these classes? I mean that my Create() method takes something like this:
public async Task<IActionResult> Create([Bind("Id,Street,BuildingNumber,Description,Entrances")] Address address) { ... }
public async Task<IActionResult> Create([Bind("Id,Street,BuildingNumber,Description,Entrances")] Address address) { ... }
Of course I cannot bind Entrances property because it is another class and it is an array. How can I do the trick? What should my controller accept and how can I do it in the view?
11 replies
CC#
Created by Dasic on 12/7/2022 in #help
❔ How to use abstract factories with DI properly?
Everything works fine till the constructors of the realizations are the same. However, I have a sort of dilemma when the constructors are different. Is it okay or maybe there is an architecture issue? Here is the code:
public class CategoryViewFactory : ICategoryViewFactory
{
private readonly ActiveProgressions _activeProgressions;

public CategoryViewFactory(ActiveProgressions activeProgressions)
{
_activeProgressions = activeProgressions;
}

public ICategoryView? Create(CategoryType type, Category category)
{
return type switch
{
CategoryType.Category => new CategoryView(category),
CategoryType.Subcategory => new SubcategoryView(category, _activeProgressions),
_ => null
};
}
}
public class CategoryViewFactory : ICategoryViewFactory
{
private readonly ActiveProgressions _activeProgressions;

public CategoryViewFactory(ActiveProgressions activeProgressions)
{
_activeProgressions = activeProgressions;
}

public ICategoryView? Create(CategoryType type, Category category)
{
return type switch
{
CategoryType.Category => new CategoryView(category),
CategoryType.Subcategory => new SubcategoryView(category, _activeProgressions),
_ => null
};
}
}
ActiveProgression is a singleton service which I inject using the container. Is it okay? And what I should do if, for instance, ActiveProgression is a transient service? How can I create SubcategoryView in this case? Are there any experts in DI?
3 replies
CC#
Created by Dasic on 8/28/2022 in #help
Newtonsoft.Json deserialize issue [Answered]
I have a json string. For example this one:
{"type":"accept","id":0,"ms_id":"00000","user_id":"000000000000","item_name":"Test Item","acc_name":"account"}
{"type":"accept","id":0,"ms_id":"00000","user_id":"000000000000","item_name":"Test Item","acc_name":"account"}
How can I deserialize some fields to a structure field in my class? For instance:
public class Order
{
[JsonProperty("id")] public int Id { get; init; }
[JsonProperty("item_name")] public string ItemName { get; init; }

[JsonProperty("ms_id")] public long MessageId { get; init; }

public Account Account { get; init; }
}

public struct Account
{
[JsonProperty("user_id")] public long UserId { get; init; }
[JsonProperty("acc_name")] public long Nickname { get; init; }
}
public class Order
{
[JsonProperty("id")] public int Id { get; init; }
[JsonProperty("item_name")] public string ItemName { get; init; }

[JsonProperty("ms_id")] public long MessageId { get; init; }

public Account Account { get; init; }
}

public struct Account
{
[JsonProperty("user_id")] public long UserId { get; init; }
[JsonProperty("acc_name")] public long Nickname { get; init; }
}
How can I deserialize the string to put these two fields in an Account field?
9 replies