Posts

Showing posts with the label NHibernate

Using NHibernate in Asp.Net Core

Starting with version 5.1, NHibernate now supports .Net Core and .Netstandard as well as the full framework. Let’s take a quick look at how to set it up in an ASP.Net Core 2.x/3.x application. Configuration As of this writing, NHibernate doesn’t support the configuration system within Asp.Net Core based on Microsoft.Extensions.Configuration. The good news is, you don’t have to use a  hibernate.cfg.xml  file or the   section in app.config. You can add the properies manually, and pull the values from Asp.Net Core’s configuration, taking advantage of all the goodies that come with it. This opens up a world of configuration deployment possibilities. var config = new Configuration (); IDictionary < string , string > properties = new Dictionary < string , string > { { "connection.connection_string" , Configuration . GetConnectionString ( "Default" ) }, { "dialect" , "NHibernate.Dialect.SQLiteDialect" }, ...

NHibernate Releases 5.1 with support for .Net Core and .NetStandard 2.0

The  NHibernate  Team has done it!  Release 5.1  of the famous ORM for .Net supports both the full .Net Framework and .Net Core. Great work! The library is actually cross compiled for net461, netCoreApp2.0 and netStandard2.0 to account for some of the subtle nuances between net461 and core and to support older tooling that doesn’t know how to work with netStandard libraries. Caveats As noted in the release notes, there are a few caveats when running on Core: Binary serialization is not supported - the user shall implement serialization surrogates for System.Type, FieldInfo, PropertyInfo, MethodInfo, ConstructorInfo, Delegate, etc. SqlClient, Odbc, Oledb drivers are converted to ReflectionBasedDriver to avoid the extra dependencies. CallSessionContext uses a static AsyncLocal field to mimic the CallContext behavior. System transactions (transaction scopes) are untested, due to the lack of data providers supporting them. WebSessionContext and ASP.Net Cor...

Building Microservices with Asp.Net WebAPI, Owin, Ninject, NHibernate, and Azure -- Part 2: Error Responses and Exception

Last time  we looked at setting up a microservice based on WebAPI with dependency injection an persistance. In this article, we’ll look at global exception handling and error responses. The Error Response We return standard error message formats for 500 errors. In WebAPI, Microsoft uses  HttpError  internally within the framework. For consistency, we do the same. That way, whether the error was generated in the pipeline, or by our business logic, the message is the same. Under the hood, HttpError inherits  Dictionary . You can add custom key/value pairs for anything you’d like to return. The response the looks something like… { "message" : "An error has occurred" , "exceptionMessage" : "Object not set to an instance of an object." } The dictionary keys to look up standard error information are available on the  HttpErrorKeys  type. Using it, we can generate a  IHttpActionResult  to return… public class Except...

Building Microservices with Asp.Net WebAPI, Owin, Ninject, NHibernate, and Azure

Image
We’ve been building microservices here for a while, so I thought I’d share how we do it. I’ll try to cover the basics of how we implement all our services without getting too complex. I’ll provide a high-level overview of the basic components that lay the foundation for our services and share some code snip-its. The goal is to provide you with a simple foundation to build your own microservices framework strategy upon. What’s a Microservice? There are a lot of opinions on the matter. When I use the term microservice in this article, I’m referring to a service that is independently deployable and encapsulates a single bounded context. Though the techniques in this article could be applied to any service, even large monoliths. Core Technology Stack Even though the benefit of microservices is “use what every you want”, we found that having a standard set of tools in the tool box keep our lives simple and allow us to produce services more quickly. If your shop does give developers...