How to determine lazy-loaded properties at runtime on a linq table? - linq-to-sql

I am iterating over the properties of various mapped tables in my code and need to know whether or not each property is lazy loaded. I have found that the instance variable used for storage, denoted by the Storage attribute on the property, will be of type System.Data.Linq.Link.
Is there a way that I can leverage these two facts at runtime to solve this problem?
Code:
public void LazyLoad(Type tableType)
{
foreach (var prop in tableType.GetGenericArguments()[0].GetProperties())
{
if (/* IS LAZY LOADED */)
{
//real work here...
Console.WriteLine(prop.Name);
}
}
}
The mappings look like this:
public partial class Address
{
private System.Data.Linq.Link<string> _City;
[Column(Storage="_City", DbType="...")]
public string City
{
get { /* ... */ }
set { /* ... */ }
}
}

You are almost there. Just a spoon full of reflection helps the medicine go down ;-)
private static bool IsLazyLoadedProperty(PropertyInfo property)
{
var column = property.GetCustomAttributes(typeof(ColumnAttribute), true)[0]
as ColumnAttribute;
var field = property.DeclaringType.GetField(column.Storage,
BindingFlags.Instance | BindingFlags.NonPublic);
if (!field.FieldType.IsGenericType)
{
return false;
}
return field.FieldType.GetGenericTypeDefinition() == typeof(Link<>);
}

Related

Unity3D: How to access List<T> elements from ParseObject subclass

I cannot seem to access an array of custom objects (that is a column in a Parse table) after querying for it and receiving the results.
I have a simple custom class call "TextEntry" that contains 2 strings.
public class TextEntry
{
public string key;
public string text;
public TextEntry() { }
}
I have a ParseObject subclass called "LocalePO", which has an IList member in addition to other native types.
[ParseClassName("LocalePO")]
public class LocalePO : ParseObject
{
[ParseFieldName("version")]
public int version
{
get { return GetProperty<int>("version"); }
set { SetProperty<int>(value, "version"); }
}
[ParseFieldName("code")]
public string code
{
get { return GetProperty<string>("code"); }
set { SetProperty<string>(value, "code"); }
}
[ParseFieldName("name")]
public string name
{
get { return GetProperty<string>("name"); }
set { SetProperty<string>(value, "name"); }
}
[ParseFieldName("keypair")]
public IList<object> keypair
{
get { return GetProperty<IList<object>>("keypair"); }
set { SetProperty<IList<object>>(value, "keypair"); }
}
public LocalePO() { }
}
I can query to Parse and successfully return a LocalePO object, but I cannot access the specific "TextEntry" members of the "keypair" List afterwards.
var cloudQuery = new ParseQuery<LocalePO>();
var queryTask = cloudQuery.FirstAsync();
// wait for query to return
while (!queryTask.IsCompleted) yield return null;
LocalePO locale = queryTask.Result;
int CloudVersion = locale.version; // this works
List<TextEntry> list = new List<TextEntry>();
list = locale.keypair.Cast<TextEntry>.ToList(); // this doesn't work
foreach (var item in locale.keypair)
{
var entry = item as TextEntry; // this does not work
TextEntry entry = (TextEntry)item; // this doesn't work either
// this is my current solution which works but seems terrible
string json = JsonConvert.SerializeObject(item);
TextEntry entry = JsonConvert.DeserializeObject<TextEntry>(json);
list.Add(entry);
}
I feel like I am overlooking something very simple here, but I just want to convert the data I pull from Parse to local objects so I can use the data throughout the app logic.
It seems to me that Parse prefers the IList of type "object"vs an IList of type "TextEntry" type for the ParseFieldName. For example, Parse always returns null for the field if I have the following:
[ParseFieldName("keypair")]
public IList<TextEntry> keypair
{
get { return GetProperty<IList<TextEntry>>("keypair"); }
set { SetProperty<IList<TextEntry>>(value, "keypair"); }
}
Perhaps I should derive TextEntry from ParseObject too? I'm so confused.
Any help would be appreciated.
Thanks!
Try this:
var cloudQuery = new ParseQuery<LocalePO>();
cloudQuery .Include("keypair");
var queryTask = cloudQuery.FirstAsync();
TextEntry will need to derive from parseObject and you will need to register it as a subclass.
Here is an example of a query I am using which has 2 levels of nested iList's of parseObject subclasses
ParseObject.RegisterSubclass<ProgramDataParse>();
ParseObject.RegisterSubclass<WorkoutDataParse>();
ParseObject.RegisterSubclass<ExerciseDataParse>();
var programQuery = new ParseQuery<ProgramDataParse>()
.OrderByDescending("createdAt").Limit(2)
.Include("workouts")
.Include("workouts.exercises");

