I believe I do not understand the full purpose of the interfaces. Therefore, I am asking an advice on this matter.
I have several tables on my database. Few are: Languages, FrontEndMenus, BackEndMenus…
These tables have different columns, so the C# objects have also different properties (even the FrontEnd and BackEnd menu objects have several different fields)
For performance purposes, they are held on RAM in SINGLETON classes named (LanguageCollection, FrontEndMenuCollection, BackEndMenuCollection)
First of all, all of these objects have definitely 3 methods which are “IsValid”, “Save” and “Remove”.
Normally they are used like:
LanguageItem item1 = new LanguageItem();
item1.IsValid();
item1.Save();
item1.Remove();
FrontEndMenuItem item2 = new FrontEndMenuItem();
item2.IsValid();
item2.Save();
item2.Remove();
//And Same as BackEndMenus class too..
And when I want to get one of the objects in the singleton collections, I call them like:
FrontEndMenuItem item = GetInstance.FilterSingleOrDefault(new FrontEndMenuItemArgs{ Id = 14 });
And here is my question:
According to my understanding, in order to make a flexible structre I should use an Interface implementation on these objects classes (FrontEndMenuItem, LanguageItem, BackEndMenuItem)
I did something like:
public interface IBusinessItem
{
bool IsValid();
OperationResult Remove();
OperationResult Remove(bool isCommitOnDatabase);
OperationResult Save();
OperationResult Save(bool isCommitOnDatabase);
}
But I’m not sure how I can use it…
Could you give me a little direction on this approach? I’d extremely happy if you could help me by writing very simple codes.
I added as a comment but since it is very important, I’d like to add this part to my existing question as well:
When an update is necessary or insert, the object itself is updated by its fields and then saved. Its like this:
FrontEndMenuItem item2 = new FrontEndMenuItem();
item2.Title = "Some title";
item2.ModuleId = 5;
item2.LinkTypeId = 2;
item2.Save(); //Since its a new item its gonna be saved as new that means INSERT.
Save methods can understand whether its new or not once it has been called from the object itself like item2.Save() so this method will also work when its called by the IBusinessItem interface.
But, what about the fields? In BackEndMenuItem there is no ModuleId field or LinkTypeId
Could there be IBusinessItem.ModuleId = 5 ? If this is the case, should I use an interface, if not, in what situation could I use interfaces on this problem?
Thank you very much,
Basically, the use of an interface is to prevent consuming code from having to know which of these two specific implementations (LanguageItem and FrontEndMenuItem) it is dealing with if it doesn’t have to. All it has to know is that any implementation of IBusinessItem has the list of methods you define.
Interfaces thus provide “loose coupling”; you don’t have to know what an object IS, you just have to know what it DOES, or more accurately, what you can do to/with it.
Now, if you somehow must know that a FrontEndMenuItem is a FrontEndMenuItem in your usage, then being able to treat it as an IBusinessItem isn’t going to gain you much. However, I would then question your usage; it looks like both implementations work exactly the same way, so why couldn’t LanguageItem be used in place of a FrontEndMenuItem? There are some good reasons, but there are also some very bad reasons to set it up this way.