Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate a Mongodb operation class in C #

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to encapsulate a Mongodb operation class in C#. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Detailed explanation of MangoDB CSharp Driver driver for C # operating MangoDB

There is an encapsulation of the MongoDb class, which I think is quite good, and then add some Linq encapsulation and database reading methods to it.

We also built three classes based on his encapsulation.

MongoConfig is a database parameter class

The method of reading InI has been added in MongoConfig. Both ConnStr and DbName have been added to read directly from the INI file. If there is no default to write to the INI file,

MongoConnect is an interface class for database connections

The class MongoConnect is basically unchanged, just like the original one.

MongoApi is the operation class.

First of all, a method GetObjectId is added to find their _ id primary key through generics.

Basically all of the original methods have not changed, but I have rewritten the method of generating Linq, so that it can be operated in the way of LINQ, such as:

Original method

Linq method

Using the calling method of Linq, the database is the same as our original test database, as follows:

Query

Query the method of obtaining List. The lambda expression used for the parameter is to get the value of the

Result

insert

Result

Update

Change the new human age just inserted to 28.

Result

Delete

Delete the new and new human data just inserted.

Result

The above is how to encapsulate a Mongodb operation class in C#. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report