orchardcmsnhibernate-mappingorchardcms-1.8

Orchard CMS record mapping from external Assembly


I have an Orchard CMS module that uses external library. And I need to use some classes from that library as part of Orchard records. For example, external assembly contains class

public class Operation {
    public virtual long Id { get; set; }
    public virtual string OperationType { get; set; } 
}

I have to store it in the database, to use it with Orchard IRepository and use it as part of other Orchard CMS records, such as

public class HistoryRecord {    
    public virtual long Id { get; set; }
    public virtual DateTime Updated { get; set; }
    public virtual Operation Operation { get; set; }
}

Solution

  • I was able to get a partial solution, based on Fluet Configuration. However, it works only if the classes correspond to the Orchard's naming conventions. Here it is:

    public class SessionConfiguration : ISessionConfigurationEvents {
        public void Created(FluentConfiguration cfg, AutoPersistenceModel defaultModel) {
            var ts = new TypeSource(new[] { typeof(OperationRecord) });
            cfg.Mappings(m => m.AutoMappings.Add(AutoMap.Source(ts)
                    .Override<OperationRecord>(mapping => mapping.Table("Custom_Module_OperationRecord"))
                ));
        }
    
        public void Prepared(FluentConfiguration cfg) { }
        public void Building(Configuration cfg) { }
        public void Finished(Configuration cfg) { }
        public void ComputingHash(Hash hash) { }
    }
    
    
    public class TypeSource : ITypeSource {
        private readonly IEnumerable<Type> _types;
        public TypeSource(IEnumerable<Type> types) {
            _types = types;
        }
        public IEnumerable<Type> GetTypes() {
            return _types;
        }
        public void LogSource(IDiagnosticLogger logger) {
            throw new NotImplementedException();
        }
        public string GetIdentifier() {
            throw new NotImplementedException();
        }
    }