bakk
❔ Good way to make sure a user is allowed to do something based on context in ASP.NET?
I'm making a website where people can join projects and then post in those projects. Currently, in order to make sure users can only post in projects they're apart of, I pass a user id to every method that eg. handles posting, to let it make sure the user is allowed to do that based on what it gathers from the database. Is this a normal way to do it, or is there some cleaner way?
9 replies
❔ Source code generator with reference to the main project
I have some methods in a project that I want to go through using reflection at compile time to generate some code for them. It seems like source code generators need to be in their own project(?), so I made a project for them in the same solution. The main project uses the generators project. Now, need to access the methods from the main project, but if I reference the main project from the generators project, I would get a circular dependency! Any idea how I would do this?
25 replies
Using dependency injection for internal classes inside a library
I'm making a library, where I feel like it would be useful to be able to use Microsoft's dependency injection library in order to deal with dependencies (both inside the library and letting other projects take advantage of it). However, I'm not quite sure how to deal with internal classes. I have a public class that users of the library would instantiate, and this class contains instances of internal classes or classes containing internal classes. I want to pass these in the various constructors to avoid creating them on the spot everywhere, but things like ActivatorUtilities.CreateInstance fail when they have to call an internal constructor at any point, even if it's inside the project itself. Any ideas how to deal with this? I don't want to make everything public!
18 replies
Testing a class that relies on timers
I have a class
RoutineManager
that uses another class TimerScheduler
to schedule different actions. This means that the timers decide the order of some function calls. I now need to test this. At first, I thought "oh I'll just mock the scheduler and make sure the functions get called", but quickly realised they won't be called in the correct order without the timers (which is the most important thing to test!). Does anyone have any suggestions of what I could do to test this? The only thing I can think of is changing the input to be in milliseconds instead of hours to make it quicker. However, I'm not sure timers are very reliable when you go down to single digit milliseconds or less (could be wrong), so it feels a bit hacky and potentially slow.20 replies