Git Product home page Git Product logo

mongodb.utilite's Introduction

Introduction

This is a framework from MongoDb orientaded in EntityFramework Core, but is basic.

Installation

Use the package manager MongoDb.Utilite to install MongoDb.Utilite.

Install-Package MongoDb.Utilite

Step by Step

1.- Create your model

public class Person
{
   //Id is necessary
   public Guid Id { get; set; }
   //This is neccesary for update with model.
   [UniqueIdentifier] 
   public Guid UniqueIdentifier {get;set;}
   public string Name { get; set; }
   public int Age { get; set; }
}

2.- Create your Mapper (Implement IMongoDbConfiguration is this example T is Person)

 public class PersonMap : IMongoDbConfiguration<Person>
 {
     public void Configure(MongoDbTypeConfiguration<Person> builder)
     {
        builder.HasKey(x=>x.Id);

        /*Configure columns*/
        builder.Property(x => x.Age)
          .Order(1)
          .DefaultValue(20);

        builder.Property(x => x.Name)
           .DefaultValue("Anonimo");
     }
 }

3.- Create your Context (Inheriting of MongoDbContext and passing their parameters in the constructor)

 public class PersonDbContext : MongoDbContext
    {
        //DbSet of your MongoCollections *this is neccesary
        public DbSet<Person> Personas { get; set; }
        //Passing their parameters
        public PersonDbContext(string connectionString, string database) : base(connectionString, database)
        {

        }
        public override void OnModalCreating(MongoModelBuilder modelBuilder)
        {
           //ApplyConfigurationFromAssembly to assigns all configures by assembly *this is neccesary
            modelBuilder.ApplyConfigurationFromAssembly(GetType().Assembly);
        }
    }

4.- Inject MongoDbContext in your Startup (ConfigureService)

  service.AddMongoDbContext<PersonDbContext>("mongodb://server", "database");

5.- In the constructor of your repositories inject your Context

  private readonly PersonDbContext _context;
  public PersonRepository(PersonDbContext context)
  {
     _context = context;
  }

  //Create
  public async Task Create(Person person){
      await _context.Person.Add(person);  
  }
  //List
  public async Task<IEnumerable<Person>> GetAll(){
      await _context.Person.ToListAsync();  
  }
  //Get
  public async Task<IEnumerable<Person>> Get(string name){
      await _context.Person.FirstOrDefaultAsync(x=> x.Name == name);  
  }
  //Edit
  public async Task<IEnumerable<Person>> Get(Person person){
      await _context.Person.UpdateAsync(x=> x.Name == name, person);  
  }
  //Delete
 public async Task<IEnumerable<Person>> Delete(string name){
      await _context.Person.DeleteAsync(x=> x.Name == name);  
  }

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

mongodb.utilite's People

Contributors

josepfs1995 avatar

Stargazers

 avatar Carlos Antonio Suarez Quintero avatar  avatar Brian Pérez López avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.