Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 230 lines (191 sloc) 5.52 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;

namespace FluentCassandra
{
/// <summary>
///
/// </summary>
/// <typeparam name="T">A type that implements <see cref="IFluentColumn"/>.</typeparam>
public abstract class FluentRecord<T> : DynamicObject, IFluentRecord, INotifyPropertyChanged, IEnumerable<IFluentBaseColumn>, ILoadable
where T : IFluentBaseColumn
{
/// <summary>
///
/// </summary>
public FluentRecord()
{
MutationTracker = new FluentMutationTracker(this);
SuppressMutationTracking = false;
}

/// <summary>
/// The record columns.
/// </summary>
public abstract IList<T> Columns { get; }

public T GetColumn(object name)
{
var col = Columns.FirstOrDefault(c => c.ColumnName == name);

if (col == null)
return default(T);

return col;
}

public bool SetColumn(object name, object value)
{
return TrySetColumn(name, value);
}

public bool RemoveColumn(object name)
{
return TryRemoveColumn(name);
}

/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="result"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract bool TryGetColumn(object name, out object result);

/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract bool TrySetColumn(object name, object value);


///
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract bool TryRemoveColumn(object name);

/// <summary>
///
/// </summary>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override IEnumerable<string> GetDynamicMemberNames()
{
return Columns.Select(x => x.ColumnName.GetValue<string>());
}

/// <summary>
///
/// </summary>
/// <param name="binder"></param>
/// <param name="result"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return TryGetColumn(binder.Name, out result);
}

/// <summary>
///
/// </summary>
/// <param name="binder"></param>
/// <param name="indexes"></param>
/// <param name="result"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
object index0 = indexes[0];
return TryGetColumn(index0, out result);
}

/// <summary>
///
/// </summary>
/// <param name="binder"></param>
/// <param name="value"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool TrySetMember(SetMemberBinder binder, object value)
{
return TrySetColumn(binder.Name, value);
}

/// <summary>
///
/// </summary>
/// <param name="binder"></param>
/// <param name="indexes"></param>
/// <param name="value"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
object index0 = indexes[0];
return TrySetColumn(index0, value);
}

/// <summary>
///
/// </summary>
/// <param name="binder"></param>
/// <param name="value"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
return TryRemoveColumn(binder.Name);
}

/// <summary>
///
/// </summary>
/// <param name="binder"></param>
/// <param name="indexes"></param>
/// <param name="value"></param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes)
{
object index0 = indexes[0];
return TryRemoveColumn(index0);
}

protected bool SuppressMutationTracking { get; private set; }

void ILoadable.BeginLoad()
{
SuppressMutationTracking = true;

if (Columns is FluentColumnList<T>)
((FluentColumnList<T>)Columns).SupressChangeNotification = true;
}

void ILoadable.EndLoad()
{
SuppressMutationTracking = false;

if (Columns is FluentColumnList<T>)
((FluentColumnList<T>)Columns).SupressChangeNotification = false;
}

#region IEnumerable<IFluentBaseColumn> Members

public IEnumerator<IFluentBaseColumn> GetEnumerator()
{
return Columns.Cast<IFluentBaseColumn>().GetEnumerator();
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

protected void OnColumnMutated(MutationType type, IFluentBaseColumn column)
{
MutationTracker.ColumnMutated(type, column);

if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(column.ColumnName));
}

#endregion

#region IFluentRecord Members

IEnumerable<IFluentBaseColumn> IFluentRecord.Columns
{
get { return Columns.OfType<IFluentBaseColumn>(); }
}

public IFluentMutationTracker MutationTracker
{
get;
private set;
}

#endregion
}
}
Something went wrong with that request. Please try again.