Dependency Injection Question
I'm working on a music player which can play both local and streamed media. To play the files, I'm using a library which has a SoundPlayer class. (https://lsxprime.github.io/soundflow-docs/getting-started/)
To handle file vs streaming, I'm using a switch statement inside my
SoundEngine
class:
I shouldn't be newing up classes inside my SoundEngine, and want to use DI instead. However, since a user can play different files while the (which must be passed into the _player variable), I can't use constructor injection. Is there another way to handle this?SoundFlow Documentation
Getting Started with SoundFlow
Learn how to install the library, set up your development environment, and write your first SoundFlow application.
25 Replies
can you show more of your codebase?
hard to really tell what's going on if all we can see is a switch statement
Here's my SoundEngine class:
https://paste.mozilla.org/PUT8LaoC
and my Program.cs:
https://paste.mozilla.org/NziKCgHZ
SoundEngine is accessed from a Terminal user interface controlled by a class called Tui
https://paste.mozilla.org/k6fzaXZs
personally, I think I'd change
Play
to take a Stream
and not an object
and if you want di, you can then create a SoundPlayerFactory
with a public SoundPlayer Generate(Stream source) {...}
method
don't know what ePlayerStatus
and eFileType
are, but using lower case values to start type names doesn't follow normal c# conventionsEnums
also, is there a reason you need your own player status enum? why not expose the
PlaybackState
of your sound player?
it would let you simplify your PlayPause
function to do something like:
How can I expose the playback state to the UI? Right now, my UI class takes an IPlayer instance, with SoundEngine being the implementation. The actual player is a third-party library initialized inside SoundEngine
I created the enum so I could manage the playbackstate from my UI
you could use a nullable
PlaybackState
property
like
granted there are downsides to an approach like this
if you ever change sound libraries you may have to refactor parts of your UI
but if that is not an issue you could use to above to keep things simple
the added benefit that a null PlaybackState
would let you know that you have not attempted to play any files and could accurately show that in your UII'd like to try and keep the UI separate from the implementation
in that case, you can still simplify your playback logic a little by using a fallthrough like I am
I'm getting an unexpected token on the nullable ? and a
Cannot resolve symbol '_player'
errorare you using an older version of c#?
.NET 8.0
show code then, should be valid unless you messed up syntax somewhere
oh im dumb, made a typo in my example lol
PlaybackState? PlaybackState
i gave you a property with a missing name :PatrickDab:Ahh, thank you!
On the switch statement, I'm getting a cannot resolve error on PlaybackState
change it to
State
https://github.com/LSXPrime/SoundFlow/blob/master/Src/Components/SoundPlayer.cs
it's an open source project so you can always look at the classes and see what you can useI had tried that, but then the switch case doesn't work:
Saying it cannot resolve symbol
https://github.com/LSXPrime/SoundFlow/blob/master/Src/Enums/PlaybackState.cs
it's a public type, should be able to use it
make sure you're not missing a
using
statement for itAnother thing is to access it inside my UI, it seems I need to modify my ISoundEngine interface to add
Which adds a dependency on SoundFlow to my Interface. Shouldn't I try to avoid that?
Vortac
I'd like to try and keep the UI separate from the implementation
Quoted by
<@138557476462264320> from #Dependency Injection Question (click here)
React with ❌ to remove this embed.
probably, but you were asking about how to use
PlaybackState
so i answered
you can still use your own enum with the switch statement i showed
just make sure you update your state property as wellI'm confused. My original switch statement used my own enum, which kept the implementation separate
yes, and I made suggestions before you mentioned wanting to keep implementations separate
my switch statement was intended to show you how to avoid copy-pasting code between cases
my switch statement with fallthrough, using your enum, would look like this:
Thanks!