开发者

NHibernate.Search mapping unknown classes

An assembly contains classes like

public class CustomPage : Page, ISearchable
{
    [Searchable]
    public virtual string CustomText { get; set; }
    public virtual string SearchableText { get; set; }
}

which I would like to map to Lucene.net. I can't add attributes to those classes and can't use ISearchMapping either, because the types are unknown (no reference to that assembly).

How can I do the mappings? All available information is that all types inherit Page and implement ISearchable. The only property to map is the SearchableText (before saving all the p开发者_开发问答roperties with [Searchable] are concatenated to that property).


You can use this design Generic DAO Object. I built an entire infrastructure using this implementation. You create an generic DAO and use it with any type of mapped object.

public GenericDAO<T>
{
     public List<T> Search(Criteria[] criterias)
     {}

     public T Save(T entity)
     {
     }

     public T Update(T entity)
     {
     }

     Public void Delete(T entity)
     {
     }

}

// any mapped object.
public MyMappedObject
{
   public virtual string ID {get; set;}
   public virtual string Name {get; set;}

   public MyMappedObject()
   {
       ID = Guid.NewGuid().ToString();
   }
}

// usage in code.
MyMappedObejct myMappedObjectInstance = new MyMappedObejct();
myMappedObjectInstance.Name = "new name";

GenericDAO<MyMappedObejct> myMappedObejctDao = new GenericDAO<MyMappedObject>();

// Insert
myMappedObjectInstance = myMappedObejctDao.Save(myMappedObjectInstance);

// Select
MyMappedObejct myMappedObjectInstance = myMappedObejctDao.Search().First();

// Update
myMappedObjectInstance.Name = "another name";
myMappedObjectInstance = myMappedObejctDao.Update(myMappedObjectInstance);

// Delete
myMappedObejctDao.Delete(myMappedObjectInstance);

This is a very simplified example, I made a much more complex version across more than a hundred classes and it works great.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