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?
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.
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);
}
}
}
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
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
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.
After that you have to make sure that the table will be created automatically and this can be done with the following code.
Finally you can use the following class in order to access your data.
Thank you both.
I'll definitely use petapoco :)
is working on a reply...