JasinCole
Well-known member
- Joined
- Feb 16, 2023
- Messages
- 66
- Programming Experience
- 1-3
I got somewhat of an unusual question. It all started around an issue with the Rider plugin for avalonia. I'll try to explain as best I can
In my avalonia xaml code I use the following
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
This was causing the plugin to crash because it was essentially telling me, I had no default constructor. Creating a default constructor solved the plugin issue. But that got me thinking about some other issues.
This code now resides in my VM
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
The question I have is why did rider suggest to use a null supression on the _ctx initialization(there were other options)? How does this now effect my other code that uses DI?
I guess what I am trying to understand is what are the downsides of code like this? I could have
But then I would have had to check for null in all my methods or get more compiler warnings. (trying to weed these out)
Is there anytime that this _ctx would be null if object initialization is done via DI?
Is it bad to have a default constructor if it is never used?
Is there a better way to handle this without going through all the headache of checking for null everywhere?
	
		
			
		
		
	
				
			In my avalonia xaml code I use the following
			
				C#:
			
		
		
		<Design.DataContext>
    <vm:HomePageViewModel/>
</Design.DataContext>This was causing the plugin to crash because it was essentially telling me, I had no default constructor. Creating a default constructor solved the plugin issue. But that got me thinking about some other issues.
This code now resides in my VM
			
				C#:
			
		
		
		private readonly SageDbContext _ctx = null!;
    public HomePageViewModel()
    {
        
    }
    
    public HomePageViewModel(SageDbContext ctx)
    {
     _ctx = ctx;
     InitializeAsync().SafeFireAndForget();
    }The question I have is why did rider suggest to use a null supression on the _ctx initialization(there were other options)? How does this now effect my other code that uses DI?
I guess what I am trying to understand is what are the downsides of code like this? I could have
private readonly SageDbContext? _ctxBut then I would have had to check for null in all my methods or get more compiler warnings. (trying to weed these out)
Is there anytime that this _ctx would be null if object initialization is done via DI?
Is it bad to have a default constructor if it is never used?
Is there a better way to handle this without going through all the headache of checking for null everywhere?
 
	 
 
		 
					
				 
 
		 
 
		 
 
		 
 
		