Need help implementing interfaces
in my application I am wanting to make my interfaces more generic. As it stands I have Two Models, CampaignModel and StringModel, these both use the same methods but with different arguments in them i.e.
CampaignModel:
void GetAll(EventHandler<GetAllCampaignsCompletedEventArgs> eh);
void GetAllByName(string name, EventHandler<GetCampaignCompletedEventArgs> eh);
void GetAllByID(long id, EventHandler<GetAllCampaignsByIDCompletedEventArgs> eh);
void Add(Campaign entity, EventHandler<CreateCampaignCompletedEventArgs> eh);
void Update(Campaign entity, EventHandler<UpdateCampaignCompletedEventArgs> eh);
void Delete(Campaign entity, EventHandler<DeleteCampaignCompletedEventArgs> eh);
StringsModel:
void GetAll(EventHandler<GetAllCampaignStringsCompletedEventArgs> eh);
void GetAllByName(string name, EventHandler<GetCampaignStringByIdentifierCompletedEventArgs> eh);
void GetAllByID(long id, EventHandler<EventArgs> eh);
void Add(CampString entity, EventHandler<EventArgs>开发者_运维百科; eh);
void Update(CampString entity, EventHandler<EventArgs> eh);
void Delete(CampString entity, EventHandler<EventArgs> eh);
I am just needing direction as to how to create a more generic interface that I can use. The reason for this is I only want one set of Views for each Models .i.e only one Add,Update and Delete view that will call the methods from either the Campaign Model or the Strings Model. If you have any ideas how to get this done I would appreciate it.
Thanks, Stuart.
If you look at the code you can see that, at least in the functions you provided, what changes between two clases is a type of paremeters. The common approach here is to define a Base type for every common group, so here is pseudocode:
public class GetCampaignCompletedEventArgs : BaseEventArg {}
public class GetCampaignStringByIdentifierCompletedEventArgs: BaseEventArg {}
...
...
public class Campaign : BaseCampaign {}
public class CampString: BaseCampaign {}
After this defin Interface or AbstractClass
public interface IBase {
void GetAllByName(string name, EventHandler<BaseEventArg > eh);
...
}
Just to give yuor idea, its difficult to me write somethign really concrete , cause its depends on yuor implementaiton.
Hope this helps.
精彩评论