NHibernate and State Pattern persistance - a good implementation? - c#

Below is my implementation of the state pattern. In order to persist the State object to my database with NHibernate, I am assigning each state class an enum value. This is stored as a private field on the entity, and mapped to a integer field in my database table.
I want to know whether this is a good implementation as I will be using the state pattern throughout my application and want to get it right the first time. Thanks
public class Order
{
private OrderStatusEnum _statusId;
public virtual Guid Id { get; set; }
private OrderState _status;
public virtual OrderState Status {
get
{
if (_status == null)
_status = GetState(_statusId);
return _status;
}
set
{
_status = value;
_statusId = _status.Id;
}
}
private OrderState GetState(OrderStatusEnum status)
{
switch (_statusId) {
case OrderStatusEnum.Pending:
return new Submitted(this);
case OrderStatusEnum.Completed:
return new Completed(this);
default:
return new NewOrder(this);
}
}
}
public abstract class OrderState
{
private readonly Order _order;
public OrderState(Order order) {
_order = order;
}
internal Order Order { get { return _order; } }
public abstract OrderStatusEnum Id { get; }
public virtual void Submit() {
throw new InvalidOperationException(
string.Format("Can't Submit a {0} Order", this.GetType().Name)
);
}
public virtual void Complete() {
throw new InvalidOperationException(
string.Format(string.Format("Can't Cancel a {0} Order", this.GetType().Name))
);
}
protected internal void _Submit() {
Order.Status = new Submitted(Order);
}
protected internal void _Complete() {
Order.Status = new Completed(Order);
}
}
public class NewOrder : OrderState
{
public NewOrder(Order order) : base(order) { }
public override OrderStatusEnum Id {
get { return OrderStatusEnum.New; }
}
public override void Submit() {
_Submit();
}
}
public class Submitted : OrderState
{
public Submitted(Order order) : base(order) { }
public override OrderStatusEnum Id {
get { return OrderStatusEnum.Pending; }
}
public override void Complete() {
_Complete();
}
}
public class Completed : OrderState
{
public Completed(Order order) : base(order) { }
public override OrderStatusEnum Id {
get { return OrderStatusEnum.Completed; }
}
}
public enum OrderStatusEnum {
New = 1,
Pending = 2,
Completed = 3
}

Not sure whether to answer or add a comment, but your approach worked very well for me in a similar situation.
I also experimented with the approach described here using the Tarantino framework, but I found it easier to extend from your code.

Related

is this implementation of the Singleton and Object null Patterns Thread Safe?

Im trying to write a simple code to implement the Singleton and object null patterns.
the code should check if the new customer has a name, if yes put it in the real customer, and if not in the fakecustomer.
My focus in this question is: Is the Singleton pattern making my code thread safe in this case?
interface Icustomer
{
string Name { get; }
bool IsNull { get; }
}
class realcustomer : Icustomer
{
public string Name { get; set; }
public bool IsNull { get { return false; } }
public realcustomer(string name)
{
Name = name;
}
}
class fakecustomer : Icustomer
{
public string Name { get { return "customer not available"; } }
public bool IsNull { get { return true; } }
}
class checkifnull
{
public static Icustomer Getcustomer(string name)
{
if (string.IsNullOrEmpty(name))
{
return new fakecustomer();
}
else
{
return new realcustomer(name);
}
}
}
class Singleton
{
private int total = 0;
private static Icustomer cust;
private Singleton() { }
public static Icustomer makecust(string name)
{
if (cust == null)
{
if (string.IsNullOrEmpty(name))
{
cust = new fakecustomer();
}
else
{
cust = new realcustomer(name);
}
}
return cust;
}
public void add()
{
total++;
}
public int getTotal()
{
return total;
}
}
internal class Program
{
static void Main(string[] args)
{
Icustomer new_cust = Singleton.makecust("name");
}
}
each pattern works when implemented on its own, but now i'm trying to use both at the same time.

Set tableAttribute mapping dynamically in linq to sql

