Is it possible to do ORM mapping between classes and the DB using Linq to SQL or NHibernate just by using attributes on the models?
For eg: If you use Parse.com as the backend, establishing a relationship between the DB and the class is as simple as:
[ParseClassName("itemsForSale")] //Name of the table
public class Item : ParseObject
{
[ParseFieldName("userId")] //Name of the column
public string UserId
{
get { return GetProperty<string>(); }
set { SetProperty<string>(value); }
}
}
Nothing else is required. No mapping files, no designer files. I was wondering, in the event I have to replace the Parse backend using SQL server, will I be able to achieve something similar?
Linq to SQL and NHibernate seems to need configuration files for it to work. Is there something else I can use?
In most of our company projects we use Linq to SQL just for linq enabled querying of database views and we use only attributes for mapping their rows to classes. We use ColumnAttribute and TableAttribute to indicate our view name (table name) and its column names. More info: https://msdn.microsoft.com/en-us/library/system.data.linq.mapping(v=vs.110).aspx
This way we use no external mapping mechanisms but attributes in our view result classes.
We also use NHibernate with attribute mappings very often. We use FluentNhibernate to set up conventions and use no mapping classes. Just conventions and attributes.
Here is code that returns NHibernate configuration object with all conventions set.
IMO this topic is too advanced and user scenario specific to elaborate in single answer. I'll list for you few classes that might be useful for what you are trying to achieve.
AutomappingConfiguration - this is class inherits after FluentNhibernate.Automapping.DefaultAutomappingConfiguration class and it helps NHibernate to guess which classes to map as entities and which of their properties should be mapped (ShouldMap, IsVersion, IsComponent, IsCollection methods might be interesting for you).
Yo can see that we use a lot of "model.Conventions.Add" method. We instantiate and add to NHibernate configuration our convention instances. They implement NHibernate from FluentNhibernate.Conventions namespace like: IPropertyConvention, IReferenceConvention, IHasManyConvention, ICollectionConvention etc.
You can learn more about FluentNHibernate conventions automapping here: https://github.com/jagregory/fluent-nhibernate/wiki/Conventions.
As I said, automapping is large and very individual topic but I hope that I was able to show you a way.