PDA

View Full Version : how to build store model for complex objects (objects has objects as property)



unicorn64
Jun 02, 2015, 8:19 PM
I'm trying to implement LocalStorage for store some data on user's browser but i have a problem,
I want to use complex object as store model like



// sample dto models
public class PersonInfoForLocalStorage
{
public string NameSurname { get; set; }
public int PersonId { get; set; }
public PersonalInfo PersonalInfo { get; set; }
public ExaminationInfo ExaminationInfo { get; set; }

public PersonInfoForLocalStorage()
{
this.PersonalInfo = new PersonalInfo ();
this.ExaminationInfo = new ExaminationInfo ();
}
}

public class PersonalInfo
{
public DateTime DateOfBirth { get; set; }
public string Gender { get; set; }
public string FathersName { get; set; }
public string MothersName { get; set; }
}

public class ExaminationInfo
{
public DateTime OperationTime { get; set; }
public List<PartInfo> PartList { get; set; }
}

public class PartInfo
{
public int PartId { get; set; }
public string PartName { get; set; }
}



and i prepared extension methods for Model and Store classes as


public static class StoreExtensions
{
public static void AddFieldsByType(this Model model,Type t)
{
model.Name = "MyApp.Models."+t.Name;
foreach (var p in t.GetProperties())
{
ModelFieldType mft = ModelFieldType.Auto;
ModelField mf = null;
Type pt = p.PropertyType;
if ((pt.IsAnsiClass) && (pt.IsValueType) && (pt.Name == "Nullable`1"))
{
pt = pt.GenericTypeArguments[0];
}
if (pt == typeof(int))
mft = ModelFieldType.Int;
else if (pt == typeof(bool))
mft = ModelFieldType.Boolean;
else if (pt == typeof(DateTime))
mft = ModelFieldType.Date;
else if (pt == typeof(decimal))
mft = ModelFieldType.Float;
else if (pt == typeof(string))
mft = ModelFieldType.String;
else if ((pt.IsAnsiClass) && (!pt.IsArray) && (!pt.IsValueType) && (!pt.IsGenericType))
{
mft = ModelFieldType.Object;
mf = new ModelField(p.Name, mft);

Model m2 = new Model();
m2.AddFieldsByType(pt);
if (!Model.IsRegistered(m2.Name))
m2.Register(false);
//m2.RegisterOnClient();
//mf.ModelName = m2.Name;
mf.Fields.AddRange(m2.Fields);
//mf.Model.Add(m2);
}

if (mf == null)
mf = new ModelField(p.Name, mft);
model.Fields.Add(mf);
if (!Model.IsRegistered(model.Name))
model.Register(false);
// model.RegisterOnClient();
}
}

public static void SetStoreModel(this Store store, Type modelType)
{
var m = new Model();
m.AddFieldsByType(modelType);
store.Model.Clear();
store.Model.Add(m);
}

public static void BuildStore(this Store store, string storageId, Type modelType, bool storeAutoLoad)
{
var proxy = new LocalStorageProxy();
proxy.StorageID = storageId;
store.Proxy.Clear();
store.Proxy.Add(proxy);
store.AutoLoad =store.AutoSync= storeAutoLoad;
store.SetStoreModel(modelType);
}

public static TBuilder AddFieldsByType<TModel, TBuilder>(this Model.Builder<TModel, TBuilder> builder, Type modelType)
where TModel : Ext.Net.Model
where TBuilder : Ext.Net.Model.Builder<TModel, TBuilder>
{
builder.ToComponent().AddFieldsByType(modelType);
return builder as TBuilder;
}

public static TBuilder SetStoreModel<TStore, TBuilder>(this Store.Builder<TStore, TBuilder> builder, Type modelType)
where TStore : Ext.Net.Store
where TBuilder : Ext.Net.Store.Builder<TStore, TBuilder>
{
builder.ToComponent().SetStoreModel(modelType);
return builder as TBuilder;
}

public static TBuilder BuildStore<TStore, TBuilder>(this Store.Builder<TStore, TBuilder> builder,
string storageId, Type modelType, bool storeAutoLoad)
where TStore : Ext.Net.Store
where TBuilder : Ext.Net.Store.Builder<TStore, TBuilder>
{
builder.ToComponent().BuildStore(storageId,modelTy pe,storeAutoLoad);
return builder as TBuilder;
}
}


after all, i create store in my view as



@(Html.X().Store().ID("MyLocalStore").BuildStore("myLocalStorage", typeof(PersonInfoForLocalStorage), true))


when i run my project and create model object in js i get only NameSurname and PersonId of my model,


console.log(new App.MyLocalStore.model());

unicorn64
Jun 15, 2015, 3:45 PM
Basically, i need to create my model object on javascript side, which is a object of a complex class.
how should i define my ext.net model ???