This project is read-only.

First using

You can create new project using DreamDriven.NET framework. You can find new project type in "New Project" wizard. Let's try to create your first application under DreamDriven.NET named "QuickStartWebApplication". This application will  manage system users and roles.

Figure 1 Creation the first DomainDriven.NET application

After you pressed “OK” button will be created new Visual Studio solution that contains 6 special projects for each layer: DataObjects, DataAccess, BusinessLogic, Presentation, Contracts, WebApplication.
 

Figure 2 Solution view

At first we should create data objects. For our plans it will be User and Role.

Figure 3 Adding new data object to DataObjects project

User:
public class User : IDataObject
{
  private ISet<Role> _roles = new HashedSet<Role>();

  public virtual int Id { get; set; }
  public virtual string Login { get; set; }
  public virtual string Password { get; set; }
  
  public virtual ISet<Role> Roles
  {
    get { return _roles; }
    set { _roles = value; }
  }
}

Role:

public class Role : IDataObject
{
  private ISet<User> _users = new HashedSet<User>();

  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  public virtual string Description { get; set; }
  
  public virtual ISet<User> Users
  {
    get { return _users; }
    set { _users = value; }
  }
}

Also we should update Nhibernate mapping file that present in DataObjects project. 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
          assembly="DataObjects"
          namespace="DataObjects">
 <class name="User" table="Accounts.User">
  <id name="Id">
   <generator class="native"/>
  </id>
  <property name="Login"/>
  <property name="Password"/>
  <set name="Roles" table="Accounts.UserRole">
   <key column="UserId"/>
   <many-to-many class="Role" column="RoleId"/>
  </set>
 </class>
 
 <class name="Role" table="Accounts.Role">
  <id name="Id">
   <generator class="native"/>
  </id>
  <property name="Name"/>
  <property name="Description"/>
  <set name="Users" table="Accounts.UserRole">
   <key column="Id" />
   <many-to-many class="User" column="UserId"/>
  </set>
 </class>
</hibernate-mapping>

Also you must create database and tables for objects described here. Now we should create Data Access objects which will provide object relational mapping for Data Objects through NHibernate. You should create new Repository classes. It is same as on Figure 3 process but you should add DreamDriven.Net Repository class to the DataAccess project. Please take attention - we have used User and Role classes in Repositories classes declaration.

 
UserRepository:

public class UserRepository : DomainRepository<User>
{    
  public UserRepository(IDomainModel domainModel)
    : base(domainModel)
  {
  }
}

RoleRepository:

public class RoleRepository : DomainRepository<Role>
{    
  public RoleRepository(IDomainModel domainModel)
    : base(domainModel)
  {
  }
}

Each of Repository object must be only in single instance for that it should be registred in the Domain Model. Let's create the Domain Model. It is same as on Figure 3 process but you should add DreamDriven.NET DomainModel class to the BusinessLogic project. DomainRepository class provides basic CRUD (Create, Read, Update, Delete) operations for specified data object. In addition it provides Search method for quick text search using Lucene.Net.

AccountsDomainModel:

public class AccountsDomainModel : AbstractDomainModel, IAccountsService
{
  public UserRepository UserRepository
  {
    get { return (UserRepository) GetRepository<User>(); }
  }

  public RoleRepository RoleRepository
  {
    get { return (RoleRepository) GetRepository<Role>(); }
  }

  private AccountsDomainModel(string serviceName)
    : base(serviceName)
  {
    RegisterRepository(new UserRepository(this));
    RegisterRepository(new RoleRepository(this));
  }
}

We've created AccountsDomainModel. We've added two properties for providing public access to these repositories. Please take attention - for getting registred Repository we use type of data object. Let's construct simple UI for data manipulation. DreamDriven.NET provides simple way to implement MVP pattern (Model-View-Presenter). At first we create View for User entities editing.

IUserView:

public interface IUserView : IView
{
  int? Id { get; }
  string Login { get; set; }
  string Password { get; set; }

  event EventHandler Create;
  event EventHandler Update;
}

UserPresenter: 

public class UserPresenter : Presenter<IUserView>
{
  private AccountsDomainModel DomainModel
  {
    get { return GetDomainModel<AccountsDomainModel>(); }
  }

