Consider IParsable Interface that I need so my ASP.NET Core model binding works:
public interface IParsable<TSelf> where TSelf : IParsable<TSelf>?
{
static abstract TSelf Parse(string s, IFormatProvider? provider);
static abstract bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out TSelf result);
}
How do I provide a default implementation to it? My attempt at making a child interface doesn't work:
public interface IEntityParsable<TSelf> : IParsable<TSelf>
where TSelf : IParsable<TSelf>
{
static TSelf Parse(string s, IFormatProvider? provider)
{
// ...
}
}
I can only hide it with new keyword but then it isn't IParsable implementation anymore. I also cannot use override:
The modifier 'override' is not valid for this item
I see they did consider this scenario but I could not find any definitive answer to this question: Default implementations
Related
My goal (so that I don't run into an x-y problem):
I am trying to make a Filter class which works with a T generic type. Since the filter works Vector operations, I want to convert the object to a Vector<double>, do the filtering and then convert it back to T once the data is requested.
Interface cannot have static methods
I made an interface that should look something like this.
public interface IFilterable {
public Vector<double> ToVector();
public static IFilterable FromVector(Vector<double> vec);
}
So that I can use it over in the Filter:
public class Filter<T> where T: IFilterable {
private Vector<double> _state;
public T GetState() {
return T.FromVector();
}
Interfaces cannot have static methods, so I'm looking for an alternative that gives me this behavior, that still allows me to use T as the generic type.
Thanks to #Fildor for a solution with interfaces:
public interface IFilterable<out T> where T: new() {
abstract Vector<double> ToVector();
T FromVector(Vector<double> vec);
}
Which can be used by the Filter like this:
public class Filter<T> where T : IFilterable<T>, new() {
private Vector<double> _state;
public T GetState() {
return new T().FromVector(_state);
}
}
Additional solutions include using an abstract class instead of an interface.
Let's assume I have an interface that has some method parametherized with another interface:
interface IFeature<T> where T : IFeatureParameters
{
CustomObject Apply(CustomObject obj, T featureParameters);
}
But features are so much different that there is nothing common in their parameters so IFeatureParameters interface is in fact marker interface. It just forces developers in future to create Feature and FeatureParameters implementations in pairs.
As far as I googled, marker interfaces are considered to have no reasons to exist in custom code.
Is it suitable to use marker interfaces in my case? If not, what may substitute it?
An interface IFeatureParameters has no added value here. Whether or not a class (or whatever type you like) is a valid type to pass parameters to a feature, is entirely determined by the feature implementation.
Every time a developer makes a new implementation of interface IFeature, they will specify explicitly what is the correct parameter type, by filling in the type variable T.
That is enough to ensure no 'alien' types will be passed into an implementation of method Apply.
Here is a simple example.
public class FeatureParametersA
{
public string SomeText;
}
public class FeatureParametersB
{
public int SomeNumber;
}
I could have made these classes implement an interface IFeatureParameters, but that is not required.
public interface IFeature<T>
{
CustomObject Apply(CustomObject obj, T par);
}
public class FeatureA : IFeature<FeatureParametersA>
{
public CustomObject Apply(CustomObject obj, FeatureParametersA par);
{
obj.Add(par.SomeText);
return obj;
}
}
public class FeatureB : IFeature<FeatureParametersB>
{
public CustomObject Apply(CustomObject obj, FeatureParametersB par);
{
obj.Add(par.SomeNumber.ToString());
return obj;
}
}
Notice how each class has its own dedicated implementation of Apply, specific for the related 'parameters' type.
Everything is strongly typed, so the compiler will prevent anyone from trying to pass the wrong type into Apply.
For completeness:
public class CustomObject
{
public void Add(string s) { _sb.AppendLine(s); }
private StringBuilder _sb = new StringBuilder();
}
I have an extension method that allows a user to turn off lazy loading.
public static IBaseEntityService<TEntity, TPrimaryKey> EnableLazyLoading<TEntity, TPrimaryKey>(
this IBaseEntityService<TEntity, TPrimaryKey> baseService,
bool enabled)
where TEntity :Entity<TPrimaryKey> where TPrimaryKey : struct
{
baseService.UnitOfWork.EnableLazyLoading(enabled);
baseService.UnitOfWork.EnableProxyCreation(enabled);
var type = baseService.GetType().;
return (type)baseService;
}
Here is a sample of the base interface:
public interface IBaseEntityService<TEntity,TPrimaryKey> :
IBaseService<TEntity, TPrimaryKey>
where TEntity: Entity<TPrimaryKey> where TPrimaryKey : struct
{
TEntity FindByTenantId(Guid tenantId);
IBaseEntitySpecification<TEntity,TPrimaryKey> Specification { get; }
}
Let's say I have this service that implements the IBaseEntityService<TEntity,TPrimaryKey> interface:
public IUserService: IBaseEntityService<User,int>
{
User FindByUserName(string username);
}
Here is what I want to achieve:
var user = _userService.EnableLazyLoading(false).FindByUserName("someUsername");
As you can see in my example after the EnableLazyLoading(false) I can get to the FindByUserName method. Currently, since it only returns an IBaseEntityService I do not have that option.
I want to ensure the extension method knows to return the IUserService since it implements the IBaseEntityService. I know it would have to cast it at some point and I want to avoid having to write a specific implementation of that same EnableLazyLoading method for the IUserService
I thought of something like this, but it seems like I could do something without having to implicitly call a Cast method:
public static TEntityService Cast<TEntity, TPrimaryKey, TEntityService>(
this IBaseEntityService<TEntity, TPrimaryKey> baseEntityService)
where TEntity : Entity<TPrimaryKey>
where TPrimaryKey : struct
where TEntityService : IBaseEntityService<TEntity, TPrimaryKey>
{
return (TEntityService) baseEntityService;
}
So it might work like this:
var user = _userService.EnableLazyLoading(false).Cast<IUserService>().FindByUserName("someUsername");
You could just make the receiver of EnableLazyLoading a generic type constrained to be an IBaseService. It's wordy, but ought to do the trick.
public static TService EnableLazyLoading<TService, TEntity, TKey>(this TService service)
where TService : IBaseService<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : struct
{
// do stuff
return service;
}
That way, you get the actual service type as the return type. I haven't done C# in a little while, so I may be forgetting something important, but IIRC this works just fine.
The best solution, provided by Ben in his answer is:
public static T EnableLazyLoading<T, TEntity, TKey>(
this T #this,
bool enabled)
where T : IBaseService<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : struct
{
#this.UnitOfWork.EnableLazyLoading(enabled);
#this.UnitOfWork.EnableProxyCreation(enabled);
return #this;
}
Another solution, cheaper but but not that clean is to use the dynamic keyword:
public static dynamic EnableLazyLoading<TEntity, TPrimaryKey>(
this IBaseEntityService<TEntity, TPrimaryKey> #this,
bool enabled)
where TEntity : Entity<TPrimaryKey>
where TPrimaryKey : struct
{
#this.UnitOfWork.EnableLazyLoading(enabled);
#this.UnitOfWork.EnableProxyCreation(enabled);
return #this;
}
The cost is that you loose the convenience of IntelliSense, and possibly some performance.
I have a generic interface with two type parameters with severe generic constraints, and several implementations for different combinations.
public interface IResolver<TIn, TOut> where ... {...}
I want to create a (static) resolver factory that will store instances of the known implementations, and just serve them, along the lines:
public static ResolverFactory{
public static IResover<TIn, TOut> GetResolver<TIn, TOut> where ... ()
{
//access some storage dictionary to return the correctly typed instance
}
}
How can I create such a containter, that will store both IResover<Entity1, Entity2> and IResolver<Entity3, Entity4>?
One option I can think of is to use a separate non-generic "marker" interface like:
public interface IResolver {}
public interface IResolver<TIn, TOut> : IResolver where ....
{...}
and use
Dictionary<Type, Dictionary <Type, IResolver>> storage;
public RegisterResolver(IResolver resolver)
{
//add to storage - how?
}
but this scenario basically invalidates the constraints put on the generic parameters.
Also when adding the IResolver, getting the generic types of the IResolver<TIn, TOut> is more or less impossible.
Is there a better solution to this?
There may be something obvious I'm missing in your question, because I don't understand where the issue is.
First, I declare a IResolver<TIn, TOut> interface with a constraint:
public interface IResolver<TIn, TOut>
where TIn : Stream
{
}
Then, I create a ResolverFactory, where the constraints are enforced by both the RegisterResolver and GetResolver method. The way the objects are actually stored doesn't matter, because the storage isn't exposed outside of the class. The encapsulation maintains the consistency:
public static class ResolverFactory
{
private static Dictionary<Type, object> storage = new Dictionary<Type, object>();
public static void RegisterResolver<TIn, TOut>(IResolver<TIn, TOut> resolver) where TIn : Stream
{
storage[typeof(IResolver<TIn, TOut>)] = resolver;
}
public static IResolver<TIn, TOut> GetResolver<TIn, TOut>() where TIn : Stream
{
return storage[typeof(IResolver<TIn, TOut>)] as IResolver<TIn, TOut>;
}
}
Like KooKiz's answer, but no casting, and no dictionary. Usage is similar.
//Rather than:
var res = ResolverFactory.GetResolver<Stream, Hat>();
//You Do:
var res = ResolverFactory<Stream, Hat>.GetResolver();
Just moved the generic parameters, with added advantage of defining generic constraints in fewer places.
public interface IResolver<TIn, TOut>
where TIn : Stream
{
}
//Still static, but now there is one per TIn,TOut pair
//so no need for dictionary, so also no need for casting.
public static class ResolverFactory<TIn, TOut> where TIn : Stream
{
private static IResolver<TIn, TOut> _resolver;
public static void RegisterResolver(IResolver<TIn, TOut> resolver)
{
_resolver = resolver;
}
public static IResolver<TIn, TOut> GetResolver()
{
return _resolver;
}
}
internal class Program
{
private static void Main(string[] args)
{
ResolverFactory<Stream, Hat>.RegisterResolver(new MyStreamToHatResolver());
var res = ResolverFactory<Stream, Hat>.GetResolver();
}
}
I have the following .
public interface IMyService<T>
where T: BaseModelType
{
Process(T input);
}
public class BaseModelType
{
...some property
}
public class SomeClass : BaseModelType
{
...some properties
}
public ServiceImpl : IMyService<SomeClass>
{
...the properties
}
Then I have a unity container where i register all the implementations of the generic interface. I want to be able to use the unitycontainer's resolve method to get the interface, then do some work on it. At the time when i want to use the Resolve method i have the type in runtime
new UnityContainer.Resolve(myTypeVar)
Can I somehow cast this to be
IMyService<BaseModelType> value = new UnityContainer.Resolve(myTypeVar) //want to cast it here from object.
So that i can call the Process method that the interface defines.
No, because IMyService<SomeClass> does not implement IMyService<BaseModelType>. If you look at the implementation of the Process method:
public void Process(SomeClass input){...}
This clearly assumes that you're giving it a SomeClass. It should be able to safely access any members of SomeClass. But if you called this method with a BaseModelType as the parameter, that wouldn't work, would it?
Assuming that you know at runtime that your input argument is going to be of the right type for the given generic IMyService<T> interface, you have two options:
Invoke the generic method signature via reflection. A little slow, but effective.
Add a non-generic parent interface for IMyService, which takes a BaseModelType. In your service implementations, you can implement this method by casting the input to the expected type for that implementation. This requires more code. But you could alleviate that somewhat by having a generic abstract base class that implements this method so the other implementations don't have to.
void Main()
{
var s = (IMyService)new ServiceImpl();
s.Process(new SomeClass());
}
public interface IMyService
{
void Process(BaseModelType input);
}
public interface IMyService<in T> : IMyService
where T: BaseModelType
{
void Process(T input);
}
public class BaseModelType{}
public class SomeClass : BaseModelType{}
public abstract class ServiceBase<T> : IMyService<T>
where T: BaseModelType
{
void IMyService.Process(BaseModelType input)
{
Process((T)input);
}
public abstract void Process(T input);
}
public class ServiceImpl : ServiceBase<SomeClass>{
public override void Process(SomeClass input){}
}