How to enforce implementation of nested class properties? - c#

I'm trying to implement my own multi-language feature in Blazor WASM. Nothing fancy but I would like to make sure I don't forget to implement properties in a specific language. In TypeScript this would work with typing or interfacing objects. In C# the equivalent of such an object would be a class. I tried using interfaces and abstract classes in C# but I'm not getting the desired result.
The desired result would be for my code editor to hint when I forgot to implement a property. How should I proceed?
I could just put all properties directly into the Translation class to enforce their implementation but than I'll have one very long list of properties, I'd prefer to chop them up a bit.
public class NavMenuProps
{
public string FirstMenuHeader { get; set; } = "Dogs";
public string SecondMenuHeader { get; set; } = "Facilities";
public string ThirdMenuHeader { get; set; } = "Profile";
}
public abstract class Translation
{
public abstract NavMenuProps NavMenu();
}
public class English : Translation
{
public override NavMenuProps NavMenu()
{
return new NavMenuProps();
}
}
public class Dutch : Translation
{
public override NavMenuProps NavMenu()
{
return new NavMenuProps
{
FirstMenuHeader = "Honden",
SecondMenuHeader = ""
};
}
}
public class French : Translation
{
public override NavMenuProps NavMenu()
{
throw new System.NotImplementedException();
}
}
In TypeScript it would work like this:
type TNavMenuProps = {
firstMenuHeader: string;
secondMenuHeader: string;
thirdMenuHeader: string;
...
}
type TLoginPageProps = {
firstLabel: string;
secondLabel: string;
...
}
type TTranslation = {
navMenu: TNavMenuProps;
loginPage: TLoginPageProps;
...
}
const englishTranslations: TTranslation = {
navMenu: {
firstMenuHeader: "first";
secondMenuHeader: "second";
// Would complain I forgot "thirdMenuHeader".
},
loginPage: {
firstLabel: "1st label";
secondLabel: "2nd label";
},
...
}

If I understood your requirements, here is the solution
Declare NavMenu properties as interface
public interface NavMenuProps
{
public string FirstMenuHeader { get; set; }
public string SecondMenuHeader { get; set; }
public string ThirdMenuHeader { get; set; }
}
Declare Translation class as abstract with abstract properties
public abstract class Translation : NavMenuProps
{
public abstract string FirstMenuHeader { get; set; }
public abstract string SecondMenuHeader { get; set; }
public abstract string ThirdMenuHeader { get; set; }
}
Now you can implemented each language translation, if you do not implement NavMenuProps properties in any language class, code editor will complain like "Does not implement inherited abstract member"
public class English : Translation
{
public override string FirstMenuHeader { get; set; }
public override string SecondMenuHeader { get; set; }
public override string ThirdMenuHeader { get; set; }
}
public class Dutch : Translation
{
public override string FirstMenuHeader { get; set; }
public override string SecondMenuHeader { get; set; }
public override string ThirdMenuHeader { get; set; }
}
public class French : Translation
{
public override string FirstMenuHeader { get; set; }
public override string SecondMenuHeader { get; set; }
public override string ThirdMenuHeader { get; set; }
}

If I understand your requirements correctly, this could be a way to go: Make NavMenuProps abstract, and implement NavMenu() inside Translation. Now, rather than implementing NavMenu() in all of the classes that inherit Translation, you rather need to implement all properties in NavMenuProps in all those classes.
public abstract class NavMenuProps
{
public abstract string FirstMenuHeader { get; }
public abstract string SecondMenuHeader { get; }
public abstract string ThirdMenuHeader { get; }
}
public abstract class Translation : NavMenuProps
{
public Translation NavMenu() => this;
}
public class English : Translation
{
public override string FirstMenuHeader => "First";
public override string SecondMenuHeader => "Second";
public override string ThirdMenuHeader => "Third";
}
Now you can call
var englishMenu = new English().NavMenu();
If there is any need to implement NavMenu() in any other way for some classes, you could make it virtual and override it where needed:
public abstract class Translation : NavMenuProps
{
public virtual Translation NavMenu() => this;
}
public class English : Translation
{
//...
public override Translation NavMenu()
{
//return base.NavMenu();
}
}

