Question

I have the following class:

public class Foo
{
    public int Id { get; set; }
    ...
    public Boo Boo1 { get; set; }
    public Boo Boo2 { get; set; }
}

I want to exclude Boo1 and Boo2 properties but I don't want to decorate those properties with PetaPoco.Ignore attribute. I want to have pure POCO objects. Can I execute Ignore command in code or do I have to create query/stored procedure and manually map all fields?

Any help would be greatly appreciated!

Was it helpful?

Solution

Looks like PetaPoco can't be told in any other way that fields/properties should be ignored than by using attributes. You can either Ignore a few members, or if you're not mapping the majority of a class then you can specify explicit column mapping for the class and decorate the ones you DO want mapped. I understand your hesitance to add ORM-specific cruft to a "pure" POCO, but unfortunately that information has to be somewhere, and as PetaPoco doesn't use mapping files (or much of a configuration at all, really), the class is where it goes.

The only thing you could do is create a DTO/DAO that will be what is mapped, then create implicit or explicit operators to convert between the domain class and its DTO. The DTO, then, can simply not have the fields you don't want to include. That keeps both classes POCO (depending on your feelings regarding operator methods), and it just adds a relatively simple step of casting the query result to your domain class.

OTHER TIPS

In my branch here: https://github.com/schotime/PetaPoco

You can fluently describe your models like I have described here: http://schotime.net/blog/index.php/2011/05/16/fluent-petapoco-external-mappings/ and also use the convention based mapping like here: http://schotime.net/blog/index.php/2012/02/13/petapoco-convention-based-fluent-mapping/

This is a great place for an anonymous type.

In your method for saving foo

public void InsertFoo(Foo f)
{
    var db = new Database("connection");           
    var petaPocoFooObj = new {f.Id}
    db.Insert("FooTable", "FooId", petaPocoFooObj);
}

It's just a little more work, although it could be a PITA if your classes are deeply nested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top