Project: Nhibernate.Automapper.With.DataAnnotations
The source code of the packages is available
here.
Using the Nuget package
EyeOpen.Data.Nhibernate entities are automatically discovered and the mapping is inferred using conventions and
DataAnnotations so is easy to swtich from NHibernate to Entity Framework and viceversa, all based on a solid Domain Model example:
void Main()
{
//To prevent errors on next executions of the snippet
//enable Edit|Preferences|Advanced/Always use Fresh Application Domains
const string KnownEmail = "me@domain.com";
const string KnownTitle = "New post";
NhibernateFactory
.DatabaseProvider<BloggerUnitOfWork>()
.DropIfExists();
//The auto discovered model works on the default instance that is SQLExpress,
//and the DB name that is Blogger (UnitOfWork string is dropped)
using (var unitOfWork = NhibernateFactory.Create<BloggerUnitOfWork>())
{
unitOfWork.BlogRepository.Add(BlogFactory.Create(KnownEmail));
unitOfWork.Commit();
}
using (var unitOfWork = NhibernateFactory.Create<BloggerUnitOfWork>())
{
var blog = unitOfWork.BlogRepository.GetByOwner(KnownEmail);
blog.AddPost(KnownTitle, DateTime.Now);
unitOfWork.BlogRepository.Update(blog);
unitOfWork.Commit();
}
using (var unitOfWork = NhibernateFactory.Create<BloggerUnitOfWork>())
{
var blog = unitOfWork.BlogRepository.GetByOwner(KnownEmail);
blog
.Posts.Single()
.Title.Should().Be.EqualTo(KnownTitle);
}
using (var unitOfWork = NhibernateFactory.Create<BloggerUnitOfWork>())
{
unitOfWork.BlogRepository.GetByOwner(KnownEmail).Dump();
}
}