Bind to action method

Is it possible to use a simple action method - just like with Caliburn.Micro - instead of a command with MvvmCross bindings?
Example:
public void Action()
{
Tip = 11;
}
<Button
android:text="Button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button1"
local:MvxBind="Click Action" />
It doesn't work out of the box, I tested that.
While I found a lot of samples about adding new target bindings, I didn't find a single one about adding a new source binding.
UPDATE:
This works now out of the box with the Rio binding. To use it, add the MvvmCross MethodBinding NuGet package to the Android project.
Up until now, much of the emphasis for MvvmCross has been on allowing multi-platform target binding with the source remaining mainly 'vanilla' INotifyPropertyChanged.
There have been some deviation in terms of ViewModel structure - e.g.:
the MvxCommandCollection - http://slodge.blogspot.co.uk/2013/03/fixing-mvvm-commands-making-hot-tuna.html
some users using Fody - http://twincoders.com/blog/codigo-limpio-con-fody/
Recently, several new feature requests have also been logged in this area:
AutoCommands - I think this is what you are asking about here - https://github.com/slodge/MvvmCross/issues/301
Rio binding sources - https://github.com/slodge/MvvmCross/issues/299
Tibet binding - https://github.com/slodge/MvvmCross/issues/298
Because of these, I do expect more functionality to be exposed in this area in the future...
With that said, if you wanted to get this working today, then MvvmCross Binding is overrideable so you could fairly easily do it:
1. Implement an ICommand that invokes a MethodInfo using reflection (for completeness this should probably also use a parameter if available) - some kind of InvokeMethodCommand (code for this left to the reader!)
.
2. Implement an MyMethodSourceBinding class which wraps the InvokeMethodCommand - something like:
public class MyMethodSourceBinding : MvxSourceBinding
{
private readonly MethodInfo _methodInfo;
protected MyMethodSourceBinding(object source, MethodInfo methodInfo)
: base(source)
{
_methodInfo = _methodInfo;
}
public override void SetValue(object value)
{
// do nothing - not allowed
}
public override Type SourceType
{
get { return typeof(ICommand); }
}
public override bool TryGetValue(out object value)
{
value = new InvokeMethodCommand(source, _methodInfo);
return true;
}
}
3. Override MvvmCross's registered IMvxSourceBindingFactory with your own implementation that can detect when a method is present - sadly most of this is cut and paste coding today - it would be something like
public class MySourceBindingFactory
: IMvxSourceBindingFactory
{
private IMvxSourcePropertyPathParser _propertyPathParser;
private IMvxSourcePropertyPathParser SourcePropertyPathParser
{
get
{
if (_propertyPathParser == null)
{
_propertyPathParser = Mvx.Resolve<IMvxSourcePropertyPathParser>();
}
return _propertyPathParser;
}
}
public IMvxSourceBinding CreateBinding(object source, string combinedPropertyName)
{
var tokens = SourcePropertyPathParser.Parse(combinedPropertyName);
return CreateBinding(source, tokens);
}
public IMvxSourceBinding CreateBinding(object source, IList<MvxPropertyToken> tokens)
{
if (tokens == null || tokens.Count == 0)
{
throw new MvxException("empty token list passed to CreateBinding");
}
var currentToken = tokens[0];
if (tokens.Count == 1)
{
return CreateLeafBinding(source, currentToken);
}
else
{
var remainingTokens = tokens.Skip(1).ToList();
return CreateChainedBinding(source, currentToken, remainingTokens);
}
}
private static MvxChainedSourceBinding CreateChainedBinding(object source, MvxPropertyToken propertyToken,
List<MvxPropertyToken> remainingTokens)
{
if (propertyToken is MvxIndexerPropertyToken)
{
return new MvxIndexerChainedSourceBinding(source, (MvxIndexerPropertyToken) propertyToken,
remainingTokens);
}
else if (propertyToken is MvxPropertyNamePropertyToken)
{
return new MvxSimpleChainedSourceBinding(source, (MvxPropertyNamePropertyToken) propertyToken,
remainingTokens);
}
throw new MvxException("Unexpected property chaining - seen token type {0}",
propertyToken.GetType().FullName);
}
private static IMvxSourceBinding CreateLeafBinding(object source, MvxPropertyToken propertyToken)
{
if (propertyToken is MvxIndexerPropertyToken)
{
return new MvxIndexerLeafPropertyInfoSourceBinding(source, (MvxIndexerPropertyToken) propertyToken);
}
else if (propertyToken is MvxPropertyNamePropertyToken)
{
//**************************
// Special code is here
var propertyToken = (MvxPropertyNamePropertyToken) propertyToken;
if (source != null)
{
var method = source.GetType().GetMethod(propertyToken.PropertyName, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
return new MyMethodSourceBinding(source, method);
}
}
return new MvxSimpleLeafPropertyInfoSourceBinding(source,
(MvxPropertyNamePropertyToken) propertyToken);
// Special code ends here
//**************************
}
else if (propertyToken is MvxEmptyPropertyToken)
{
return new MvxDirectToSourceBinding(source);
}
throw new MvxException("Unexpected property source - seen token type {0}", propertyToken.GetType().FullName);
}
}
4. Supply this source binding factory in your own custom binding builder - e.g.:
public class MyAndroidBindingBuilder
: MvxAndroidBindingBuilder
{
protected override IMvxSourceBindingFactory CreateSourceBindingFactory()
{
return new MvxSourceBindingFactory();
}
}
5. Supply this binding builder during your setup
public class Setup : MvxAndroidSetup
{
// ....
protected override MvxAndroidBindingBuilder CreateBindingBuilder()
{
return new MyAndroidBindingBuilder();
}
}
Note: This approach is only for advanced users right now... As suggested in the first part of this question, I do expect the code in this area to change quite a lot so you might also encounter some issues maintaining a fork in this area. (Indeed the code in this area has already changed quite significantly on the Tibet Binding branch within the GitHub repo!)

