Created
November 15, 2011 04:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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