big OOF
big OOF
CC#
Created by big OOF on 4/11/2023 in #help
❔ QuestionController
Souns like a good idea! I have a endpoint for bith Store and item, lets say i have a webpage that allows the user to add a new store and the stores items. Would you use two seperate calls(POST store and POST Item) or would you create anew POST-endpint? In that case it would be only one endpoint instead of two but i would have to seperate the POST-body to be able to insert it to the Store and Item table in the DB 🙂
9 replies
CC#
Created by big OOF on 3/28/2023 in #help
❔ Dev/prod databse
@BananaPie Sounds like a good idea! Im a little bit confused - do you mean that you push your database-changes? 🙂
6 replies
CC#
Created by big OOF on 3/14/2023 in #help
❔ Dezerialise JSON response
I just created the JSON above as an example but in relity it do come from an API. When creating the Perosn class i used "paste as JSON" in visual studio 🙂
10 replies
CC#
Created by big OOF on 2/24/2023 in #help
❔ Retrieve API data in C#
I think i understand the concept, i just got it twisted! In the case of adding a singleton: 1. Add singleton to service 2. Pass singleton to class via DI
services.AddSingleton<Thing1>();
services.AddSingleton<Thing1>();
class Stuff
{
private readonly Thing1 _thing1;
private readonly Thing2 _thing2;
private readonly Thing3 _thing3;
public Stuff(IThing1 thing1, IThing2 thing2, IThing3 thing3)
{
_thing1 = thing1;
_thing2 = thing2;
_thing3 = thing3;
}
}
class Stuff
{
private readonly Thing1 _thing1;
private readonly Thing2 _thing2;
private readonly Thing3 _thing3;
public Stuff(IThing1 thing1, IThing2 thing2, IThing3 thing3)
{
_thing1 = thing1;
_thing2 = thing2;
_thing3 = thing3;
}
}
I think it was the case with singleton that got me confused because using DI doesn't look to different to declaring it like:
private static Thing1 instance = new Thing1();
private static Thing1 instance = new Thing1();
If you understand what i mean 🙂 But i understand the value of having it in the container instead of declaring it in the class. Thanks!
20 replies
CC#
Created by big OOF on 2/24/2023 in #help
❔ Retrieve API data in C#
Thanks for all the responses! Indeed a great read @pip, but i do have one question regarding the article: He adds services like this: services.AddSingleton<GetAllProjectsQuery>(); services.AddHttpClient<GitHubClient>(x => { x.BaseAddress = new Uri(GitHubConstants.ApiBaseUrl); }); services.AddSingleton<GitHubClientFactory>(); services.AddSingleton<JsonSerializer>(); The only reason you declare the singletons like above is so that you can access it via DI and dont have to instantiate a new class? For example, This: services.AddSingleton<GetAllProjectsQuery>(); Replaces: private readonly GetAllProjectsQuery _getAllProjectsQuery; Am i understanding it correctly? 🙂
20 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
Is there a way to save this help threads ion discord? 🙂
86 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
I that all my questions got answered! I want you to know you made my day - cant thank you enough @Pobiega!
86 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
Ah yes!
86 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
Perfect i think i understand the concept now! So EF knows "under the hood" that when we are querying the ICollection entity in the object it will look to the join table? 🙂
86 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
Ah okey, and in that case EF creates for example a PersonGroup table, a join table
86 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
Okey now i see the reasoning behind the DTO, thanks @Pobiega! One additional question regarding EF: This generates a field "GroupId" in the persons table
cs
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group{ get; set; }
}
cs
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group{ get; set; }
}
While this dosent
cs
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Group> Group{ get; set; }
}
cs
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Group> Group{ get; set; }
}
I want to use the latter i think (because a group can hold multiple persons) but how will i ever be able to query Person to include group if the Person object dosent hold a reference to the group object/class/table? 🙂 Or do a DTO solve this problem and im maybe a bit slow? 🙂
86 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
@Pobiega Thank you, it worked when i changed from ICollection<Group> to just Group but when im using ICollection i get no data at all. I also tried to add a relation in the Group class:
cs
public class Group
{
public long Id { get; set; }
public string? GroupName { get; set; }
public ICollection<Person> Person { get; set; }
}
}

cs
public class Group
{
public long Id { get; set; }
public string? GroupName { get; set; }
public ICollection<Person> Person { get; set; }
}
}