Howto allow any data type to be returned by a function in actionscript 3?

I have a static Settings class where my application can retrieve settings from. The problem is that some of these settings are strings, while others are ints or numbers. Example:
package
{
public final class Settings
{
public static function retrieve(msg:String)
{
switch (msg)
{
case "register_link":
return "http://test.com/client/register.php";
break;
case "time_limit":
return 50;
break;
}
}
}
}
Now, in the first case it should send a string and in the second a uint. However, how do I set this in the function declarement? Instead of eg. function retrieve(msg:String):String or ...:uint? If I don't set any data type, I get a warning.
HanClinto has answered your question, but I would like to also just make a note of another possible solution that keeps the return types, typed. I also find it to be a cleaner solution.
Rather than a static retrieve function, you could just use static consts, such as:
package
{
public final class Settings
{
public static const REGISTER_LINK:String = "my link";
public static const TIME_LIMIT:uint= 50;
}
}
And so forth. It's personal preference, but I thought I would throw it out there.
Use *
public static function retrieve(msg:String):*
{
if (msg == "age") {
return 23;
} else {
return "hi!";
}
}

linq2sql missing event model?

How come the "Table" classes Generated in the Dbml do not contain useful events like
OnBeforeInsert
OnBeforeUpdate
OnAfterInsert
etc.
Am I missing something?
This question is related to frustration trying to set timestamp columns.
UPDATE
I created the following method of doing this neatly what does everyone think?
public class Model
{
internal virtual void OnBeforeInsert()
{
}
internal virtual void OnBeforeUpdate()
{
}
}
public partial class DbDataContext
{
public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{
foreach (var insert in this.GetChangeSet().Inserts)
{
if (insert is Model)
{
((Model)insert).OnBeforeInsert();
}
}
foreach (var update in this.GetChangeSet().Updates)
{
if (update is Model)
{
((Model)update).OnBeforeUpdate();
}
}
base.SubmitChanges(failureMode);
}
}
public partial class Address : Model
{
internal override void OnBeforeInsert()
{
var created = DateTime.Now;
this._Modified = created;
this._Created = created;
}
}
I had a similar issue like this recently.
There is a partial method in the generated class for "OnValidate". Simply declaring the method in your partial will force it to be called (vb.net does not support partial methods like c#) or in c# simply declare a partial method.
The method is passed a System.Data.Linq.ChangeAction enum that is either: Delete, Insert, Update, or None.
Below is a sample of what you did using the built in partial method.
public partial class Address
{
private partial void OnValidate(System.Data.Linq.ChangeAction action)
{
if (action == System.Data.Linq.ChangeAction.Insert)
{
var created = DateTime.Now;
this._Modified = created;
this._Created = created;
} else if (action == System.Data.Linq.ChangeAction.Update) {
this._Modified = DateTime.Now;
}
}
}

Extend a LINQ entity-class with constructor methods and make that entity-class inherit from it's DataContext class

Is it possible to extend LINQ-to-SQL entity-classes with constructor-methods and in the same go; make that entity-class inherit from it's data-context class?--In essence converting the entity-class into a business object.
This is the pattern I am currently using:
namespace Xxx
{
public class User : Xxx.DataContext
{
public enum SiteAccessRights
{
NotRegistered = 0,
Registered = 1,
Administrator = 3
}
private Xxx.Entities.User _user;
public Int32 ID
{
get
{
return this._user.UsersID;
}
}
public Xxx.User.SiteAccessRights AccessRights
{
get
{
return (Xxx.User.SiteAccessRights)this._user.UsersAccessRights;
}
set
{
this._user.UsersAccessRights = (Int32)value;
}
}
public String Alias
{
get
{
return this._user.UsersAlias;
}
set
{
this._user.UsersAlias = value;
}
}
public User(Int32 userID)
{
var user = (from u in base.Users
where u.UsersID == userID
select u).FirstOrDefault();
if (user != null)
{
this._user = user;
}
else
{
this._user = new Xxx.Entities.User();
base.Users.InsertOnSubmit(this._user);
}
}
public User(Xxx.User.SiteAccessRights accessRights, String alias)
{
var user = (from u in base.Users
where u.UsersAccessRights == (Int32)accessRights && u.UsersAlias == alias
select u).FirstOrDefault();
if (user != null)
{
this._user = user;
}
else
{
this._user = new Xxx.Entities.User
{
UsersAccessRights = (Int32)accessRights,
UsersAlias = alias
};
base.Users.InsertOnSubmit(this._user);
}
}
public void DeleteOnSubmit()
{
base.Users.DeleteOnSubmit(this._user);
}
}
}
Update:
Notice that I have two constructor-methods in my User class. I'd like to transfer those to the User entity-class and extend the User entity-class on it's data-context class, so that the data-context is available to the entity-class on "new-up".
Hope this makes sense.
Rick Strahl has a number of really good articles that address what I think you are looking for. Check out his list of Linq Articles Here
Inheriting an entity from a data context is a bad idea. They are two discrete objects and are designed to operate that way. Doing this will cause all sorts of issues least of all problems with trying to submit a bunch of related changes together at the same time - going through multiple data contexts will cause this to fail as each tries to work independently.
It doesn't seem to make sense to make an entity a type of DataContext. It doesn't need to be a DataContext in order to be considered a business object, nor do you necessarily need to create a type that contains the original entity. It might be better to just extend the entity class and contain a reference to a DataContext using composition:
namespace Xxx.Entities
{
public partial class User : IDisposable
{ DataContext ctx;
public static GetUserByID(int userID)
{ var ctx = new DataContext();
var user = ctx.Users.FirstOrDefault(u=>u.UsersID == userID);
if (user == null)
{
user = new User();
ctx.Users.InsertOnSubmit(user);
}
user.ctx = ctx;
return user;
}
public void Dispose() { if (ctx != null) ctx.Dispose(); }
}
}
If you just want the property names to be different than the database column names, do that in the mapping file.