(Answer posted on behalf of the question author, to move it to the answer section).
The solution is not that much better than having just one giant interface but it does what I need, which is having manageable interfaces and the IDE telling me when I forgot to implement a field.
Solution inspired by the accepted answer:
public interface INavMenuProps
{
public string FirstMenuHeader { get; set; }
public string SecondMenuHeader { get; set; }
public string ThirdMenuHeader { get; set; }
}
public interface ILoginPageProps
{
public string FirstLabel { get; set; }
public string SecondLabel { get; set; }
}
public interface Translation : INavMenuProps, ILoginPageProps
{
}
public class English : Translation
{
public string FirstMenuHeader { get; set; } = "Dogs";
public string SecondMenuHeader { get; set; } = "Facilities";
public string ThirdMenuHeader { get; set; } = "Profile";
public string FirstLabel { get; set; } = "1st label";
public string SecondLabel { get; set; } = "2nd label";
}
public class Dutch : Translation
{
}
public class French : Translation
{
}

Related

How do I create a generic List using abstract class?

I have a Json class "GetAllDevices()". My JSON response consists of an Array/List of objects, where each object has the below common properties.
public class GetAllDevices
{
[JsonProperty("_id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("actions")]
public Action[] Actions { get; set; }
public class Action
{
public string _id { get; set; }
public Action_Def action_def { get; set; }
}
public class Action_Def
{
public string _id { get; set; }
public string name { get; set; }
}
}
I want to create 2 generic lists containing all the above properties based on its "type".
lstfoo1 List contains all the properties(_id, name type and actions) where type="foo1". Similarly, lstfoo2 is a List which contains the above properties where type="foo2".
What I have done so far:
string strJson=getJSON();
Foo1 lstfoo1=new Foo1();
Foo2 lstfoo2=new Foo2();
List<Foo1> foo1list= lstfoo1.GetDeviceData(strJson);
List<Foo2> foo2list = lstfoo2.GetDeviceData(strJson);
public class AllFoo1: GetAllDevices
{
}
public class AllFoo2: GetAllDevices
{
}
public abstract class HomeDevices<T>
{
public string type { get; set; }
public string _id { get; set; }
public List<AllFoo1> lstfoo1{ get; set; }
public List<AllFoo2> lstfoo2{ get; set; }
public abstract List<T> GetDeviceData(string jsonResult);
}
public class Foo1: HomeDevices<AllFoo1>
{
public Foo1()
{
type = "foo1";
}
public override List<AllFoo1> GetDeviceData(string jsonResult)
{
var lst =Newtonsoft.Json.JsonConvert.DeserializeObject<List<AllFoo1>>(jsonResult);
var lst1 = lst.Where(x => x.Type.Equals(type)).ToList();
return lst1;
}
}
public class Foo2: HomeDevices<AllFoo2>
{
public Foo2()
{
type = "foo2";
}
public override List<AllFoo2> GetDeviceData(string jsonResult)
{
var lst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AllFoo2>>(jsonResult);
var lst1 = lst.Where(x => x.Type.Equals(type)).ToList();
return lst1;
}
}
My question is, is there an easier way to do this using abstract classes? Can I directly convert my "GetAllDevices" class into an abstract class and inherit it and deserialize into it and create a generic list?
This should help, if I understand your problem correctly. Let me know if you have questions or it doesn't work as you need. I put this together really quickly without testing.
The way the Type property is defined could be improved but I left it as you had it.
public class MyApplication
{
public void DoWork()
{
string json = getJSON();
DeviceTypeOne foo1 = new DeviceTypeOne();
DeviceTypeTwo foo2 = new DeviceTypeTwo();
IList<DeviceTypeOne> foo1Results = foo1.GetDeviceData(json); // calls GetDeviceData extension method
IList<DeviceTypeTwo> foo2Results = foo2.GetDeviceData(json); // calls GetDeviceData extension method
}
}
// implemented GetDeviceData as extension method of DeviceBase, instead of the abstract method within DeviceBase,
// it's slightly cleaner than the abstract method
public static class DeviceExtensions
{
public static IList<T> GetDeviceData<T>(this T device, string jsonResult) where T : DeviceBase
{
IEnumerable<T> deviceDataList = JsonConvert.DeserializeObject<IEnumerable<T>>(jsonResult);
IEnumerable<T> resultList = deviceDataList.Where(x => x.Type.Equals(typeof(T).Name));
return resultList.ToList();
}
}
// abstract base class only used to house common properties and control Type assignment
public abstract class DeviceBase : IDeviceData
{
protected DeviceBase(string type)
{
if(string.IsNullOrEmpty(type)) { throw new ArgumentNullException(nameof(type));}
Type = type; // type's value can only be set by classes that inherit and must be set at construction time
}
[JsonProperty("_id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public string Type { get; private set;}
[JsonProperty("actions")]
public DeviceAction[] Actions { get; set; }
}
public class DeviceTypeOne : DeviceBase
{
public DeviceTypeOne() : base(nameof(DeviceTypeOne))
{
}
}
public class DeviceTypeTwo : DeviceBase
{
public DeviceTypeTwo() : base(nameof(DeviceTypeTwo))
{
}
}
// implemented GetAllDevices class as IDeviceData interface
public interface IDeviceData
{
string Id { get; set; }
string Name { get; set; }
string Type { get; }
DeviceAction[] Actions { get; set; }
}
// renamed and relocated class Action to DeviceAction
public class DeviceAction
{
public string Id { get; set; }
public DeviceActionDefinition DeviceActionDefinition { get; set; }
}
// renamed and relocated Action_Def to DeviceActionDefinition
public class DeviceActionDefinition
{
public string Id { get; set; }
public string Name { get; set; }
}
It should be simple enough to move the implementation of method GetDeviceData() to the base class.
For this to work, you will need to add a constraint on T so the compiler knows a bit more about the base type. You will also need to implement a constructor to populate the concrete type's type string you use around. This is a necessary measure to ensure the value is always populated as it is used for comparison in the method in question:
public abstract class HomeDevices<T> where T: GetAllDevices
{
public HomeDevices(string concreteType)
{
type = concreteType;
}
public string type { get; set; }
public string _id { get; set; }
public List<AllFoo1> lstfoo1 { get; set; }
public List<AllFoo2> lstfoo2 { get; set; }
//This method is now generic and works for both.
public List<T> GetDeviceData(string jsonResult)
{
var lst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(jsonResult);
var lst1 = lst.Where(x => x.Type.Equals(type)).ToList();
return lst1;
}
}
I hope that helps.

Interface Cannot implicitly convert type

I have following code below. I have two main interfaces IWatch and IWatchService. Oryginally Watch() was in IWatchService and there was no IWatch but since that CollectionService cannot use Watch() method i decided (ISP) to create IWatch interface additionally.In CollectionService i want in ctor pass either DatabaseWatchService or RemoteFilesWatchService therefore i put parameter type in ctor as IWatchService<IEntity> watchService nevertheless when in DoIt() method initialize fileWatcherServiceCsv variable it says:
Cannot implicitly convert type 'RemoteFilesWatchService' to
'IWatchService'. An explicit conversion exists (are you
missing a cast?)
public interface IWatch
{
void Watch();
}
public interface IWatchService<TDataEntity> where TDataEntity : IEntity
{
INotificationFactory NotificationFactory { get; }
ObservableCollection<TDataEntity> MatchingEntries { get; set; }
}
public interface IDatabaseWatchService<TDataEntity> : IWatchService<TDataEntity> where TDataEntity : IDatabaseEntity
{
IDatabaseRepository<IDbManager> DatabaseRepository { get; }
}
public interface IRemoteFilesWatchService<TDataEntity> : IWatchService<TDataEntity> where TDataEntity : IFileEntity
{
List<string> ExistingRemoteFiles { get; set; }
List<RemoteLocation> RemoteLocations { get; set; }
IWinScpOperations RemoteManager { get; set; }
IRemoteFilesRepository<IDbManager, TDataEntity> RemoteFilesRepository { get; }
}
public class RemoteFilesWatchService : IRemoteFilesWatchService<IFileEntity>, IWatch
{
public INotificationFactory NotificationFactory { get; }
public ObservableCollection<IFileEntity> MatchingEntries { get; set; }
public List<string> ExistingRemoteFiles { get; set; }
public List<RemoteLocation> RemoteLocations { get; set; }
public IWinScpOperations RemoteManager { get; set; }
public IRemoteFilesRepository<IDbManager, IFileEntity> RemoteFilesRepository { get; }
public RemoteFilesWatchService(IWinScpOperations remoteOperator,
IRemoteFilesRepository<IDbManager, IFileEntity> remoteFilesRepository,
INotificationFactory notificationFactory)
{
RemoteManager = remoteOperator;
RemoteFilesRepository = remoteFilesRepository; //csv, xml or other repo could be injected
NotificationFactory = notificationFactory;
}
public void Watch()
{
}
}
public class DatabaseWatchService : IDatabaseWatchService<DatabaseQuery>, IWatch
{
public INotificationFactory NotificationFactory { get; }
public ObservableCollection<DatabaseQuery> MatchingEntries { get; set; }
public IDatabaseRepository<IDbManager> DatabaseRepository { get; }
public DatabaseWatchService(IDatabaseRepository<IDbManager> databaseRepository,
INotificationFactory notificationFactory)
{
DatabaseRepository = databaseRepository;
NotificationFactory = notificationFactory;
}
public void Watch()
{
}
}
public class CollectionService
{
private IWatchService<IEntity> _watchService;
public CollectionService(IWatchService<IEntity> watchService)
{
_watchService = watchService;
}
}
class Run
{
void DoIt()
{
IWatchService<IEntity> fileWatcherServiceCsv = new RemoteFilesWatchService(new WinScpOperations(),
new RemoteCsvFilesRepository(new DbManager(ConnectionDbType.MySql)),
new NotificationFactory());
var coll1 = new CollectionService(fileWatcherServiceCsv);
}
}
public interface IEntity
{
}
public interface IFileEntity : IEntity
{
int Id { get; set; }
string Name { get; set; }
bool IsActive { get; set; }
bool RemoveFromSource { get; set; }
string DestinationFolder { get; set; }
RemoteLocation RemoteLocation { get; set; }
}
public interface IDatabaseEntity : IEntity
{
}
public class CsvFile : IFileEntity
{
public int ColumnHeader { get; set; }
public int ColumnsCount { get; set; }
public string Separator { get; set; }
public int ValuesRowStartposition { get; set; }
public int ColumnRowPosition { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public bool RemoveFromSource { get; set; }
public string DestinationFolder { get; set; }
public RemoteLocation RemoteLocation { get; set; }
}
public class XmlFile : IFileEntity
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public bool RemoveFromSource { get; set; }
public string DestinationFolder { get; set; }
public RemoteLocation RemoteLocation { get; set; }
public string SubNode { get; set; }
public string MainNode { get; set; }
}
This question gets posted almost every day. One more time!
A box of apples is not a box of fruit. Why not?
You can put a banana into a box of fruit, but you cannot put a banana into a box of apples, so a box of apples is not a box of fruit, because the operations you can perform on them are different. Similarly, a box of fruit is not a box of apples.
You're trying to use a IWatchService (box) of IFileEntity (apples) as an IWatchService of IEntity (fruit), and that's not legal.
Now, you might notice that in C# you can use an IEnumerable<Apple> where an IEnumerable<Fruit> is expected. That works just fine because there is no way to put a banana into an IEnumerable<Fruit>. In every member of IEnumerable<T> and IEnumerator<T>, the T comes out, not in.
If you are in that situation then you can mark your interface as
interface IWatchService<out T> ...
And the compiler will verify that every T in the interface is used in "out" positions, and then will allow the conversion you want.
That conversion is called a generic covariant conversion and it only works when:
The generic type is an interface or delegate
The type parameter is marked out, and the compiler verifies that is safe
The varying types (Fruit and Apple, say) are both reference types. You can't do covariant conversions involving int and object, for example.
Your RemoteFilesWatchService implements interface IWatchService<IFileEntity>, while your CollectionService expects a IWatchService<IEntity>. The two types are different, that's why it cannot convert.
Modify your CollectionService to accept IWatchService<IFileEntity> instead, or make RemoteFilesWatchService implement IRemoteFilesWatchService<IEntity>. Or use a non-generic interface in CollectionService instead.
You cannot have a IWatchService<IFileEntity> and treat it as a IWatchService<IEntity>. Compare it to a List<T> for example. You cannot expect to be able to do this:
class Animal {}
class Bird : Animal {}
class Elephant : Animal {}
var birds = new List<Bird>();
// compiler does not allow this...
List<Animal> animals = birds;
// ...because there is no point in adding elephants to a list of birds.
animals.Add(new Elephant());
Making a slight change to take support from variance, should fix your issue as follows:
public interface IEntity
{
}
public interface IFileEntity : IEntity
{
...
}
public interface IWatchService<out TDataEntity> where TDataEntity : IEntity //note the "out" keyword here.
{
}
You can learn more about Variance in Generic Interfaces Here

Can I create Interface Properties as Generic?

Is there anyway to create such interface which generates properties as generic.
public interface IInterface<T>
{
string nameOf(T)+"_Email" { get; set; } // this won`t compile
string nameOf(T)+"_Phone" { get; set; } // this won`t compile
}
public class Person
{
}
public class Details : IInterface<Person>
{
public string Person_Email { get; set; }
public string Person_Phone { get; set; }
}
I asked above question because my problem was as follow. I want to secure two classes with Interface contract. Then I combine these two classes in a ViewModel. Viewmodel is not really helping because I need these properties on Razor. Please see below.
public interface IPerson
{
string Email { get; set; }
string Phone { get; set; }
}
public interface IHotel
{
string Email { get; set; }
string Phone { get; set; }
}
public class Person : IPerson
{
public string Email { get; set; }
public string Phone { get; set; }
}
public class Hotel: IHotel
{
public string Email { get; set; }
public string Phone { get; set; }
}
public class ViewModel1 : IPerson, IHotel
{
//
// This is missing either Person or Hotel details
//
public string Email { get ; set ; }
public string Phone { get ; set ; }
}
public class ViewModel2 : IPerson, IHotel
{
//
// This is ok but PUBLIC modifier is not allowed, I cannot use.
//
string IPerson.Email { get ; set ; } // public modifier not allowed
string IHotel.Email { get ; set ; } // public modifier not allowed
string IPerson.Phone { get ; set ; } // public modifier not allowed
string IHotel.Phone { get ; set ; } // public modifier not allowed
}
No. It is not possible to dynamically modify the names of interface members with a class level generic argument.
Generics are designed to enable you to re-use the same functionality regardless of which generic type is specified. This is only possible if the interface remains consistent.
Consider this dilemma for example:
public class Foo<T>
{
public string GetPhone(IInterface<T> bar)
{
// how would I know what method to call on foo here?
return bar.????_Phone;
}
}
Below is an example of how you can have generic properties in an interface. You need a base class to translate for you and to bind the type you are looking for
public interface IInterface<T> where T : Contact
{
string Email { get; }
string Phone { get; }
}
public class Person : Contact
{
public string Phone { get; set; }
public string Email { get; set; }
}
public class DetailsBase<T> : IInterface<T> where T : Contact
{
Contact _cont { get; set; }
public string Email { get { return _cont.Email; } }
public string Phone { get { return _cont.Phone; } }
public DetailsBase(Contact cont)
{
_cont = cont;
}
}
public class Contact
{
public string Email { get; set; }
public string Phone { get; set; }
}
public class PersonDetails : DetailsBase<Person>
{
public PersonDetails(Person person) : base(person)
{
}
}

Specialize type when hiding property in subclass c#

i need an help on a really easy question about oop .net design.
I've an abstracted class:
public abstract class DataExtractionMethodConfig
{
public abstract DataExtractionMethod DataExtractionMethod { get; }
public object DataExtractionConfig { get; protected set; }
}
where DataExtractionConfig is a simple object that will be used in subclass to contains the configuration details. For example the OdbcExtractionConfig will be
public class OdbcExtractionConfig
{
public string OdbcName { get; set; }
public string BarcodeFilterExpression { get; set; }
public string ConnectionString { get; set; }
public string SqlCommand { get; set; }
public Enum.DatabaseServer DatabaseType { get; set; }
public List<string> KeyColumns { get; set; }
}
and the derived concrete class is
public class OdbcDataExtractionMethod : DataExtractionMethodConfig
{
private OdbcDataExtractionMethod(){}
public OdbcDataExtractionMethod(OdbcExtractionConfig config)
{
this.DataExtractionConfig = config;
}
public override DataExtractionMethod DataExtractionMethod { get { return DataExtractionMethod.ODBC; } }
}
How can i refactor the code to have DataExtractionConfig typed and not a simple object but mantaining the capability to extend the abstract class ?
How can i refactor the code to have DataExtractionConfig typed and not a simple object
By using Generics, but I warn you it can sometimes be a rabbit hole! Meaning that once you start down it, you get deeper and deeper, and things get more and more complex as you go.
public abstract class DataExtractionMethodConfig<T>
{
public abstract DataExtractionMethod DataExtractionMethod { get; }
public T DataExtractionConfig { get; protected set; }
}
And then
public class OdbcDataExtractionMethod : DataExtractionMethodConfig<OdbcExtractionConfig>
{ ... }

Trying to work out these interfaces

I'm trying to create some interfaces. The IReportSection object will have one string and a collection of items, which could be different depending on what we're working with. Do I need to make it generic?
The IReport will have one string and a collection of IReportSection.
Here's how I'm trying to define it now.
public interface IReport
{
string ReportName { get; set; }
ICollection<IReportSection> ReportSections { get; }
}
public interface IReportSection
{
string ReportSectionName { get; set; }
ICollection ReportItems { get; }
}
public abstract class ReportSectionBase : IReportSection
{
public string ReportSectionName { get; set; }
public ICollection ReportItems { get; set; }
}
And my models:
pulic class ProjectSubmissionViewModel
{
public int ProjectSubmissionId { get; set; }
public string SubmissionTitle { get; set; }
}
pulic class AffiliateViewModel
{
public int AffiliateId { get; set; }
public string AffiliateName { get; set; }
}
This is how I'm trying to use it in code:
public class ChapterAffiliates : ReportSectionBase
{
public string ReportSectionName { get { return "Chapter Affiliates"; } }
public ICollection<AffiliateViewModel> ReportItems { get; set; }
}
public class ChapterTitles : ReportSectionBase
{
public string ReportSectionName { get { return "Chapter Titles"; } }
public ICollection<ProjectSubmissionViewModel> ReportItems { get; set; }
}
public class SubmissionListViewModel : IReport
{
public ICollection<ProjectSubmissionViewModel> Submissions { get; set; }
public ICollection<AffiliateViewModel> Affiliates{ get; set; }
public string ReportName { get; set; }
public ICollection<IReportSection> ReportSections
{
get
{
var affiliateSection = new ChapterAffiliates
{
ReportItems = Affiliates
};
var titleSection = new ChapterTitles
{
ReportItems = Submissions.Where(s => s.SubmissionTitle.Contains("SomePhrase")).ToList()
};
var sections = new List<IReportSection> { {subSection}, {titleSection} };
return sections;
}
}
}
I'm not sure how to best define this. I'm pretty sure I've done it before, but it's not coming to me.
Are the type parameters for TRType all the same within a certain report? E.g. will you have report sections with different report types in them?
If all types within a report are the same, the solution is relatively simple:
public interface IReport<T> { ... }
If this is not the case - you'll have to do something different, e.g:
public interface IReportSection
{
string ReportSectionName { get; }
ICollection ReportItems { get; }
}
public abstract class ReportSectionBase<TRType> : IReportSection {
...
}
This allows you to put different underlying types in the ReportSections collection related to the report. You'll have to do some more work to get the exact information that you need out of each report section.

Categories

Resources