使用NHibernate 3.2实现Repository(ORuM)(五)MVCQuick.Framework.Repository

MVCQuick的Repository实现特点:
  1、实体类与仓储类真正实现分离;
  2、可以支持多种数据库;
  3、通过.Net反射技术,每个实体对象自动产生映射文件;
  4、单元测试实现简单。

MVCQuick.Framework.Repository的IRepository接口

void Save<TEntity>(TEntity entity) where TEntity : EntityBase;
void Update<TEntity>(TEntity entity) where TEntity : EntityBase;
void SaveOrUpdate<TEntity>(TEntity entity) where TEntity : EntityBase;
void Delete<TEntity>(TEntity entity) where TEntity : EntityBase;
TEntity Get<TKey, TEntity>(TKey id) where TEntity : EntityBase;
bool Exists<TEntity>();
long Count<TEntity>();
IList<TEntity> FindAll<TEntity>() where TEntity : EntityBase;
TEntity FindOne<TEntity>(string propertyName, object value) where TEntity : EntityBase;
TEntity FindOne<TEntity>(IDictionary<String, Object> properties) where TEntity : EntityBase;
TEntity FindOne<TEntity>(string propertyName1, object value1, string propertyName2, object value2) where TEntity : EntityBase;
IList<TEntity> Find<TEntity>(string propertyName, object value) where TEntity : EntityBase;
IList<TEntity> Find<TEntity>(string propertyName1, object value1, string propertyName2, object value2) where TEntity : EntityBase;
IList<TEntity> Find<TEntity>(IDictionary<String, Object> properties) where TEntity : EntityBase;
ITransaction BeginTransaction();

使用方法
1、添加引用"MVCQuick.Framework.dll"

2、创建实体类

public class Album : EntityBase
{
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}

public class Genre : EntityBase
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IEnumerable<Album> Albums { get; set; }

}

public class Artist : EntityBase
{
public virtual string Name { get; set; }
public virtual Address Address { get; set; }
public virtual IEnumerable<Album> Albums { get; set; }
}

public class User : EntityBase
{
public virtual string Username { get; set; }
public virtual string Email { get; set; }
public virtual string Password { get; set; }
public virtual IEnumerable<Role> Roles { get; set; }
}

public class Role : EntityBase
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IEnumerable<User> Users { get; set; }
}

public class Address : IValueObject
{
public virtual string City { get; set; }
public virtual string Country { get; set; }
public virtual string State { get; set; }
public virtual string Street { get; set; }
public virtual string Zip { get; set; }
}

3、web.config配置数据库

<connectionStrings>   
<clear />
<!--
providerName:DB2,DB2400,Firebird,MsSql2000,MsSql2005,MsSql2008,
MsSqlCe,MySQL,MySQL5,Oracle8i,Oracle9i,Oracle10g,
PostgreSQL,PostgreSQL81,PostgreSQL82,SQLite
-->
<add name="MVCQuickRepository"
connectionString
="Data Source=|DataDirectory|test1.db;Version=3;New=True;"
providerName
="SQLite"/>

</connectionStrings>

4、初始化repository

NHibernateManager.Configuration();
NHibernateManager.AddMapping("MVCQuick",
typeof(EntityBase).Assembly,
typeof(Album).Assembly);
NHibernateManager.UpdateSchema();

5、CRUD

IRepository repository = new NHibernateRepository();
User user1 = new User { Username = "admin", Email = "admin@163.com", Password = "admin123" };
repository.Save(user1);
User user2 = new User { Username = "guest", Email = "guest@163.com", Password = "guest123" };
repository.Save(user2);

User user3 = repository.Get<int, User>(1);

IList<User> users1 = repository.FindAll<User>();
IList<User> users2 = repository.Find<User>("Username", "admin");

User user4 = repository.FindOne<User>("Username", "admin");
User user5 = repository.FindOne<User>("Username", "admin", "Password", "admin123");

User user6 = repository.FindOne<User>("Username", "guest");
user6.Password = "guest1234";
repository.Update(user6);

User user7 = repository.FindOne<User>("Username", "guest", "Password", "guest1234");
repository.Delete(user7);


源代码下载:http://mvcquick.codeplex.com/ 

posted @ 2011-10-15 16:04  GuYoung  阅读(1637)  评论(0编辑  收藏  举报