StructureMap and public property problem I need to install
I have an interface that I used StructureMap
for Injectency.
public interface IFileStorageService
{
void SaveFile(string fileName, byte[] data);
}
The interface doesn't care where the data is stored. Whether it's memory, a file, a network resource, a satellite in space ...
So, I have two classes that implement this interface; a test class
and a network file storage class
: -
public class TestFileStorageService : IFileStorageService
{ ... etc ...}
public class NetworkFileStorageService : IFileStorageService
{
public string NetworkUnc { get; set; }
public void SaveFile(...);
}
Note that mine NetworkFileStorageService
has a property? This class requires an implementation of the SaveFile method.
Well, I'm not sure how to define this property.
I thought I could hardcode it where I define my dependency (like in my bootstrapper method -> ForRequestedType<IFileStorageService>
... etc), but the kicker is .. the business logic WILL DETERMINE the location. It is not static.
Finally, since I am using interfaces in my logic, this property is not available.
Can anyone please help?
If you can, the image you want to save as two files
- Name: Test1.bin; location: \ server1 \ folder1
- Name: Test2.bin; location: \ server1 \ folder2
Hooray!
source to share
The structure of the structure assumes that all constructor arguments are required and properties are optional.
There are several ways to solve this problem with structure. You can create a factory method like this
x.ForRequestedType<Func<string, IFileStorageService>>()
.TheDefault.Is.ConstructedBy(
() =>
NetworkUnc =>
{
var args = new StructureMap.Pipeline.ExplicitArguments();
args.SetArg("NetworkUnc", NetworkUnc );
return StructureMap.ObjectFactory.GetInstance<IFileStorageService>(args);
});
now you can create your object with
StructureMap.ObjectFactory
.GetInstance<Func<string, IFileStorageService>>()("something");
But this is not the case. Or create a config class for this class.
public class NetworkFileStorageServiceConfig
{
public string NetworkUnc { get; set; }
}
and configure it
x.ForRequestedType<NetworkFileStorageServiceConfig>().TheDefault.Is.IsThis(
new NetworkFileStorageServiceConfig { NetworkUnc = "something" });
I'm sure there are many other ways to do this.
source to share