Skip to content

Instantly share code, notes, and snippets.

@maritaria
Created March 3, 2016 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maritaria/67a4c7ebede4e79857fe to your computer and use it in GitHub Desktop.
Save maritaria/67a4c7ebede4e79857fe to your computer and use it in GitHub Desktop.
Example of EntityFramework
using System;
/*
THIS FILE WORKS BEST WHEN READ FROM TOP TO BOTTOM
WARNING, ENTITY FRAMEWOKR IS REALLY COOL BUT IM NOT AN EXPERT.
There are 3 ways of declaring stuff in entity framework.
But the one I used mainly is code-first, which is where you
make your code and the database is generated in order to support the model the code presents.
https://msdn.microsoft.com/en-us/data/jj193542.aspx
*/
/*
First, off we need a class that represents the data of a single user.
And make a class out of it
*/
public class SkyhookViewer
{
//Based on HotBot.Plugins.Wallet.WalletValue
//System.ComponentModel.DataAnnotations.KeyAttribute
//Indicates this field is the primary key
//from what I've seen this must be a Guid or an int type.
[Key]
public Guid Id { get; set; }
//System.ComponentModel.DataAnnotations.Schema
//Also, we want to make sure that all usernames are unique
//so this will prevent double entries for any single viewer.
[Index(IsUnique = true)]
[StringLength(Verify.MaximumUsernameLength, MinimumLength = Verify.MinimumUsernameLength)]
public string Username { get; set; }
//Quick thing to store money as a simple example.
//C# 6.0 notation, same effect when setting this in the PARAMETERLESS (a.k.a. default) constructor.
//Entity framework will set this value after the constructor is run if it is created from a database constext
public double Money { get; set; } = 0;
}
/*
Now to create the connection with the database, simply create a class that is derived from DbContext
*/
public class SkyhookContext : DbContext
{
//Based on HotBot.Plugins.Wallet.WalletContext
//The DbSet will automatically create a list-like structure that you can store instances on and query.
public DbSet<SkyhookViewer> Viewers { get; set; }
//Now here's the cool part, when you use LINQ on the DbSet.
//it translates it into SQL and executes it directly on the database!!!!!
//No more manual querries, the entity framework allows you to use adapters
//(based on app.conf) and the adapter is responsible for creating the query.
//There already are adapters for the most common database engines.
//Check my hotbot project to see how I used configs to setup for local db storage.
public SkyhookContext()
{
}
}
//Personally I like to split off the logic from the context because of the using-context pattern (see below)
public class UserManager
{
//Based on HotBot.Plugins.Wallet.WalletPlugin
public double GetMoney(string username)
{
using (var context = new SkyhookContext())
{
//The using statement guarantees that context.Dispose() is called whenever the code leaves the using block
var viewer = context.Viewers.FirstOrDefault(v => v.Username == username);
if (viewer != null)
{
return viewer.Money;
}
else
{
return 0d;
}
}
}
public void SetMoney(string username, double value)
{
using (var context = new SkyhookContext())
{
SkyhookViewer viewer = context.Viewers.FirstOrDefault(v => v.Username == username);
if (viewer == null)
{
viewer = new SkyhookViewer();//Just create a instance and add it to the context's list
context.Viewers.Add(viewer);
viewer.Username = username;
viewer.Money = value;
}
viewer.Money = value;
context.SaveChanges();//Executes the update/insert query.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment