First, some background on my code.
I have a interface for items that can be saved/used with a data store/repository type service:
public interface IItem
{
Guid Id { get; set; }
}
public interface IDataStore<T> where T: IItem { ... }
An example of such an item:
public class ItemNormalExample : IItem
{
public Guid Id { get; set; }
public string RandomProperty { get; set; }
}
And then I also have special/specific type of item, which other items inherit from. This is where my problem lies.
public interface ISpecialItem : IItem
{
string ImportantString { get; set; }
}
public class ItemSpecialBase : ISpecialItem
{
public Guid Id { get; set; }
public string ImportantString { get; set; }
}
public class ItemSpecialExample : ItemSpecialBase
{
public string RandomProperty { get; set; }
}
I am using Autofac to resolve instances of this data store of each concrete implementation of IItems. I.e.:
builder.RegisterType<DataStore<ItemNormalExample>>().As<IDataStore<ItemNormalExample>>();
This works as intended, however, for my objects inheriting from the ItemSpecialBase base class, I would like these to be instantiated as a DataStore of the base class type instead of the actual child class.
Something like this (This doesn't work):
builder.RegisterType<DataStore<ItemSpecialBase>>().As<IDataStore<ItemSpecialExample>>();
My intend is for every object inheriting from ItemSpecialBase to use the same DataStore. My DataStore class supports this and works fine.
How can I accomplish this using Autofac?