开发者

What should be the better way to design data layer, multiple interfaces or single interface when some functions are shared?

开发者 https://www.devze.com 2023-03-30 21:10 出处:网络
Data stores in the solution that I am architecting are databases, queues and Csv files. Some of the functionality is common in both of them E.g. Csv files and the DBs both have GetNames() method - na

Data stores in the solution that I am architecting are databases, queues and Csv files.

Some of the functionality is common in both of them E.g. Csv files and the DBs both have GetNames() method - names info resides in CSV and DB, where as some methods are only valid for DB and not CSV e.g. GetAddressOfPerson(Person person) is a method that will work for DB as DB contains address information but not for CSV files as they do not have address information.

Another example are properties. Some properties are common e.g. bool IsTestMode is common for both DAO but some are not e.g. PersonsFilePath

Whilst architecting, I have created an interface: IPersonDao and it contains all of the methods and the properties which 开发者_运维技巧any of the class with implement.

The concerete implementation IPersonCsvDao and IPersonDBDao and IPersonQueueDao implement all of the methods and properties, however, they throw "Not Implemented Exception" in their implementations if the method does not belong to the DAO e.g. CsvDao:

List<Address> GetPersonAddress(Person person)
{
throw new NotImplementedException();
}

DBDao:

List<Address> GetPersonAddress(Person person)
{
//connect to db table
//return list<address>
}

QueueDao:

List<Address> GetPersonAddress(Person person)
{
throw new NotImplementedException();
}

I am not sure if this is correct because there are many methods which have Not Implemented Exception. I am not sure what should be the best way to create data layer for this solution?


If you follow the SOLID principles, I think that you should split that into multiple interfaces. This also makes it possible to "detect" what methods are supported and which aren't, by doing an is check or an as cast.


As muc as I understood you're going to implement tree like structure, where classes present have a common properties (write them in mainstream base class/interface), but at some level of derivation, classes become to defere slitly more (some of them still share same functions/properties, some of them share others), from that point you're gonna to implement a new base class for every group of classes you have.

Hope it's clear what I mean.

0

精彩评论

暂无评论...
验证码 换一张
取 消