  public UserPresenter(IUserView view)
    : base(view)
  {
    View.Load += OnViewLoad;
    View.Create += OnViewCreate;
    View.Update += OnViewUpdate;
  }

  private void OnViewUpdate(object sender, EventArgs e)
  {
    if (View.Id.HasValue)
    {
      User user = DomainModel.UserRepository.GetById(View.Id.Value);
      if (user != null)
      {
        user.Login = View.Login;
        user.Password = View.Password;
      }
      DomainModel.UserRepository.Update(user);
    }
  }

  private void OnViewCreate(object sender, EventArgs e)
  {
    var user = new User
            {
              Login = View.Login,
              Password = View.Password
            };
    DomainModel.UserRepository.Create(user);
  }

  private void OnViewLoad(object sender, EventArgs e)
  {
    if (View.Id.HasValue)
    {
      User user = DomainModel.UserRepository.GetById(View.Id.Value);
      if (user != null)
      {
        View.Login = user.Login;
        View.Password = user.Password;
      }
    }
  }
}

Please take attention - UserPresenter uses base Repository methods such as Create and Update. Also we've just added method GetById to UserRepository.

public User GetById(int id)
{
  using (ITransaction transaction = Session.BeginTransaction())
  {
    User user = Collection.Where(u => u.Id == id).FirstOrDefault();
    transaction.Commit();
    return user;
  }
}

Now we will implement our View in ASP.NET web page. For activation this opportunity you should use DreamDrivenPage as base class for your page (special template is available). At first you develop web form as usually. 

Web page .aspx file:

<form id="form1" runat="server">
  <table>
    <tr>
      <td>Username:</td>
      <td><asp:TextBox ID="tbUsername" runat="server"/></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><asp:TextBox ID="tbPassword" runat="server"/></td>
    </tr>
  </table>
  <asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="OnCreateClick" />
  <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="OnUpdateClick" />
</form>

Web page code file:

[PresenterBinding(typeof(UserPresenter))]
public partial class UserForm : DreamDrivenPage, IUserView
{
  public int? Id
  {
    get
    {
      string id = Request.QueryString["userid"];
      int idValue;
      if (int.TryParse(id, out idValue))
      {
        return idValue;
      }
      return null;
    }
  }

  public string Login
  {
    get { return tbUsername.Text; }
    set { tbUsername.Text = value; }
  }

  public string Password
  {
    get { return tbPassword.Text; }
    set { tbPassword.Text = value; }
  }

  public event EventHandler Create;
  public event EventHandler Update;

  protected void OnCreateClick(object sender, EventArgs e)
  {
    Create(sender, e);
  }

  protected void OnUpdateClick(object sender, EventArgs e)
  {
    Update(sender, e);
  }
}

In the code behind we implement IUserView interface and bind it to UserPresenter. You should configure your application. At first edit nhibernate config (webapplication project contains it), put your own settings.

AccountService.hibernate.cfg.xml - NHibernate configuration file:

 

 

In the web.config you also must change some settings. Change section dreamdriven.nhibernate.service.cofig (you should put here path to nhibernate config file)

<nhibernate>
	<serviceConfigs>
		<add name="AccountsService" file="X:\AccountService.hibernate.cfg.xml />
	</serviceConfigs>
</nhibernate>

Update config section dreamdriven.integration.services:

<integration>
	<services>
	<add name="AccountsService" 
	  type="BusinessLogic.AccountsDomainModel, BusinessLogic, Version=1.0.0.0, Culture=neutral" 
	  contract="Contracts.IAccountsService, Contracts, Version=1.0.0.0, Culture=neutral"/>
	</services>
</integration>
That's all - we have web form for basic user settings editing.

 

<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration>

 <session-factory xmlns="urn:nhibernate-configuration-2.2">
  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
  <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
  <property name="connection.connection_string">
	Server=(local);initial catalog=nhibernate;Integrated Security=SSPI
  </property>
  <property name="proxyfactory.factory_class">
	NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
  </property>
  <mapping resource="DataObjects.Mapping.hbm.xml" assembly="DataObjects" />
 </session-factory>

</hibernate-configuration>

 

Last edited Jan 5, 2011 at 12:06 AM by kalinkin, version 27

Comments

No comments yet.