I have looked over the Repository pattern and I recognized some ideas that I was using in the past which made me feel well.
However now I would like to write an application that would use this pattern BUT I WOULD LIKE TO HAVE THE ENTITY CLASSES DECOUPLED from the repository provider.
I would create several assemblies :
- an "Interfaces" assembly which would host common interfaces including the IRepository interface
- an "Entities" assembly which would host the entity classes such as Product, User, Order and so on. This assembly would be referenced by the "Interfaces" assembly since some methods would return such types or arrays of them. Also it would be referenced by the main application assembly (such as the Web Application)
- one or more Repository provider assembly/assemblies. Each would include (at least) a class that implements the IRepository interface and it would work with a certain Data Store. Data stores could include an SQL Server, an Oracle server, MySQL, XML files, Web / WCF services and so on.
Studying LINQ to SQL which looks very productive in terms of time taken to implement all seems well until I discover the deep dependency between the generated classes and the CustomDataContext class.
How can I use LINQ to SQL in such a scenario?
I did something similar with WCF
1 On your DBML, set you Serialization mode to Unidirectional
2 Set ALL columns on your tables to UpdateCheck=false
3 Write your service something like the following:
4 Add a service reference
Could your Entity classes implement IProduct, IUser, IOrder etc. interfaces that would be declared in your "Interfaces" assembly? This way the IRepository interface references only the business object interfaces (i.e., returns collections of IProduct etc.) and the "Interfaces" assembly is decoupled from your other implementation-specific assemblies.
I think you want POCO (Plain Old CLR Objects) support. LINQ to SQL has a adapter called Close2Poco.
But I would advise making the switch to Entity Framework, at the moment they also have a POCO adapter, but in v2 its expected to be supported out of the box.