Comments (10)
My understanding from the original report is that the Essential.Interfaces package is supposed to provide instantiable wrappers for the static classes, as long as you are using its namespace rather than the static class namespace. I’m surprised that it doesn’t work if you’ve set it up properly.
That said, it looks like the project hasn’t been updated in years. I’m intrigued enough to want to follow up by building a test but probably won’t have a lot of time to look for a while.
from tinyioc.
@gaurakshay I did say that I know of... I don't know very much :). I'm just trying to help as I saw no one else responding... If you feel like we should support this, please feel free to open a pr and add unit test coverage. If you have a link to the docs for those respective library features I'd love to read more when I get time.
from tinyioc.
I'm not sure this makes any sense at all - as @niemyjski said, you have a static class there, and the static class can't implement that interface, so you can't use it as an implementation. The only way to work around this would be to build a non-static wrapper that implements that interface and delegates to the static class, which is something you'd have to do yourself then register that wrapper class in the container.
It's possible that some containers generate that wrapper for you automatically, but it would surprise me if that was the case as its a pretty strange thing to do, and unless your interface is huge its not much work to create the delegating implementation.
from tinyioc.
My understanding from the original report is that the Essential.Interfaces package is supposed to provide instantiable wrappers for the static classes, as long as you are using its namespace rather than the static class namespace. I’m surprised that it doesn’t work if you’ve set it up properly.
That said, it looks like the project hasn’t been updated in years. I’m intrigued enough to want to follow up by building a test but probably won’t have a lot of time to look for a while.
If that's the case then the OP is probably using the wrong class in the registration, it should be IFileSystem against the wrapper class, not the static one 🤷
from tinyioc.
@niemyjski , @grumpydev , @gkarabin I was being a dumbass. It was supposed to be
_container.Register<IFileSystem, FileSystemImplementation>();
instead of
_container.Register<IFileSystem, FileSystem>();
Please accept my apologies.
from tinyioc.
I'd recommend reading this: https://stackoverflow.com/questions/52325438/how-to-inject-dependency-to-static-class Also static classes can't implement interfaces. If you will have more than one implementation of file system, then I'd probably create a wrapper around the static type which is not ideal.. otherwise I'd consume FileSystem directly.
from tinyioc.
@niemyjski Thanks for the input!
Since I plan on using the DI in a regular class (not static) I don't think that the stackoverflow discussion applies in this case. I simply want to register the interface and implementation which then I will use like we usually do:
The interface that needs to be registered:
namespace Xamarin.Essentials.Intefaces
{
public Interfaces IFileSystem
{
string CacheDirectory { get; }
string AppDataDirectory { get; }
}
}
The concrete type that I want to register against the interface:
namespace Xamarin.Essentials
{
public static class FileSystem
{
public static string CacheDirectory { get; }
public static string AppDataDirectory { get; }
}
}
Register the two:
_container.Register<IFileSystem, FileSystem>();
Use DI to obtain the FileSystem
object:
public class ClassA{
public ClassA(IFileSystem fileSystem)
{
// initialize class fields
}
}
It is alright if TinyIOC doesn't support this use case, I was looking for confirmation in case it is me just not familiar enough with this awesome package.
from tinyioc.
FileSystem doesn't even implement that interface and it's something no di will support (that I know of as there is no concept of shapes). I'm going to close this for the reasons above.
from tinyioc.
@niemyjski that is an unfair statement to make. I know both Autofac and MVVMCross's IOC implementation support this feature. I was merely trying to understand if this is something that TinyIOC offers as well or not. Based on your response I can guess it doesn't and that is fine. That is all I needed to know in the first place.
from tinyioc.
@niemyjski , @grumpydev I can look up and find more details about this if you both are interested.
from tinyioc.
Related Issues (20)
- Unnecessary SuppressFinalizes HOT 3
- Platform compatibility HOT 7
- Resolving generic type from parent container fails HOT 3
- Injecting IEnumerable<Type> to constructor - unnamed registrations HOT 1
- System.IO.FileNotFoundException with UWP and TinyIoC 1.3.0 HOT 6
- It would be nice to have a .NET Core test app HOT 1
- NuGet Support & Build System HOT 4
- Resolve Issues with 1.4 alpha nuget packages HOT 10
- AutoRegister registers over 10k types when using in Xamarin.Forms HOT 4
- The type.gettypeconStructors method gets seven constructors in .Net Core HOT 3
- QUESTION: Can I disable property injection? HOT 2
- TinyIoC namespace cannot be found HOT 1
- PublishAsync publishes message twice (.netstandard)
- Optimization of objects allocation during auto-registration of TinyIoC HOT 3
- Is this good choice for large scale ASP.NET projects? HOT 1
- TinyIoCResolutionException after multiple calls of AutoRegister
- Should nullable reference types be supported, or have their code analysis warnings disabled inside the file via pragma's? HOT 4
- [.net-ios] Warning : The file 'TinyIoc.cs' does not specify a 'PublishFolderType' metadata, and a default value could not be calculated. HOT 1
- Registering any class with a copy constructor throws a System.TypeInitializationException HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyioc.