nhibernate.postgresql.json
1.1.2
dotnet add package nhibernate.postgresql.json --version 1.1.2
NuGet\Install-Package nhibernate.postgresql.json -Version 1.1.2
<PackageReference Include="nhibernate.postgresql.json" Version="1.1.2" />
paket add nhibernate.postgresql.json --version 1.1.2
#r "nuget: nhibernate.postgresql.json, 1.1.2"
// Install nhibernate.postgresql.json as a Cake Addin #addin nuget:?package=nhibernate.postgresql.json&version=1.1.2 // Install nhibernate.postgresql.json as a Cake Tool #tool nuget:?package=nhibernate.postgresql.json&version=1.1.2
nhibernate.postgresql.json
NHibernate custom type to handle PostgreSQL JSON and JSONB column types.
As of June/2019 this repository is being actively maintained.
Installation
This library depends on the following NuGet packages:
- Direct
- Indirect
These will be installed as dependencies along with this page.
To install this download package nhibernate.postgresql.json
or run:
Install-Package nhibernate.postgresql.json
Compatibility
This library has been tested with .NET Core 2.1 and NHibernate 5.x.
Configuration
All this package does is offer an NHibernate custom type that you can use to handle json/jsonb PostgreSql data types. It relies on the configuration provided by NpgSql.Json.Net.
The way it works is by telling the extension which .NET types you want it to handle for you. This is done in the Startup
class:
NpgsqlConnection.GlobalTypeMapper.UseJsonNet(new[] { typeof(AnotherType) });
It is important to note that you must specify each of the types that you want the library to handle for you and not their parents.
Usage
After the configuration is complete, you can just use it in your mapping as a custom type:
Map(x => x.PropertyWithMyType).CustomSqlType("jsonb").CustomType<JsonType<AnotherType>>().Column("db_column");
It also works with native CLR types such as Dictionary<string,string>
which is perfect to store unstructured data. In my particular case, I use it to store raw data from web requests along with parsed data.
Map(x => x.Raw).CustomSqlType("jsonb").CustomType<JsonType<Dictionary<string,string>>>().Column("raw");
Sample
A more complete sample can be found below:
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
NpgsqlConnection.GlobalTypeMapper.UseJsonNet(new[] { typeof(MyType) });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseMvc();
}
MyEntity.cs
public class AnotherType
{
public AnotherType() {}
public virtual string AnotherProperty {get;set;}
}
public class MyType
{
public MyType() {}
public virtual string Name { get; set; }
public virtual int Age { get; set; }
public virtual AnotherType Other { get; set; }
}
public class MyTypeMap : ClassMap<AnotherType>
{
public MyType()
{
Table("my_table");
Map(x => x.Name).Column("name");
Map(x => x.Age).Column("age");
Map(x => x.Other).CustomSqlType("jsonb").CustomType<JsonType<Transaction>>().Column("other").Not.Nullable();
}
}
How to contribute / I want to help
If you are interested in helping out there are a few things we could use:
- Add tests to the project to make sure that this behaves as it should (so far I have been using it in a production app, but my use case is very simple and not comprehensive)
- Add more examples
If you feel like helping, just send a PR 😃
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 is compatible. netcoreapp2.2 is compatible. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.0
- NHibernate (>= 5.2.1)
- Npgsql.Json.NET (>= 4.0.4)
-
.NETCoreApp 2.1
- NHibernate (>= 5.2.1)
- Npgsql.Json.NET (>= 4.0.4)
-
.NETCoreApp 2.2
- NHibernate (>= 5.2.1)
- Npgsql.Json.NET (>= 4.0.4)
-
.NETStandard 2.0
- NHibernate (>= 5.2.1)
- Npgsql.Json.NET (>= 4.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.