DarthFubuMVC / fubumvc

A front-controller style MVC framework for .NET
http://fubumvc.github.io
Other
427 stars 151 forks source link

Register configurations from document type class #983

Open bradyclifford opened 8 years ago

bradyclifford commented 8 years ago

Add ability to register indexes, foreign keys, grants, other document mapping needs, etc. for a document type from within the document type class using a static method called ConfigurePersistence(MartenRegistry).

If FubuMVC.Marten sees that in a document type, call it before creating the DocumentStore.

public class DocumentTypeExample
{

   public Guid Id { get; set; }
   public string FirstName { get; set; }
   public int AssigneeId { get; set; }
   public int Number { get; set; }

   public static void ConfigurePersistence(MartenRegistry registry) 
   {
        registry.MappingFor(this).DatabaseSchemaName = "other";        
        registry.For(this).ForeignKey<User>(x => x.AssigneeId);
        registry.For(this).GinIndexJsonData();

        registry.For<User>().Duplicate(x => x.FirstName, configure:idx =>
        {
            idx.IndexName = "idx_special";
            idx.Method = IndexMethod.hash;
        });

        registry.For(this).Index(x => x.Number, x =>
        {
            x.Method = IndexMethod.brin;
            x.Casing = ComputedIndex.Casings.Lower;
            x.IndexName = "mt_my_name";
            x.IsConcurrent = true;
            x.IsUnique = true;
            x.Where = "(data ->> 'Number')::int > 10";
        });

   }

}
bradyclifford commented 8 years ago

Maybe the Attributes better serve this purpose?

jeremydmiller commented 8 years ago

I went for a public static ConfigureMarten(DocumentMapping) convention on DocumentMapping classes here. If we want to do more discovery via type scanning, I'd rather build that out into FubuMVC.Marten where we can use the more efficient StructureMap type scanning.