I was wondering if it is possible to dynamically set the table mapping for the linq classes:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Comfort 0601$Contact]")]
public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged
{
...
The reason I want to change this dynamically or programmatically is because the database I'm using is created by Navision. There the tables have a prefix of the company name to which these tables belong to. Therefor I would like to be able to change that prefix.
[global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.["+SPResources.Database.Company+"$Contact]")]
whit Company as:
public const string Company = "Comfort 0601";
I've tried this, but it only work if i declared Company as a constant.
The table structure between companies are identical. just the name & content change.
I hope someone can give some advice about this. I'm also not sure if it even is possible.
Think the correct solution for adding table-prefix is to use SQL Server 2005 (Or newer), and then create a db-user for each required prefix, and change default schema for each db-user from "dbo" to something unique.
Another solution is to extend the existing DataContext with the ability to "correct" the MappingSource at runtime:
[Table] // Look no name
public class DbRecord
{
[Column(IsPrimaryKey=true)]
public long Id { get; set; }
}
CustomDataContext.UpdateCustomTable(typeof(DbRecord), "DbTable");
using (CustomDataContext dc = new CustomDataContext(dbConnection))
{
Table<DbRecord> dbTable = dc.GetTable<DbRecord>();
var query = from item in dbTable;
}
This is the custom DataContext (I guess one could easily change it to add a prefix to an existing table-name) (Replace Dictionary with ConcurrentDictionary if in multi-threaded environment):
public class CustomDataContext : DataContext
{
static CustomMappingSource _sharedMappingSource = new CustomMappingSource();
public static void UpdateCustomTable(Type rowType, string tableName)
{
_sharedMappingSource.UpdateCustomTable(rowType, tableName);
}
public CustomDataContext(System.Data.IDbConnection connection) : base(connection, _sharedMappingSource) { }
public CustomDataContext(string fileOrServerOrConnection) : base(fileOrServerOrConnection, _sharedMappingSource) { }
}
internal class CustomMappingSource : MappingSource
{
AttributeMappingSource mapping = new AttributeMappingSource();
Dictionary<Type, string> _customTableNames = new Dictionary<Type, string>();
public void UpdateCustomTable(Type rowType, string tableName)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("TableName");
_customTableNames[rowType] = tableName;
}
protected override MetaModel CreateModel(Type dataContextType)
{
MetaModel oldmodel = mapping.GetModel(dataContextType);
CustomMetaModel newmodel = new CustomMetaModel(oldmodel, _customTableNames);
return newmodel;
}
}
internal class CustomMetaModel : MetaModel
{
MetaModel _orgmodel;
Dictionary<Type, MetaTable> _customtables = new Dictionary<Type, MetaTable>();
Dictionary<Type, string> _tableNames = new Dictionary<Type, string>();
public CustomMetaModel(MetaModel orgmodel, Dictionary<Type, string> tableNames)
{
_orgmodel = orgmodel;
_tableNames = tableNames;
}
public override MetaType GetMetaType(Type type)
{
MetaTable metaTable;
if (_customtables.TryGetValue(type, out metaTable))
return metaTable.RowType;
else
return _orgmodel.GetMetaType(type);
}
public override MetaTable GetTable(Type rowType)
{
MetaTable customMetaTable;
if (_customtables.TryGetValue(rowType, out customMetaTable))
return customMetaTable;
if (_tableNames.ContainsKey(rowType))
{
MetaTable orgtable = _orgmodel.GetTable(rowType);
MetaType orgrowtype = orgtable.RowType;
CustomMetaType newRowType = new CustomMetaType(orgrowtype, this);
_customtables.Add(rowType, new CustomMetaTable(orgtable, this, newRowType, _tableNames[rowType]));
newRowType.MetaTable = _customtables[rowType];
return newRowType.MetaTable;
}
return _orgmodel.GetTable(rowType);
}
#region MetaModel Forwards
public override Type ContextType { get { return _orgmodel.ContextType; } }
public override string DatabaseName { get { return _orgmodel.DatabaseName; } }
public override MappingSource MappingSource { get { return _orgmodel.MappingSource; } }
public override Type ProviderType { get { return _orgmodel.ProviderType; } }
public override MetaFunction GetFunction(System.Reflection.MethodInfo method) { return _orgmodel.GetFunction(method); }
public override IEnumerable<MetaFunction> GetFunctions() { return _orgmodel.GetFunctions(); }
public override IEnumerable<MetaTable> GetTables() { return _orgmodel.GetTables(); }
#endregion
}
internal class CustomMetaTable : MetaTable
{
MetaTable _orgtable;
MetaModel _metamodel;
MetaType _rowtype;
string _tableName;
public CustomMetaTable(MetaTable orgtable, MetaModel metamodel, MetaType rowtype, string tableName)
{
_orgtable = orgtable;
_metamodel = metamodel;
_rowtype = rowtype;
_tableName = tableName;
}
public override MetaModel Model { get { return _metamodel; } }
public override MetaType RowType { get { return _rowtype; } }
public override string TableName { get { return _tableName; } }
#region MetaTable Forwards
public override System.Reflection.MethodInfo DeleteMethod { get { return _orgtable.DeleteMethod; } }
public override System.Reflection.MethodInfo InsertMethod { get { return _orgtable.InsertMethod; } }
public override System.Reflection.MethodInfo UpdateMethod { get { return _orgtable.UpdateMethod; } }
#endregion
}
internal class CustomMetaType : MetaType
{
MetaType _orgtype;
MetaModel _metamodel;
public MetaTable MetaTable { get; set; }
public CustomMetaType(MetaType orgtype, MetaModel metamodel)
{
_orgtype = orgtype;
_metamodel = metamodel;
}
public override MetaTable Table { get { return MetaTable; } }
public override MetaModel Model { get { return _metamodel; } }
#region MetaType Forwards
public override System.Collections.ObjectModel.ReadOnlyCollection<MetaAssociation> Associations { get { return _orgtype.Associations; } }
public override bool CanInstantiate { get { return _orgtype.CanInstantiate; } }
public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> DataMembers { get { return _orgtype.DataMembers; } }
public override MetaDataMember DBGeneratedIdentityMember { get { return _orgtype.DBGeneratedIdentityMember; } }
public override System.Collections.ObjectModel.ReadOnlyCollection<MetaType> DerivedTypes { get { return _orgtype.DerivedTypes; } }
public override MetaDataMember Discriminator { get { return _orgtype.Discriminator; } }
public override bool HasAnyLoadMethod { get { return _orgtype.HasAnyLoadMethod; } }
public override bool HasAnyValidateMethod { get { return _orgtype.HasAnyValidateMethod; } }
public override bool HasInheritance { get { return _orgtype.HasInheritance; } }
public override bool HasInheritanceCode { get { return _orgtype.HasInheritanceCode; } }
public override bool HasUpdateCheck { get { return _orgtype.HasUpdateCheck; } }
public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> IdentityMembers { get { return _orgtype.IdentityMembers; } }
public override MetaType InheritanceBase { get { return _orgtype.InheritanceBase; } }
public override object InheritanceCode { get { return _orgtype.InheritanceCode; } }
public override MetaType InheritanceDefault { get { return _orgtype.InheritanceDefault; } }
public override MetaType InheritanceRoot { get { return _orgtype.InheritanceRoot; } }
public override System.Collections.ObjectModel.ReadOnlyCollection<MetaType> InheritanceTypes { get { return _orgtype.InheritanceTypes; } }
public override bool IsEntity { get { return _orgtype.IsEntity; } }
public override bool IsInheritanceDefault { get { return _orgtype.IsInheritanceDefault; } }
public override string Name { get { return _orgtype.Name; } }
public override System.Reflection.MethodInfo OnLoadedMethod { get { return _orgtype.OnLoadedMethod; } }
public override System.Reflection.MethodInfo OnValidateMethod { get { return _orgtype.OnValidateMethod; } }
public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> PersistentDataMembers { get { return _orgtype.PersistentDataMembers; } }
public override Type Type { get { return _orgtype.Type; } }
public override MetaDataMember VersionMember { get { return _orgtype.VersionMember; } }
public override MetaDataMember GetDataMember(System.Reflection.MemberInfo member) { return _orgtype.GetDataMember(member); }
public override MetaType GetInheritanceType(Type type) { return _orgtype.GetInheritanceType(type); }
public override MetaType GetTypeForInheritanceCode(object code) { return _orgtype.GetTypeForInheritanceCode(code); }
#endregion
}
The answers of Brian and Gert are possible solutions for this problem. But changing the mapping at run-time is just not possible (as Brian mentioned).
As our project progressed we decided that if we would need another company name (which would mean that that build is for a different customer) we would just rebuild the project. That decision made the following solution possible:
We commented out the original mappings generated by Visual Studio:
//[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Comfort 0601$Contact]")]
public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged
{
...
Because we have our own partial class for every Linq class, we added the modified mapping at the top of our own class:
[global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.["+SPResources.Database.Company+"$Contact]")]
public partial class Contact
{
where our company will be:
public const string Company = "Comfort 0601";
If a rebuild is needed for a different company we simply change the Company variable.
Note: This only works properly if you don't change your *.dbml file in VS. If you do, VS will automatically undo your changes.
Edit: As time has passed and I started to look more into the Entity Framework I found a more specific and suitable solution for this problem: http://www.codeproject.com/Articles/421643/How-to-Use-MVC-Net-on-the-Dynamics-NAV-Database-St
You could use SQLMetal to custom generate the code for each entities. You cannot apply attributes dynamically at runtime, so this would be the best option.
If you'd work code-first you could use fluent mapping you can use the ToTable() method to configure the database table at runtime (see under Changing the Database Table Name). E.g. in a OnModelCreating() override of your DbContext derivative:
string company = "Comfort 0601";
modelBuilder.Entity<Contact>().ToTable(company + "Contact");
modelBuilder.Entity<....
modelBuilder.Entity<....
You could take the company name from a config file.

How to prevent property setter from modifying private property data

Let me explain my question by posing a hypothetical situation. Lets start with a class:
public class PaymentDetails
{
public int Id {get;set;}
public string Status {get;set;}
}
And then I have another class:
public class PaymentHelper
{
private PaymentDetails _paymentDetails;
public PaymentDetails MyPaymentDetails{ get { return _paymentDetails; } }
public PaymentHelper()
{
_paymentDetails = new PaymentDetails();
}
public void ModifyPaymentDetails(string someString)
{
// code to take the arguments and modify this._paymentDetails
}
}
OK, so I have these two classes. PaymentHelper has made the property MyPaymentDetails read-only.
So I cannot instantiate PaymentHelper and modify MyPaymentDetails like this:
PaymentHelper ph = new PaymentHelper();
ph.MyPaymentDetails = new PaymentDetails(); // Not allowed!!!
But I can modify the public properties inside of ph.MyPaymentDetails like this:
ph.MyPaymentDetails.Status = "Some status"; // This is allowed
How do I prevent that from working? Or is there no good way of doing that?
A property may apply access modifiers to individual accessors, for instance:
public string Status { get; private set; }
The scope of access is left to your circumstance. Keeping it private, I'm sure you can tell, will mean only elements within the scope of the current class can use the setter, protected would allow inheritors to use it, etc.
Obviously your classes need to be engineered properly from the bottom up, so as to account for appropriate scoping and robust management when used further up the hierarchy.
The idea of protecting the properties of a complex type that is itself a property isn't available from a language construct at that level.
One option is to design the contained type in such a way as to make its properties read-only using the access modifiers (public set, protected set, private set, etc).
My preference is to expose it as an interface to public consumers:
public class PaymentHelper
{
private PaymentDetails _paymentDetails;
public IPaymentDetails MyPaymentDetails{ get { return _paymentDetails; } }
public PaymentHelper()
{
_paymentDetails = new PaymentDetails();
}
public void ModifyPaymentDetails(string someString)
{
// code to take the arguments and modify this._paymentDetails
}
}
interface IPaymentDetails
{
int Status { get; }
}
Code inside the PaymentHelper class can then use the PaymentDetails class directly, and code outside the class won't be able to use PaymentDetails unless they cast directly to it, which you can stop if you don't release the PaymentDetails class and only provide the interface.
Of course, you can never really stop the determined person who may use reflection to set things. I tend to let these people break the code :-)
Another solution is not to expose the PaymentDetails object directly, but rather wrap the properties you wish to expose. For example:
public class PaymentHelper
{
private PaymentDetails _paymentDetails;
public string PaymentDetailsStatus { get { return _paymentDetails.Status; } }
public PaymentHelper()
{
_paymentDetails = new PaymentDetails();
}
public void ModifyPaymentDetails(string someString)
{
// code to take the arguments and modify this._paymentDetails
}
}
Edit: You could always let the behavior of value types take care of this for you. Change PaymentDetails to a struct instead of a class:
public struct PaymentDetails
{
public int Id { get; set; }
public string Status { get; set; }
}
public class PaymentHelper
{
public PaymentDetails Details { get; set; }
}
If you then try to
ph.Details.Status = "Some status"; //
You'll get a compiler error telling you that you can't do this. Since value types are returned, well, by value, you can't modify the .Status property.
Or...
If PaymentDetails and PaymentHelper are declared in the same class library (separate from the code you want to prevent from writing to the .MyPaymentDetails property, you could use:
public class PaymentDetails
{
public int Id { get; internal set; }
public string Status { get; internal set; }
}
public class PaymentHelper
{
public PaymentDetails Details { get; private set; }
}
which will prevent anything declared outside of that class library from writing to .Id or .Status.
Or, force access to .Id and .Status to go through the helper class instead of allowing read access to a .Details property:
public class PaymentHelper
{
private PaymentDetails _details;
public string Id { get { return _details.Id; } private set { _details.Id=value; } }
public string Status { get { return _details.Status; } private set { _details.Status = value; } }
}
Of course, if you're going to do that, you could just
public calss PaymentDetails
{
public int Id { get; protected set; }
public string Status { get; protected set; }
}
public class PaymentHelper : PaymentDetails
{
}
... assuming that this sort of inheritance fits with the rest of your architecture.
Or, just to illustrate the interface suggestion proposed by #MrDisappointment
public interface IDetails
{
int Id { get; }
string Status { get; }
}
public class PaymentDetails : IDetails
{
public int Id { get; private set; }
public string Status { get; private set; }
}
public class PaymentHelper
{
private PaymentDetails _details;
public IDetails Details { get { return _details; } private set { _details = value; } }
}
So there are two ways that I can think of to deal with this. One is really simple:
public class PaymentDetails
{
private int _id;
private bool _idSet = false;
int Id
{
get
{
return _id;
}
set
{
if (_idSet == false)
{
_id = value;
_idSet == true;
}
else
{
throw new ArgumentException("Cannot change an already set value.");
}
}
}
private string _status;
private bool _statusSet = false;
string Status
{
get
{
return _status;
}
set
{
if (_statusSet == false)
{
_status = value;
_statusSet = true;
}
else
{
throw new ArgumentException("Cannot change an already set value.");
}
}
}
The simple solution only allows values to be set once. Changing anything requires creating a new instance of the class.
The other is rather complex but very versatile:
public interface IPaymentDetails : IEquatable<IPaymentDetails>
{
int Id { get; }
string Status { get; }
}
public class PaymentDetails : IPaymentDetails, IEquatable<IPaymentDetails>
{
public PaymentDetails()
{
}
public PaymentDetails(IPaymentDetails paymentDetails)
{
Id = paymentDetails.Id;
Status = paymentDetails.Status;
}
public static implicit operator PaymentDetails(PaymentDetailsRO paymentDetailsRO)
{
PaymentDetails paymentDetails = new PaymentDetails(paymentDetailsRO);
return paymentDetails;
}
public override int GetHashCode()
{
return Id.GetHashCode() ^ Status.GetHashCode();
}
public bool Equals(IPaymentDetails other)
{
if (other == null)
{
return false;
}
if (this.Id == other.Id && this.Status == other.Status)
{
return true;
}
else
{
return false;
}
}
public override bool Equals(Object obj)
{
if (obj == null)
{
return base.Equals(obj);
}
IPaymentDetails iPaymentDetailsobj = obj as IPaymentDetails;
if (iPaymentDetailsobj == null)
{
return false;
}
else
{
return Equals(iPaymentDetailsobj);
}
}
public static bool operator == (PaymentDetails paymentDetails1, PaymentDetails paymentDetails2)
{
if ((object)paymentDetails1 == null || ((object)paymentDetails2) == null)
{
return Object.Equals(paymentDetails1, paymentDetails2);
}
return paymentDetails1.Equals(paymentDetails2);
}
public static bool operator != (PaymentDetails paymentDetails1, PaymentDetails paymentDetails2)
{
if (paymentDetails1 == null || paymentDetails2 == null)
{
return ! Object.Equals(paymentDetails1, paymentDetails2);
}
return ! (paymentDetails1.Equals(paymentDetails2));
}
public int Id { get; set; }
public string Status { get; set; }
}
public class PaymentDetailsRO : IPaymentDetails, IEquatable<IPaymentDetails>
{
public PaymentDetailsRO()
{
}
public PaymentDetailsRO(IPaymentDetails paymentDetails)
{
Id = paymentDetails.Id;
Status = paymentDetails.Status;
}
public static implicit operator PaymentDetailsRO(PaymentDetails paymentDetails)
{
PaymentDetailsRO paymentDetailsRO = new PaymentDetailsRO(paymentDetails);
return paymentDetailsRO;
}
public override int GetHashCode()
{
return Id.GetHashCode() ^ Status.GetHashCode();
}
public bool Equals(IPaymentDetails other)
{
if (other == null)
{
return false;
}
if (this.Id == other.Id && this.Status == other.Status)
{
return true;
}
else
{
return false;
}
}
public override bool Equals(Object obj)
{
if (obj == null)
{
return base.Equals(obj);
}
IPaymentDetails iPaymentDetailsobj = obj as IPaymentDetails;
if (iPaymentDetailsobj == null)
{
return false;
}
else
{
return Equals(iPaymentDetailsobj);
}
}
public static bool operator == (PaymentDetailsRO paymentDetailsRO1, PaymentDetailsRO paymentDetailsRO2)
{
if ((object)paymentDetailsRO1 == null || ((object)paymentDetailsRO2) == null)
{
return Object.Equals(paymentDetailsRO1, paymentDetailsRO2);
}
return paymentDetailsRO1.Equals(paymentDetailsRO2);
}
public static bool operator != (PaymentDetailsRO paymentDetailsRO1, PaymentDetailsRO paymentDetailsRO2)
{
if (paymentDetailsRO1 == null || paymentDetailsRO2 == null)
{
return ! Object.Equals(paymentDetailsRO1, paymentDetailsRO2);
}
return ! (paymentDetailsRO1.Equals(paymentDetailsRO2));
}
public int Id { get; private set; }
public string Status { get; private set;}
}
public class PaymentHelper
{
private PaymentDetails _paymentDetails;
public PaymentDetailsRO MyPaymentDetails
{
get
{
return _paymentDetails;
}
}
public PaymentHelper()
{
_paymentDetails = new PaymentDetails();
}
public void ModifyPaymentDetails(string someString)
{
// code to take the arguments and modify this._paymentDetails
}
}
The complex solution allows a changeable backing store, but presents a readonly version to the consumer that cannot be changed by outsiders to your helper class.
Note that both patterns only work if you implement them all the way down the object graph or stick to value types and strings.
You can't prevent that, the property returns a refrence to a PaymentDetails, and once somebody has that, it is out of your control.
However, you can just wrap the PaymentDetails. Instead of returning it verbatim, offer only getters for its public properties.
You can also assign access modifiers for the PaymentDetails class like so:
public string Status { get; private set; }
if you don't need the class elsewhere with a public setter.
Yet another solution: Make setters internal
This is the pragmatical way if the PaymentHelper is in the same assembly of PaymentDetails and the clients of PaymentHelper are in another assembly.
Yet another solution: Delegate from PaymentHelper to PaymentDetails.
This is to add the same properties to PaymentHelper as in PaymentDetails.
If you have many properties you can let generate the delegating properties into PaymentHelper by ReSharper. Place cursor on *_paymentDetails* of line
private PaymentDetails _paymentDetails;
Press Alt+Insert->Delegating Members. Then all PaymentHelper properties delegate to PaymentDetails properties.

Making Validation Generic

I have the following C# code. Here the validations are kept outside the class to satisfy Open – Closed Principle. This is working fine. But the challenge is – the validations are not generic. It is specific to employee class (E.g DateOfBirthRuleForEmployee). How do I make the validations generic for all objects (DateOfBirthRuleForAnyObject).
Note: Make Generic <==> Make Type-Independent
Note: I have NameLengthRuleForEmployee validation also. New validation may come in future.
EDIT
Generic Method Example: Using “OfType” in LINQ
CODE
class Program
{
static void Main(string[] args)
{
Employee employee = new Employee();
employee.DateOfBirth = DateTime.Now;
employee.Name = "Lijo";
DateOfBirthRuleForEmployee dobRule = new
DateOfBirthRuleForEmployee();
NameLengthRuleForEmployee nameRule = new
NameLengthRuleForEmployee();
EmployeeManager employeeManager = new EmployeeManager();
employeeManager.AddRules(dobRule);
employeeManager.AddRules(nameRule);
bool result = employeeManager.validateEntity(employee);
Console.WriteLine(result);
Console.ReadLine();
}
}
public interface IEntity
{
}
public interface IRule<TEntity>
{
bool IsValid(TEntity entity);
}
public class DateOfBirthRuleForEmployee : IRule<Employee>
{
public bool IsValid(Employee entity)
{
return (entity.DateOfBirth.Year <= 1975);
}
}
public class NameLengthRuleForEmployee : IRule<Employee>
{
public bool IsValid(Employee employee)
{
return (employee.Name.Length < 5);
}
}
public class Employee : IEntity
{
private DateTime dateOfBirth;
private string name;
public DateTime DateOfBirth
{
get
{
return dateOfBirth;
}
set
{
dateOfBirth = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
public class EmployeeManager
{
RulesEngine<Employee> engine = new RulesEngine<Employee>();
public void AddRules(IRule<Employee> rule)
{
engine.AddRules(rule);
//engine.AddRules(new NameLengthRuleForEmployee());
}
public bool validateEntity(Employee employee)
{
List<IRule<Employee>> rulesList = engine.GetRulesList();
//No need for type checking. Overcame Invariance problem
bool status = true;
foreach (IRule<Employee> theRule in rulesList)
{
if (!theRule.IsValid(employee))
{
status = false;
break;
}
}
return status;
}
}
public class RulesEngine<TEntity> where TEntity : IEntity
{
private List<IRule<TEntity>> ruleList = new
List<IRule<TEntity>>();
public void AddRules(IRule<TEntity> rule)
{
//invariance is the key term
ruleList.Add(rule);
}
public List<IRule<TEntity>> GetRulesList()
{
return ruleList;
}
}
The challange is for your rules to know which property of what type to validate. You can either provide this by implementing an interface that provides just that as suggested by SLaks or by quessing it dynamically or by providing a concrete rule class with a bit more information on how to access the given property, e.g.:
class NameRule<T> : IRule<T>
{
private Func<T, string> _nameAccessor;
public NameRule(Func<T, string> nameAccessor)
{
_nameAccessor = nameAccessor;
}
public bool IsValid(T instance)
{
return _nameAccessor(instance).Length > 10;
}
}
this ofcourse can be used in the following way:
NameRule<Employee> employeeNameRule = new NameRule<Employee>(x => x.name);
employeeManager.addRule(employeeNameRule);

Castle ActiveRecord Save() will update but not create

I'm updating a program and adding a new table to the database. The program uses Castle's ActiveRecord with repositories. I've set the classes and repository up and can get the test case out of the database fine. However, when I try to add a new record to the database, nothing happens. No error and no new record. If I get the test case out of the database and change something then call Save(), the record in the database changes.
This is the code I'm using to test:
ICountryRepository _countryRepository = Country.GetRepository(site.Id);
//get test case and update
Country c = _countryRepository.FindById(1).Value;
c.Name = "c";
_countryRepository.Save(c);
//create new Country and save
Country d = new Country();
d.Id = 2;
d.Name = "d";
_countryRepository.Save(d);
Now as it's a maintenence project with no real time to stop and study how the Castle framework is doing everything, I'm learning as I go along. I've picked up how to do things by studying the rest of the code and there are occasions in the code where the above code has been used to create new records, so I'm sure Save() is the right function to call.
I've tried dropping the create code in with another part of the code that inserts an object into a different table, just to make sure there weren't different levels of permission in play. There are no differences in the database between the table I added and the table I'm basing my code off.
As I've said, my experiences with ActiveRecord and Castle's framework here are few, it could/will be something quite simple. So hopefully someone can point it out to me.
Thanks.
Edit:
Country Class:
[ActiveRecord("Country")]
[SearchEntity]
public class Country : AbstractEntity<Country, ICountryRepository>
{
int _countryId;
string _name;
int _action;
[PrimaryKey("CountryId")]
public int Id
{
get { return _countryId; }
set { _countryId = value; }
}
[SearchProperty]
[Property(Length = 100)]
public string Name
{
get { return _name; }
set { _name = value; }
}
[Property]
public int PostRegisterActionId
{
get { return _action; }
set { _action = value; }
}
}
AbstractRepository since CountryRepository does nothing at the moment:
[Transient]
public abstract class AbstractRepository<T> : IRepository<T> where T : AbstractEntity, new()
{
#region Private Vars
protected FutureQueryRunner _futureQueryRunner;
protected IDynamicSearchService _dynamicSearchService;
protected bool _caching;
protected int _cachedPages;
protected CachingFutureQueryOfList<T> _cachedQuery;
protected IServiceBus _serviceBus;
private string _entityTypeName = string.Empty;
#endregion
#region Constructors
public AbstractRepository(IDynamicSearchService dynamicSearchService, IServiceBus serviceBus)
{
_dynamicSearchService = dynamicSearchService;
_serviceBus = serviceBus;
}
#endregion
#region Public Methods
public virtual void Save(T instance)
{
ActiveRecordMediator<T>.Save(instance);
}
public virtual void Create(T instance)
{
ActiveRecordMediator<T>.Create(instance);
}
public void Delete(T instance)
{
ActiveRecordMediator<T>.Delete(instance);
}
public virtual IFutureQueryOf<T> New()
{
return new NullFutureQuery<T>();
}
public virtual IFutureQueryOf<T> FindById(int id/*, params string[] eagerAssociations*/) // eager associations buggy
{
DetachedCriteria criteria = GetDefaultCriteria()
.Add(Expression.IdEq(id));
/*foreach (string eager in eagerAssociations)
criteria.SetFetchMode(eager, NHibernate.FetchMode.Eager);*/
return new FutureQueryOf<T>(_futureQueryRunner)
.SetCriteria(criteria);
}
public virtual IFutureQueryOfList<T> FindAll(string sortBy, bool sortAsc)
{
DetachedCriteria criteria = GetDefaultCriteria();
if (!string.IsNullOrEmpty(sortBy))
criteria.AddOrder(sortAsc ? NHibernate.Criterion.Order.Asc(sortBy) : NHibernate.Criterion.Order.Desc(sortBy));
return new FutureQueryOfList<T>(_futureQueryRunner)
.SetCriteria(criteria);
}
public virtual IFutureQueryOfList<T> FindAll(int page, int resultsPerPage, string sortBy, bool sortAsc)
{
return FindAll(new DefaultCriteriaProvider<T>(DetachedCriteria.For<T>()),
page,
resultsPerPage,
sortBy,
sortAsc,
"FindAll");
}
public virtual IFutureQueryOfList<T> FindAll(string searchString, int page, int resultsPerPage, string sortBy, bool sortAsc)
{
return FindAll(new SearchStringCriteriaProvider<T>(_dynamicSearchService, searchString),
page,
resultsPerPage,
sortBy,
sortAsc,
"FindAllSearchString_" + searchString);
}
public virtual IFutureQueryOfList<T> FindAll(IListFilter filter, int page, int resultsPerPage, string sortBy, bool sortAsc)
{
return FindAll(new FilterCriteriaProvider<T>(_dynamicSearchService, filter),
page,
resultsPerPage,
sortBy,
sortAsc,
"FindAllListFilter"); // TODO - the cache key needs to represent individual filters
}
public virtual IFutureQueryOf<int> GetCount(string searchString)
{
return new FutureQueryOf<int>(_futureQueryRunner)
.SetCriteria(AddDefaultCriteria(new SearchStringCriteriaProvider<T>(_dynamicSearchService, searchString).GetDetachedCriteria())
.SetProjection(Projections.RowCount()));
}
public virtual IFutureQueryOf<int> GetCount()
{
return new FutureQueryOf<int>(_futureQueryRunner)
.SetCriteria(GetDefaultCriteria()
.SetProjection(Projections.RowCount()));
}
public virtual string EntityType
{
get
{
if (string.IsNullOrEmpty(_entityTypeName))
_entityTypeName = typeof(T).Name;
return _entityTypeName;
}
}
public IRepository<T> EnableCaching(bool caching)
{
_caching = caching;
return this;
}
public IRepository<T> WithPagesToCache(int cachedPages)
{
_cachedPages = cachedPages;
return this;
}
public virtual IRepository<T> ForSite(int siteId)
{
return this;
}
public IRepository<T> RunFutureQueriesWith(FutureQueryRunner futureQueryRunner)
{
_futureQueryRunner = futureQueryRunner;
return this;
}
#endregion
#region Protected Methods
protected virtual DetachedCriteria AddDefaultCriteria(DetachedCriteria criteria)
{
return criteria;
}
protected DetachedCriteria GetDefaultCriteria()
{
return AddDefaultCriteria(DetachedCriteria.For<T>());
}
protected IFutureQueryOf<U> NewQueryOf<U>(DetachedCriteria criteria)
{
return new FutureQueryOf<U>(_futureQueryRunner).SetCriteria(criteria);
}
protected IFutureQueryOfList<U> NewQueryOfList<U>(DetachedCriteria criteria)
{
return new FutureQueryOfList<U>(_futureQueryRunner).SetCriteria(criteria);
}
#endregion
#region Private Methods
private IFutureQueryOfList<T> FindAll(ICriteriaProvider<T> criteriaProvider, int page, int resultsPerPage, string sortBy, bool sortAsc, string cacheKey)
{
CachingFutureQueryOfList<T> rtnVal = null;
bool cached = false;
if (_cachedQuery != null && _caching)
{
rtnVal = _cachedQuery;
cached = rtnVal.SetPage(page, sortBy, sortAsc, cacheKey);
}
if (!cached)
{
rtnVal = new CachingFutureQueryOfList<T>(_futureQueryRunner, page, _cachedPages, resultsPerPage, cacheKey)
.SetCriteria(AddDefaultCriteria(criteriaProvider.GetDetachedCriteria()), sortBy, sortAsc);
if (_caching)
_cachedQuery = rtnVal;
}
return rtnVal;
}
#endregion
#region Criteria Providers
private interface ICriteriaProvider<U>
{
DetachedCriteria GetDetachedCriteria();
}
private class DefaultCriteriaProvider<U> : ICriteriaProvider<U>
{
private DetachedCriteria _criteria;
public DefaultCriteriaProvider(DetachedCriteria criteria)
{
_criteria = criteria;
}
public DetachedCriteria GetDetachedCriteria()
{
return _criteria;
}
}
private class SearchStringCriteriaProvider<U> : ICriteriaProvider<U>
{
private IDynamicSearchService _searchService;
private string _searchString;
public SearchStringCriteriaProvider(IDynamicSearchService searchService, string searchString)
{
_searchService = searchService;
_searchString = searchString;
}
public DetachedCriteria GetDetachedCriteria()
{
return _searchService.GetDetachedCriteria<U>(_searchString);
}
}
private class FilterCriteriaProvider<U> : ICriteriaProvider<U>
{
private IDynamicSearchService _searchService;
private IListFilter _filter;
public FilterCriteriaProvider(IDynamicSearchService searchService, IListFilter filter)
{
_searchService = searchService;
_filter = filter;
}
public DetachedCriteria GetDetachedCriteria()
{
return _searchService.GetDetachedCriteria<U>(_filter);
}
}
#endregion
}
If you're explicitly setting the Country PK, as it seems you're doing, you probably need to call Create() instead of Save() (I don't know if your repository implementation exposes this).
If this didn't work, please post your class mappings and repository implementation.

Categories

Resources