Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simone Chiaretta 135 posts 542 karma points c-trib
    Mar 07, 2016 @ 20:40
    Simone Chiaretta
    0

    What's best approach for a package that needs its own data?

    Hi, in the package I'm building I'd need to store some custom data.

    Is there a standard approach for this, with APIs from Umbraco, or shall I just treat it as normal custom development and do my own data access anyway I like?

    Thx

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Mar 08, 2016 @ 07:19
    David Brendel
    0

    Hi Simone,

    the umbraco way would be to use the build in petapoco to access the database for storing/querying your data. But if you want to use something like entity framework or nhibernate you can do that also.

    I can say that the petapoco stuff in umbraco is Quote nice and worth a look.

    Regards David

  • Aristotelis Pitaridis 84 posts 402 karma points
    Mar 08, 2016 @ 07:49
    Aristotelis Pitaridis
    101

    PetaPoco is very easy to use and very fast. I will give you a simple example in order to see how to use it. I have not tested the code below but I am sure that it is going to work for you. Inform me if you have any problem.

    First you have to define your model.

    using Umbraco.Core.Persistence;
    using Umbraco.Core.Persistence.DatabaseAnnotations;
    
    namespace DataExample
    {
        [TableName("Customers")]
        [PrimaryKey("CustomerID", autoIncrement = true)]
        [ExplicitColumns]
        public class Customer
        {
            [Column("CustomerID")]
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int CustomerID { get; set; }
    
            [Column("Name")]
            [Length(50)]
            public string Name { get; set; }
        }
    }
    

    After that you have to make sure that the table will be created automatically and this can be done with the following code.

    using Umbraco.Core;
    using Umbraco.Core.Persistence;
    
    namespace DataExample
    {
        public class RegisterEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var ctx = applicationContext.DatabaseContext;
                var db = new DatabaseSchemaHelper(ctx.Database, applicationContext.ProfilingLogger.Logger, ctx.SqlSyntax);
    
                if (!db.TableExist("Customers"))
                {
                    db.CreateTable<Customer>(false);
                }
            }
        }
    }
    

    Finally you can use the following class in order to access your data.

    using System.Collections.Generic;
    using Umbraco.Core.Persistence;
    
    namespace DataExample
    {
        public static class CustomersRepository
        {
            public static IList<Customer> GetAll()
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                return db.Fetch<Customer>("SELECT * FROM Customers");
            }
    
            public static Customer GetByCustomerID(int CustomerID)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                var Data = db.Fetch<Customer>("SELECT * FROM Customers WHERE CustomerID = @0", CustomerID);
                if (Data.Count > 0)
                    return Data[0];
                else
                    return null;
            }
    
            public static void Insert(Customer customer)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                db.Insert(customer);
            }
    
            public static void Update(Customer customer)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                db.Update(customer);
            }
    
            public static void DeleteByID(int CustomerID)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                db.Execute("DELETE FROM Customers WHERE CustomerID = @0", CustomerID);
            }
        }
    }
    
  • Simone Chiaretta 135 posts 542 karma points c-trib
    Mar 08, 2016 @ 08:27
    Simone Chiaretta
    0

    Thank you both.

    I'll definitely use petapoco :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies