How to develop a 'content part' with my own data access framework?

Topics: Customizing Orchard, Writing modules
Nov 15, 2011 at 8:55 AM

how to using my own data access on content part handler? after google I saw the link http://stackoverflow.com/questions/4727843/how-to-change-orchard-record-repository

My Handler Code:

public class QueryPersonsHandler : ContentHandler
    {
         public QueryPersonsHandler(ITestService service)//ITestService implement IDependency
         {
             OnLoaded<QueryConditionPart>(CustomInitializing);//not fire
         }

        private void CustomInitializing(LoadContentContext ctx, QueryConditionPart part) 
        {
            var d = part.Id;//I just want to get data from my mongodb data access layer,and fill to the part
        }
    }
My Model:
namespace QueryPersons.Models {
    
    public class QueryCondition : ContentPartRecord {
        public virtual string IdCardNo { set; get; }
        public virtual string Name { set; get; }
    }

    public class QueryConditionPart : ContentPart
    {
        [Required]
        public string IdCardNo { set; get; }

        public string Name { set; get; }
    }
}

Please give me a piece customize handler code...
Thanks a lot

Coordinator
Nov 15, 2011 at 6:14 PM

Do you mean that if you put a breakpoint in the constructor it is never hit or that CustomInitializing never gets hit?

Nov 15, 2011 at 6:35 PM

The model should be called "QueryConditionPartRecord", not "QueryCondition" - things tend to fail if you don't follow the conventions. What does your Migrations look like?

Nov 16, 2011 at 2:10 AM
bertrandleroy wrote:

Do you mean that if you put a breakpoint in the constructor it is never hit or that CustomInitializing never gets hit?


Thanks reply,I mean the CustomInitializing never gets hit

Nov 16, 2011 at 3:08 AM
Edited Nov 16, 2011 at 3:08 AM
randompete wrote:

The model should be called "QueryConditionPartRecord", not "QueryCondition" - things tend to fail if you don't follow the conventions. What does your Migrations look like?

Thanks reply,it's my Migrations:

 

public class Migrations : DataMigrationImpl
    {

        public int Create()
        {
	    //I don't want to persist the query condition record in database
            ContentDefinitionManager.AlterPartDefinition(
                "QueryPersons", cfg => cfg.Attachable());

            return 1;
        }
    }


Actually,I just want to create a content part,this part is a function that find persons by some query condition('Name' or 'IdCardNo'),the condition have not need to persist to the database


Nov 16, 2011 at 3:18 AM

Unfortunately, to create a part you need a record, and to create a record you need a Migration.

So you need a SchemaBuilder.CreateTable(...) to persist the record to the database, even if that table will have no columns of its own.

Finally - your part is called "QueryCondition" so why are you creating the part "QueryPersons" in your Migration?

Coordinator
Nov 16, 2011 at 3:31 AM

No, a part does not require a record.

Nov 16, 2011 at 6:55 AM
randompete wrote:

Unfortunately, to create a part you need a record, and to create a record you need a Migration.

So you need a SchemaBuilder.CreateTable(...) to persist the record to the database, even if that table will have no columns of its own.

Finally - your part is called "QueryCondition" so why are you creating the part "QueryPersons" in your Migration?

"Finally - your part is called "QueryCondition" so why are you creating the part "QueryPersons" in your Migration?" It's my mistake,i change to :

 public int UpdateFrom1() {
            SchemaBuilder.CreateTable("PersonQueryModelRecord", table => table
                                                                             .ContentPartRecord());

            ContentDefinitionManager.AlterPartDefinition(
                typeof(QueryConditionPart).Name, cfg => cfg.Attachable());
            return 2;
        }
but it's still not work

Nov 16, 2011 at 7:01 AM
Edited Nov 16, 2011 at 7:30 AM
bertrandleroy wrote:

No, a part does not require a record.

thanks bertrandleroy, If i  must be implement 'IRepository'?( i just want to create a query function,there is a simple way if i don't implement IRepository completely?)

I'm sorry,I am a green hand on the orchard

Nov 16, 2011 at 1:11 PM

Maybe the problem is you need to actually attach your part to a content type? You will probably want to create a new content type to do this. Make sure you include CommonPart, all content types should have it.

You can do this under Content -> Content Types tab in Admin.

I think what Bertrand was saying is that since your QueryConditionPart is just a ContentPart, not a ContentPart<TRecord>, you don't actually need the ContentPartRecord to back it.