C # PER-FILE Constant Constant?

Is it possible to define a compiler constant based on PER-FILE / project-item?

Background: I want to achieve a database abstraction layer (DAL) that separates all read and write tasks, but retains a DAL that can do both, but without implementing the same methods several times (an abstract class means that for each instance there will be 1 instance class for each supported database type).

So, I want to separate my DAL as follows:

abstract class ReadDAL abstract class WriteDAL abstract class ReadWriteDAL (multiple-inheritance from Read&Write-DAL). 

Unfortunately, this does not work, because C # does not support multiple inheritance.

Thus, one way to solve this problem would be to define interfaces:

 abstract class ReadDAL : IReadDAL abstract class WriteDAL : IWriteDAL abstract class ReadWriteDAL : IReadDAL, IWriteDAL 

However, if I do this, I will have to change the interface definition every time I change the method in one of the DALs and change the methods defined in ReadWriteDAL, and I need to copy-paste somewhere the implementation of the method, which means there will be a DRY mess - mismatch.

I realized what I could do by adding the same file a second time as a link and having a definition for each project element:

 #if SOMECONSTANT // true if file is PartialReadDAL.cs public partial abstract class ReadDAL #else // false if "file" is link called "PartialReadWriteDAL.cs" symlinking to PartialReadDAL.cs public partial abstract class ReadWriteDAL #endif and here some implementation. 

But can I somehow determine the compiler constant for each file?

Or achieve the same effect?

+4
source share
1 answer

The symlink route will be very, very confusing. When you do this, I would do it by adding some #define to the appropriate files as a pre-build step. Then I would #if for the presence of these characters in the code. I would not like it at all: I assume that it will not be as transparent as I would like, even if I cleared these markers after the assembly was completed so that it would not fall into version control.

Can ReadWriteDAL contain any of its own state, or will it just be a dispatcher for method calls in ReadDAL and WriteDAL ? If this is just a dispatcher, you can discard the actual implementation ( ReadWriteDAL ) and pass the IReadDAL and IWriteDAL calls registered in the root of the composition using a dynamic proxy mechanism . I wrote a tool like this for Castle Windsor.

0
source

Source: https://habr.com/ru/post/1013044/


All Articles