We share a single poco's with
- ServiceStack WebServices
- ServiceStack Orm with MySQL
- Xamarin mobile client that uses Sqlite.net.
Problem is the shared classes have become a mess.
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
#if __MOBILE__
[Indexed]
#else
[Index]
#endif
public string UserAccountId { get; set; }
#if __MOBILE__
[Indexed]
#else
[Index]
#endif
And if it's not bad enough already, i need to
- Constrain the length of each field in the database....
- Save these objects in MongoDb. Fortunately, they have an AutoMapper class that does this at runtime.
Not sure what to do. My Failed ideas include:
Try to use: [Conditional("DEBUG")]. But it does nothing for this
It appears that sqlite.net uses its own attribute
[AttributeUsage (AttributeTargets.Property)] public class IndexedAttribute : Attribute
So it won't find the Mysql [Index] attribute
Could try to include two attributes on each property
[Indexed] [Index] public string UserAccountId { get; set; }
I tried to turn it into a two one line'ers but c# VS complains
#if __MOBILE__ [Indexed] #endif #if __MOBILE__ [Index] #endif
In the end, the only approach that ** APPEARS ** will work is to just keep the interface as the single definition of the class and have many concrete classes that get decorated differently.
Any Ideas??
If you were to architect your code as per Uncle Bob's Clean Architecture, you wouldn't attempt to have a single representation to be used for many different purposes, as it causes your dependencies to point outwards from high-level policy towards outer circles.
Source: https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
Effectively, you could put an Entity in the centre circle, along with enterprise business rules, but the DTO you use to store/retrieve data for a particular database should be way out in your "frameworks and drivers" circle.
All of those database-specific attributes belong on DTOs in the blue circle, not on the entities in the centre.