ORM – how to use dapper Rainbow (or optionally dapper. Contrib) inserts and updates objects with navigation properties

Recently I started studying dapper I'm testing it and can do basic crud. I mean, the basic is to work in a class and this structure:

public class Product {
    public int Id {get;set;}
    public string Name {get;set;}
}

Now I'm looking for something that makes it easier to insert and update and find dapper Rainbow's stuff I checked it and was able to use it to get and insert objects, as described above My problem is that when the product has navigation properties, I can't insert in this field So if I have this:

public class Product {
    public int Id {get;set;}
    public string Name {get;set;}
    public ProductCategory Category {get;set;}
}

I will not be able to do this:

// connection is a valid and opened connection            
 var db = TestDatabase.Init(connection,300);
 var newId = db.Products.Insert(newProduct);

For this reason:

The member Category of type ProductCategory  cannot be used as a parameter value

If I replace the category with type int (the same data type in the database), I can solve the problem However, if I do, I will not be able to use its category information to query products, not just (category) ID

So without resorting to the original dapper, how can I insert and update using the class of navigation properties? I wish I could do the following and tell dapper Rainbow ignores categories when inserting or updating

public class Product {
    public int Id {get;set;}
    public string Name {get;set;}
    public ProductCategory Category {get;set;}
    public int CategoryId {get;set;} // this will be the same field name in the database
}

In this scenario, NHibernate can be used. I can have a category of proxy object, assign it to the product and save it, and the mapping works perfectly But I really want to use dapper, which is why I am exploring and want to learn how to do such things

Solution

No, dapper Rainbow

It's impossible, dapper Rainbow is in its current form, but my pull request in GitHub makes this possible

I was surprised that no one suggested using dapper Contrib. I know. I asked rainbow if it had a function But I didn't expect anyone to notice this statement (especially in BOLD):

... and propose an alternative, a solution already in the dapper library I think I should be more clear about my problem and ask if a solution exists somewhere in the whole dapper library that is in GitHub So after digging more libraries, I found a problem supporting me

Dapper. Path to contrib

All these work well with my project and rainbow until I need more work I have some tables with many fields If I just give rainbow my object, it will update all the fields, which is not very good However, this does not mean that I will jump out of the boat and return to NH soon So before I implement my own change tracking, I don't want to reinvent it, especially if someone has done a good job and I have been online on July 2 This thread confirms my knowledge that rainbow does not support change tracking, but another beast, it is called dapper Contrib. So I started trying

So we'll meet again

I have the same problem as rainbow Contrib does not support navigation properties! I began to feel that I was wasting my time with dapper, and the performance I sought would only be wishful thinking Until

Writeattribute came to the rescue

This kind lives in dapper Sqlmapperextensions. Contained in the contrib project CS file I didn't find any documents about the course, and I didn't have any comments that could be easily found. I spoke to me and said hey, I'm what you're looking for When I placed the rainbow as described above, I came across it

The usage of this class is the same as I use ignorepropertyattribute. It is a property that you can use to decorate the properties of your class You should use this attribute to decorate any attributes that do not need to be included in the SQL created by dapper So in my example, I told dapper to exclude the category fields I need to do:

public class Product {
    public int Id {get;set;}

    public string Name {get;set;}

    [Write(false)] // tell Dapper to exclude this field from the sql
    public ProductCategory Category {get;set;}

    public int CategoryId {get;set;}
}

I'm coming

Remember, I went to contrib because of the change tracking function This so thread, the same link I mentioned above, points out that to track changes, you need to provide an interface for your course and use it with contrib So for my example class, I need to have:

public interface IProduct {
    int Id {get;set;}
    string Name {get;set;}
    ProductCategory Category {get;set;}
    int Category {get;set;}
}

// and implement it on my Product class
public class Product : IProduct {
    public int Id {get;set;}

    public string Name {get;set;}

    [Write(false)]
    public ProductCategory Category {get;set;}

    int Category {get;set;}
}

I thought so, almost! You might ask me why I need to define categories in my interface if dapper doesn't care about it at all In fact, this will only cause one problem, and I will solve it

In my case, sometimes I need to work on the category field while keeping track of changes to the product object In order to maintain the tracking function, the following interface types should be used to provide get call:

var product = connection.Get<IProduct>(id);

And through this call, if I do not define it in the interface, I will not be able to access the category field But if I define it in my interface, I get a familiar error

Really? Please stop

sentence

Don't worry. It's easy to solve this problem by decorating interface members, just as we do for this class Therefore, the final configuration for all work should be:

public interface IProduct {
    // I will not discuss here what this attribute does
    // as this is documented already in the github source.
    // Just take note that this is needed,// both here and in the implementing class.
    [Key]
    int Id {get;set;}

    string Name {get;set;}

    [Write(false)]
    ProductCategory Category {get;set;}

    int Category {get;set;}
}

// and implement it on my Product class
public class Product : IProduct {
    [Key]        
    public int Id {get;set;}

    public string Name {get;set;}

    [Write(false)]
    public ProductCategory Category {get;set;}

    int Category {get;set;}
}

If you prefer to use contrib with change tracking, you can use this method If you want to work with rainbow and have problems like navigation properties, you can play with my pull request It works the same way as writeattribute and can only be used with rainbow

If you are not a fan of decorating the course with attributes, the extension project is not suitable for you I know there is another extension project that allows you to do some smooth configuration, but this is not related to the dapper Library in GitHub (not included) My preference is to work with the core library, which leads me to investigate the whole library to see if it already exists or can be improved to meet my needs This is what I do and explain here, for rainbow and contrib

I hope this contribution, the very simple classes I added, the configuration tips I displayed, and the scenarios that guide me will help people use dapper in the future, and there will be similar settings In addition, this answer will teach developers more about what dapper can and can't do This great tool is called dapper deserves a better wiki. I hope this answer / article here will help even in a small way

**If the content I write here has been written somewhere, I don't find that I've been waiting for an answer in two weeks, then I'll be happy to let anyone contact me It has been two weeks now. 29 people have read my problem and have not suggested any links or solutions, so I think the information I share here is the new dapper*

The above is the ORM collected by programming house for you - how to use dapper Rainbow (or optionally use dapper. Contrib) inserts and updates all contents of objects with navigation properties. I hope this article can help you solve ORM - how to use dapper Application development problems encountered by rainbow (or optionally dapper. Contrib) in inserting and updating objects with navigation properties.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>