why can't we have concrete implementation of base interface in aggregation? - c#

While implementing an interface, why can't we have concrete implementation of base interface in aggregation?
Which OOP, principle is violating here? StorageEntitySas is essentially of type IStorageEntitySas
public interface IValetKeyResponse
{
IStorageEntitySas Sas { get; set; }
string UploadUrl { get; set; }
}
public class ValetKeyResponse : IValetKeyResponse
{
//Which OOP, principle is violating here? StorageEntitySas is essentially of type IStorageEntitySas
**public StorageEntitySas Sas { get; set; }**
public string UploadUrl { get; set; }
}
public class StorageEntitySas : IStorageEntitySas
{
public string Credentials { get; set; }
public Uri BlobUri { get; set; }
public string Name { get; set; }
}

An interface defines a contract that every implementing class has to fulfill. It defines what a method has to return and which parameters you may use. In particular, users of your interface don't know - and don't have to know anything of the concrete classes, which is an implementation detail.
However you're still able to do the following:
public class ValetKeyResponse : IValetKeyResponse
{
public IStorageEntitySas Sas { get; set; } = new StorageEntitySas();
public string UploadUrl { get; set; }
}
So why is this not allowed? Imagine you have an API that simply returns the interface-type.
IValetKeyResponse response = myClass.GetValetResponse();
// what am I allowed to pass here when I don´t know the actual class of response?
response.Sas = ...
A client of this API doesn´t know which class you actually get and thus which kind of IStorageEntitySaS you´re allowed to assign to that instance. That´s what the contract defines, it sais you can pass anything that implements IStorageEntitySas.

The setter on the Sas property allows you to provide any implementation of IStorageEntitySas. This means you should be able to do:
public class OtherStorageSas : IStorageEntitySas { ... }
IValetKeyResponse r = new ValetKeyResponse();
r.Sas = new OtherStorageSas();
This would not be type-safe if allowed since ValetKeyResponse.Sas has an incompatible type StorageEntitySas.

Related

Implement Interface through class member

Is it possible in C# to implement an Interface through a class member without explicitly returning the members implementation?
I want something like this
interface IAttachement
{
byte[] Data { get; }
string Name { get; }
long Size { get; }
}
class Attachement : IAttachement
{
public byte[] Data { get; set; }
public string Name { get; set; }
public long Size { get; set; }
}
class Request : IAttachement
{
public Attachement Attachement { get; set; } : IAttachement
}
Instead of
class Request : IAttachement
{
public Attachement Attachement { get; set; }
public byte[] Data => Attachement.Data;
public string Name => Attachement.Name;
public long Size => Attachement.Size;
}
No, this isn't a feature that C# offers.
You can delegate interface implementation to a field/property/member, but you have to do it explicitly, as in your example.
If you would like this feature adding to the language, you can raise an issue here.
It is not possible to implement an interface through a class member without explicitly returning the member's implementation. In order for a class to implement an interface, it must provide an implementation for all members of the interface, either explicitly or implicitly. In your example the Request class needs to provide an implementation for the Data, Name, and Size properties of the IAttachment interface, either by implementing them directly or by delegating to the Attachement member.

Generic interfaces and inheritance in .NET

