- basicsample.zip
- BasicSample
- BasicSample.Core
- BasicSample.Data
- BasicSample.resharper
- BasicSample.resharper.user
- BasicSample.sln
- BasicSample.Tests
- BasicSample.Web
- Solution Items
- enterprisesample.zip
- EnterpriseSample
- EnterpriseSample.Core
- EnterpriseSample.Data
- EnterpriseSample.Presenters
- EnterpriseSample.resharper
- EnterpriseSample.resharper.user
- EnterpriseSample.sln
- EnterpriseSample.Tests
- EnterpriseSample.Web
- ProjectBase.Data
- ProjectBase.Utils
- Solution Items
- nhibernatebestpractices_src.zip
- NHibernateSample
- NHibernateSample.Core
- NHibernateSample.Data
- NHibernateSample.sln
- NHibernateSample.Tests
- NHibernateSample.Web
- Solution Items
- Iesi.Collections.dll
- log4net.dll
- NHibernate.dll
- NHibernate.Generics.dll
- nunit.framework.dll
|
using System.Reflection;
using BasicSample.Core.Domain;
using BasicSample.Core.Utils;
namespace BasicSample.Tests.Domain
{
/// <summary>
/// I've gone back and forth as to when it is appropriate to use reflection for
/// accessing/setting a private/protected members. It is imperitive that the <see cref="DomainObject{IdT}.ID"/>
/// property is read-only and set only by the ORM. With that said, some unit tests need
/// ID set accordingly; therefore, this utility enables that ability. This class should
/// never be used outside of the unit tests. Instead, implement <see cref="IHasAssignedId{IdT}" /> to
/// expose a public setter.
/// </summary>
public class DomainObjectIdSetter<IdT>
{
/// <summary>
/// Uses reflection to set the ID of a <see cref="DomainObject{IdT}" />.
/// </summary>
public void SetIdOf(DomainObject<IdT> domainObject, IdT id) {
// Set the data property reflectively
PropertyInfo idProperty = domainObject.GetType().GetProperty(NAME_OF_ID_MEMBER,
BindingFlags.Public | BindingFlags.Instance);
Check.Ensure(idProperty != null, "idProperty could not be found");
idProperty.SetValue(domainObject, id, null);
}
private const string NAME_OF_ID_MEMBER = "ID";
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.