wpf - What is the proper way to set up a domain class MongoDB/C# -


I am currently creating a WPF application and am now ready to add a mango database, although I do not know I found this to be the best process to do with this:

MongoManagment.cs:

  Namesplace MoneyManagment {class MongoManagment {Public Zero Connect () {String connection string = "mongodb: // local host"; MongoClient Client = new Mongo Client (connectionString); MongoServer Server = client.GetServer (); MongoDatabase database = server.GetDatabase ("MoneyManagment"); // Connect to Database MongoCollection & lt; Users & gt; _users = database.GetCollection & lt; Users & gt; ("the user"); // "user" is the collection name, "user" is the domain class}} public class user {public object id _id {get; Set; } Public String Username {get; Set; }}}  

Then I try to access the domain class data in menundo.xcls:

  Namesplace MoneyManagment {Public partial square MainWindow: Window {public manwindows () {initialization (); Foreach (user user in _users.FindAll) () do something with the user here}}}  

And I get an error here that VS _users , which Somewhat understandable, the problem is that, I have tried many different ways to make this accessible and it just is not working, which makes me believe that to know about it There is a better way. Any ideas?

First of all, you need a variable that you want to access to appear publicly Are there.

  Public Square MongoManagment {Public MongoCollection & lt; Users & gt; Mongoowers {receive; Set;} Public Zero Connect () {.... this.MongoUsers = database.GetCollection & lt; Users & gt; ("the user"); }  

After that, you need to reference the object in the external class:

  public manwindows () {.... MongoManagement mm = new MongoManagement (); Mm.Connect ();  

Note This is a very simple example, for example, if you do not do anything with user data in Foreach (user user mm.MongoUsers.FindAll ()) And you do not want a copy of the Mongo management for every window, etc. Still, you want to spend some time exploring the concepts of variable lifestyles ....


Comments

Popular posts from this blog

Folder list php -

objective c - How to open front/back camera at the same time in iOS developing? -

GO: Serve static pages -