I have the following scenario that involves a couple of interfaces as below
internal interface ITranslation
{
string LanguageCode { get; set; }
string Title { get; set; }
}
Any object that hold translations will implement the ITranslation interface. Some of these objects can have synonyms as well, so I have another interface
internal interface ITranslationWithSynonmys : ITranslation
{
IList<string> Synonyms { get; set; }
}
Next step I have defined ITranslatable<T> interface for any object that has translations and can be translated in different languages
internal interface ITranslatable<T> where T : ITranslation
{
IList<T> Translations { get; set; }
}
while when there are synonyms involved the ITranslatableWithSynonyms<T> looks like this
internal interface ITranslatableWithSynonyms<T> : ITranslatable<T> where T : ITranslationWithSynonmys
{
IList<T> SynonymTanslations { get; set; }
}
Concrete implementations of ITranslation and ITranslationWithSynonmys would be
internal class BaseTranslation : ITranslation
{
public string Title { get; set; }
public string LanguageCode { get; set; }
}
internal class BaseTranslationWithSynonmys : ITranslationWithSynonmys
{
public IList<string> Synonyms { get; set; }
public string LanguageCode { get; set; }
public string Title { get; set; }
}
while an entity that can be translated would be
internal class TranslatableEntity : ITranslatable<ITranslation>
{
public IList<ITranslation> Translations { get; set; }
}
and if it has synomys
internal class TranslatableWithSynonymsEntity : ITranslatableWithSynonyms<ITranslationWithSynonmys>
{
public IList<ITranslationWithSynonmys> SynonymTanslations { get; set; }
public IList<ITranslationWithSynonmys> Translations { get; set; }
}
Next, I'm creating a service that can translate any object that implements ITranslatable<T> and I have defined it as
internal class TranslationService
{
internal string Translate(ITranslatable<ITranslation> translatable, string languageCode)
{
// It will iterate through the Translations list to find the correct translation
return string.Empty;
}
}
Now, when I try to use the service, I'm writting
var translationService = new TranslationService();
var translatableEntity = new TranslatableEntity();
var translatableWithSynonymsEntity = new TranslatableWithSynonymsEntity();
string x = translationService.Translate(translatableEntity, "en");
string y = translationService.Translate(translatableWithSynonymsEntity, "en");
and here the last line translationService.Translate(translatableWithSynonymsEntity, "en") fails to compile with error CS1503: Argument 1: cannot convert from 'TestInheritance.TranslatableWithSynonymsEntity' to 'TestInheritance.ITranslatable<TestInheritance.ITranslation>'
It's true that TranslatableWithSynonymsEntity doesn't implement ITranslatable<ITranslation>, but it implements ITranslatableWithSynonyms<ITranslationWithSynonmys> with both ITranslatableWithSynonyms<T> inheriting from ITranslatable<T> and ITranslationWithSynonmys inheriting from ITranslation.
I can get the code to compile by having TranslatableWithSynonymsEntity implement both ITranslatableWithSynonyms<ITranslationWithSynonmys> and ITranslatable<ITranslation>, but that means managing two lists and it doesn't look clean.
internal class TranslatableWithSynonymsEntity : ITranslatableWithSynonyms<ITranslationWithSynonmys>, ITranslatable<ITranslation>
{
public IList<ITranslationWithSynonmys> SynonymTanslations { get; set; }
public IList<ITranslationWithSynonmys> Translations { get; set; }
IList<ITranslation> ITranslatable<ITranslation>.Translations { get; set; }
}
Is there a way to avoid this? Or am I taking a wrong approach?
Thank you
Generic parameters are invariant by default, in the method Translate you want the type to be <ITranslation>, so you must provide a type whose (or its parents') generic parameter is exactly <ITranslation>.
In your example you cannot simply mark the parameter as covariant because it contains a property has both getter and setter.
Since the problem is the generic parameter, to solve the problem, don't specify one, in fact you have already constrained the generic parameter.
interface ITranslatable<T> where T : ITranslation
The method (or the class) just need to be declared with the same constraint.
internal string Translate<T>(ITranslatable<T> translatable, string languageCode)
where T : ITranslation

Interface problem c#, and dependency injection

I am having a problem. Lets see an example:
you got this interface which would be implemented by Employee.cs and Owener.cs:
public interface IEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
}
public class Employee: IEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
}
public class Owner: IEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
public string Status{ get; set; } <--- //problem string
}
Now when we are using Dependency Injection and it returns the object of employee or manager, thats where i run into problem.
public class EmployeeCheck{
private IEmployee empObj;
public EmployeeCheck(IEmployee _em)
{
empObj=_em
}
public void PrintCheck()
{
string str=_em.FirstName;
string str2=(Owner)_emp.Status <--- //problem...how do I access it?? It can't be accessed cause
//IEMployee doesn't have status field!
}
So basically if I use IEmployee as the interface , I can't access fields in new the Owner class, and if I do put them in interface, then Employee class which doesn't need to implement it, will be forced to implement something it doesn't need! And I do need IEmployee due to DI injection or other design pattern
OK, I can't use abstract class...so lets discuss more about the IStatus solution...so you are talking about writing code like this:
public interface IStatus:IEmployee
{
public string Title { get; set; }
}
public class Owner: IEmployee, IStatus
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
public string Status{ get; set; } <--- //problem string
}
But how do I work it in Employee check class?
public class EmployeeCheck
{
private IEmployee empObj;
public EmployeeCheck(IEmployee _em, IStatus)
{
empObj=_em
}
}
The scenario that you're dealing with could be handled traditionally using IoC Containers like StructureMap or Unity by using something called Named Instances. These containers provide this kind of functionality out of the box.
The same can be achieved in .NET Core in multiple ways. One way is to use the extension method from IServiceCollection. The below snippet walks you through how this could be done in your scenario
// using Microsoft.Extensions.DependencyInjection
// Startup.cs - ConfigureServices()
services.AddTransient(serviceProvider =>
{
Func<string, IMyClass> func = key =>
{
switch (key)
{
case "MyClass":
return serviceProvider.GetService<MyClass>();
case "MyClass1":
return serviceProvider.GetService<MyClass2>();
default:
throw new KeyNotFoundException();
}
};
return func;
});
//Register your services here as usual
services.AddTransient<IMyClass, MyClass>();
services.AddTransient<IMyClass, MyClass2>();
You are essentially creating a factory here that is going to give out the dependency of the type that you need based on the key. The following snippet of code shows how this can be done within your controller.
// ctor of your controller
public MyController(Func<string, IMyClass> injector)
{
// key here could be 'MyClass' or 'MyClass2'
IMyClass service = injector("<key>");
}
Below is the structure of sample classes I have considered for the above sample
// implementation 1
public class MyClass : IMyClass
{
}
// implementation 2
public class MyClass2 : IMyClass
{
}
// interface
public interface IMyClass
{
}
There are also other ways to handle this. You can take a look at this answer for other approaches.
It depends on how you use or why you have to use dependency injection. I think that in these cases according to your example it is not so good to use it, since it is giving you complexity in something simple.
If you are going to perform an action with the Status value, you could segregate the interface by generating a new one. Like this.
public interface IStatus { string Status { get; set; } } and then you implement this interface just in Owner and in your constructor EmployeeCheck you inject IStatus.
But if it is not necessary why not IEmployee you do it as an abstract class.
public abstract class Employee
{
public string Name { get; set; }
public string LastName { get; set; }
public string Location { get; set; }
}
public class Owner : Employee
{
public string Status { get; set; }
}
public class EmployeeCheck
{
public EmployeeCheck(Employee employee)
{
var owner = employee as Owner;
var statuts= owner.Status;
}
}
The first question you need to consider is: what is EmployeeCheck supposed to do when it is instantiated with an Employee (say) since it seems to require Status to print a check?
The whole idea behind interfaces is that they provide a contract for the operations that can be performed with an object. In this case, you are trying to do something (use Status) that is not specified in the contract so the type system is making it a little bit harder (forcing you to cast).
An option (as suggested by #Diegorincon) which avoids casting, is to create another interface (something like IHasStatus) which implements IEmployee and then change the type in EmployeeCheck (CheckPrinter maybe clearer?) to IEmployeeWithStatus.
interface IEmployee
{
string FirstName { get; }
string LastName { get; }
}
interface IEmployeeWithStatus:IEmployee
{
string Status { get; }
}
public class Owner : IEmployeeWithStatus
{
public string FirstName { get; }
public string LastName { get; }
public string Status { get; }
}
class EmployeeCheck
{
private readonly IEmployeeWithStatus _employeeWithStatus;
public EmployeeCheck(IEmployeeWithStatus employeeWithStatus)
{
_employeeWithStatus = employeeWithStatus;
}
void PrintCheck()
{
// no casting needed
Console.Write($"{_employeeWithStatus.FirstName} {_employeeWithStatus.LastName} {_employeeWithStatus.Status}");
}
}
If you are stuck using the signatures in your example, one option for writing the code is to write a switch statement using the type pattern:
switch (employee)
{
case Owner owner:
{
// you can use owner.Status here
Console.WriteLine(owner.Status);
}
break;
case Employee employee:
{
// hmm.., now what?!
}
break;
}
It's cleaner than having to cast all over the place but at the end of the day, it's just pushing the same problem around.
(In the Object Oriented paradigm, this situation comes up all the time where you have a object in hand with a more general type (something like Animal) but you find yourself wanting to do operations based on its specific type (something like Dog). Switching on type as the code above does is usually considered a code smell. With more details, I might be able to offer some other ideas)

How to properly implement concrete class that inherits interface with List<AnotherInterface> [duplicate]

This question already has answers here:
Understanding Covariant and Contravariant interfaces in C#
(2 answers)
Closed 4 years ago.
I have a proposed dictionary item schema that looks like this:
public interface IDataDictionary
{
List<IDataDictionaryItem> DictionaryItems { get; set; }
}
And the dictionary items as:
public interface IDataDictionaryItem
{
Guid Guild { get; set; }
int SequenceId { get; set; }
string Title { get; set; }
string Value { get; set; }
string Description { get; set; }
string Language { get; set; }
}
When I try to implement this in a concrete class like such:
public class HairColorDictionary : IDataDictionary
{
public List<HairColor> DictionaryItems { get; set; }
}
With the items concrete implementation:
public class HairColor : IDataDictionaryItem
{
public Guid Guild { get; set; }
public int SequenceId { get; set; }
public string Title { get; set; }
public string Value { get; set; }
public string Description { get; set; }
public string Language { get; set; }
}
I get the error message below, and I can't seem to wrap my head around it.
Can someone please enlighten me where this went wrong.
Error message:
Error CS0738 'HairColorDictionary' does not implement interface member 'IDataDictionary.DictionaryItems'. 'HairColorDictionary.DictionaryItems' cannot implement 'IDataDictionary.DictionaryItems' because it does not have the matching return type of 'List'.
I understand what you're trying to get at and the reason it doesn't work is because the interface defined a collection that is non specific (that interface could be implemented), but you're trying to implement that interface member in a specific way.
If you really must do this you can do it with generics. Here's the updated bits:
public interface IDataDictionary<T> where T : IDataDictionaryItem
{
List<T> DictionaryItems { get; set; }
}
public class HairColorDictionary : IDataDictionary<HairColor>
{
public List<HairColor> DictionaryItems { get; set; }
}
T allows you to use anything and the where T:... part puts a constraint on it.
Please realize that is a potential answer to your solution. I've not taken the time to ponder the upsides or downsides of it.
In your implementation, HairColorDictionary is-a IDataDictionary. So i could do something bad like:
IDataDictionary d = new HairColorDictionary();
d.DictionaryItems.add(blah);
Here add method expects something of type IDataDictionaryItem and not necessarily HairColor because d is just IDataDictionary and not HairColorDictionary! So i could do something like
class SkinColor : IDataDictionaryItem { ... }
IDataDictionaryItem s = new SkinColor();
d.DictionaryItems.add(s);
Question is whether this last statement is valid or not? It should be valid because add expects a IDataDictionaryItem which SkinColor correctly implements. But then it should not be valid because the actual public List<HairColor> DictionaryItems only allows HairColors and not SkinColors!!!
To eliminate all this confusion and to maintain strict type safety, you have to follow Covarience and Contravarience rules. Or you can go ahead with #TheMuffinMan's nice answer.

how to set value of property in constructor (explicit interface implementation)

[DataContract]
public class OrderSyncData : ISync
{
public OrderSyncData(Order o)
{
this.CurrentOrderStatus = o.DriverStatus;
this.StatusDescription = o.StatusDescription;
SyncTimestamp = o.SyncTimestamp; ????
}
[DataMember]
public string CurrentOrderStatus { get; set; }
[DataMember]
public string StatusDescription { get; set; }
[DataMember]// I don't think I need these any more
public bool IsCanceled { get; set; }
[DataMember]
public bool IsResolved { get; set; }
[DataMember]
public bool IsPendingResponse { get; set; }
DateTime ISync.SyncTimestamp { get; set; }
}
How to set the value of ISync.SyncTimestamp? I tried casting the "this." but it doesn't work
This should work:
((ISync)this).SyncTimestamp = o.SyncTimestamp;
Note the extra braces around (ISync)this.
You just need to cast this:
((ISync) this).SyncTimestamp = o.SyncTimestamp;
Or you could do it in two statements:
ISync sync = this;
sync.SyncTimestamp = o.SyncTimestamp;
Basically, the explicit interface implementation means that the property is only available when you're viewing this in the context of simply ISync, not the implementation class.
This is because you've implemented SyncTimestamp explicitly. Explicit implementations cannot be accessed from a class instance. Why? Because explicit implementation allows you to implement multiple interfaces with the same member name.
class Foo: IBar, IFoo
{
bool IBar.FooBar {get;set;}
bool IFoo.FooBar {get;set;}
}
Then writing this.FooBar refers to which implementation? So either you cast this to the desired interface explicitly, like other answers suggest, or you don't implement the SyncTimestamp explicitly, but do it implicitly: public DateTime SyncTimestamp { get; set; }.
Then this.SyncTimestamp will work.

Categories