Skip to content

Instantly share code, notes, and snippets.

@JoshClose
Created November 15, 2011 04:58
Show Gist options
  • Save JoshClose/1366196 to your computer and use it in GitHub Desktop.
Save JoshClose/1366196 to your computer and use it in GitHub Desktop.
Setting Up WCF with Multiple Services and Multiple Databases Using NHibernate and Ninject
using System.Collections.Generic;
using System.ServiceModel;
using Wnn.Service.Contract.DataContracts;
namespace Wnn.Service.Contract.ServiceContracts
{
[ServiceContract]
public interface IBlogService
{
[OperationContract]
Post GetPostById( int id );
[OperationContract]
User GetUserById( int id );
[OperationContract]
User GetUserByUserNameAndPassword( string userName, string password );
[OperationContract]
List<Post> GetPosts();
[OperationContract]
List<Post> GetPostsFromAuthor( string userName );
}
}
Post.cs
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Wnn.Service.Contract.DataContracts
{
[DataContract]
public class Post
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Content { get; set; }
[DataMember]
public DateTime Created { get; set; }
[DataMember]
public string Author { get; set; }
[DataMember]
public List<string> Tags { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace Wnn.Business.Objects
{
public class Post
{
public virtual int Id { get; protected set; }
public virtual string Title { get; set; }
public virtual string Content { get; set; }
public virtual DateTime Created { get; set; }
public virtual User Author { get; set; }
public virtual List<Tag> Tags { get; set; }
public virtual void AddTag( Tag tag )
{
tag.Post = this;
Tags.Add( tag );
}
}
}
using FluentNHibernate.Mapping;
using Wnn.Business.Objects;
namespace Wnn.Business.Mappings
{
public class PostMap : ClassMap<Post>
{
public PostMap()
{
SetupMapping();
}
private void SetupMapping()
{
Table( "Posts" );
Id( m => m.Id );
Map( m => m.Content );
Map( m => m.Title );
References( m => m.Author, "UserId" );
HasMany( m => m.Tags )
.Inverse()
.Cascade.AllDeleteOrphan()
.KeyColumns.Add( "PostId" )
.Fetch.Subselect();
}
}
}
using System;
using System.Data;
using System.Linq;
using NHibernate;
using NHibernate.Linq;
namespace Wnn.Business
{
public class SessionManager
{
private readonly ISession session;
public SessionManager( ISession session )
{
if( session == null )
{
throw new ArgumentNullException( "session" );
}
this.session = session;
}
public virtual IQueryable<T> Linq<T>()
{
return session.Linq<T>();
}
public virtual T Get<T>( int id )
{
return session.Get<T>( id );
}
public virtual ISQLQuery CreateSqlQuery( string queryString )
{
return session.CreateSQLQuery( queryString );
}
public virtual void SaveOrUpdate( object obj )
{
session.SaveOrUpdate( obj );
}
public virtual void Delete( object obj )
{
session.Delete( obj );
}
public virtual ITransaction BeginTransaction()
{
return session.BeginTransaction();
}
public virtual IDbConnection Close()
{
return session.Close();
}
public virtual void Dispose()
{
session.Dispose();
}
}
}
namespace Wnn.Business.Objects
{
public class Tag
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual Post Post { get; set; }
}
}
using FluentNHibernate.Mapping;
using Wnn.Business.Objects;
namespace Wnn.Business.Mappings
{
public class TagMap : ClassMap<Tag>
{
public TagMap()
{
SetupMapping();
}
private void SetupMapping()
{
Table( "Tags" );
Id( m => m.Id );
Map( m => m.Name );
References( m => m.Post, "PostId" );
}
}
}
using System.Runtime.Serialization;
namespace Wnn.Service.Contract.DataContracts
{
[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
}
using System;
using System.Security.Cryptography;
using System.Text;
namespace Wnn.Business.Objects
{
public class User
{
private string password;
public virtual int Id { get; protected set; }
public virtual string UserName { get; set; }
public virtual string Email { get; set; }
public virtual string Password
{
get { return password; }
set
{
var md5 = MD5.Create();
var passwordBytes = Encoding.UTF8.GetBytes( value );
md5.ComputeHash( passwordBytes );
password = Convert.ToBase64String( md5.Hash );
}
}
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
}
using FluentNHibernate.Mapping;
using Wnn.Business.Objects;
namespace Wnn.Business.Mappings
{
public class UserMap : ClassMap<User>
{
public UserMap()
{
SetupMapping();
}
private void SetupMapping()
{
Table( "Users" );
Id( m => m.Id );
Map( m => m.Email );
Map( m => m.FirstName );
Map( m => m.LastName );
Map( m => m.Password ).Access.CamelCaseField();
Map( m => m.UserName );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment