Is this possible using NHibernate or Linq to SQL?

2019-09-19 02:03发布

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?

1条回答
对你真心纯属浪费
2楼-- · 2019-09-19 02:25

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.

return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012.ConnectionString(c => c.FromConnectionStringWithKey(Stale.NazwaPolaczeniaERP)).DefaultSchema("dbo"))
.Mappings(m =>
{
    var model = AutoMap.Assemblies(new AutomappingConfiguration(),
    new Assembly[]
    {
        typeof(ERP.DomenaERP.ZnacznikZasobu).Assembly
    });

    model.Conventions.Add(new SetEnumTypeConvention());
    model.Conventions.Add(new ColumnConvention());
    model.Conventions.Add(new CollectionAccessConvention());
    model.Conventions.Add(new OptimisticLockIgnoreConvention());
    model.Conventions.Add(new SqlTimestampConvention());
    model.Conventions.Add(new SetTableNameConvention());
    model.Conventions.Add(new VersionConvention());
    model.Conventions.Add(new HasManyConvention());
    model.Conventions.Add(new InheritanceConvention());
    model.Conventions.Add(new ColumnNameConvention());

    model.Conventions.Add(DefaultLazy.Always());
    m.AutoMappings.Add(model);
})

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.

查看更多
登录 后发表回答