Sadly i get a object cycle error, do i understand it incorrectly or do u just need to alter my LinQ select: var a = await _context.Person.Include(x => x.Group).ToListAsync(); Thanks in advance 🙂
86 replies
CC#
Created by big OOF on 1/9/2023 in #help
❔ Architecture
@ChucklesTheBeard okey thanks! A follow-up question, i have a model class that I'm using when dezerializing retrieved JSON from an API. From what i understand you never write function into the model class? - It should only contain the model 🙂
6 replies
CC#
Created by big OOF on 11/25/2022 in #help
✅ MS webshop example
8 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
Okey perfect, then i think i understand. Cant thank you enough, great educator, great answers and maybe even greater patience with a noob like me 😉 you made my day!
20 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
Okey, so in a way this solution is using two DI-containers? IserviceCollection and autofac or is autofac an extension of IserviceCollection? Another question since im not that experienced with lambda expressions. If you were to transform the code below into words, can you say: - When something is registred, is it of type IOrderQueries -> return a OrderQueries object? 🙂
builder.Register(c => new OrderQueries(QueriesConnectionString))
.As<IOrderQueries>()
.InstancePerLifetimeScope();
builder.Register(c => new OrderQueries(QueriesConnectionString))
.As<IOrderQueries>()
.InstancePerLifetimeScope();
20 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
@icebear
20 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
Damn, my mind is blown! Ive had such a hard time grasping the whole DI-container term. Cant thank you enough! Something that still is a bit vague to me is where the conditions or default class is set for the interface. I attached the Startup file and added a comment under CunfigureServices. Im thinking it would look something like this:
//.AddServiceThatKnowsWhichClassAreNeeded(Configuration);

public static IServiceCollection AddServiceThatKnowsWhichClassAreNeeded(this IServiceCollection services, IConfiguration configuration)
{
//Does something that tells the DI which class to return when interface IOrderQueries is called
return services;
}
//.AddServiceThatKnowsWhichClassAreNeeded(Configuration);

public static IServiceCollection AddServiceThatKnowsWhichClassAreNeeded(this IServiceCollection services, IConfiguration configuration)
{
//Does something that tells the DI which class to return when interface IOrderQueries is called
return services;
}
Im having a hard time to really understand everything in the startup file, maybe there is a conditions that ish-does what i mentioned above but that i just cant relly see it with my own eyes. Can you tell? Or maybe i misunderstood something? 🙂
20 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
Ah okey, i think i just realised something! Whenever i add something to IServiceCollection services i can say that i added it to the DI-container, right? For example: .Service.AddMVC just adds the classes that are needed for MVC to the DI-container? -> and are therefore avaliable in whatever class we instantiate it in? 😄
20 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
Thank you!
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{
services
.AddGrpc(options =>
{
options.EnableDetailedErrors = true;
})
.Services
.AddApplicationInsights(Configuration)
.AddCustomMvc()
.AddHealthChecks(Configuration)
.AddCustomDbContext(Configuration)
.AddCustomSwagger(Configuration)
.AddCustomIntegrations(Configuration)
.AddCustomConfiguration(Configuration)
.AddEventBus(Configuration)
.AddCustomAuthentication(Configuration);
//configure autofac

var container = new ContainerBuilder();
container.Populate(services);

container.RegisterModule(new MediatorModule());
container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"]));

return new AutofacServiceProvider(container.Build());
}
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{
services
.AddGrpc(options =>
{
options.EnableDetailedErrors = true;
})
.Services
.AddApplicationInsights(Configuration)
.AddCustomMvc()
.AddHealthChecks(Configuration)
.AddCustomDbContext(Configuration)
.AddCustomSwagger(Configuration)
.AddCustomIntegrations(Configuration)
.AddCustomConfiguration(Configuration)
.AddEventBus(Configuration)
.AddCustomAuthentication(Configuration);
//configure autofac

var container = new ContainerBuilder();
container.Populate(services);

container.RegisterModule(new MediatorModule());
container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"]));

return new AutofacServiceProvider(container.Build());
}
This is from the startup file, my guess is that the logic which class that is "selected"(for example as default) comes from one of these lines, am i correct? 🙂 .AddApplicationInsights(Configuration) .AddCustomMvc() .AddHealthChecks(Configuration) .AddCustomDbContext(Configuration) .AddCustomSwagger(Configuration) .AddCustomIntegrations(Configuration) .AddCustomConfiguration(Configuration) .AddEventBus(Configuration) .AddCustomAuthentication(Configuration); As a example, if you were to change which class is given as default, you would have to look into one of the mentioned above? 🙂
20 replies