This is a migrated thread and some comments may be shown as answers.

CRUD with Artificial Types

2 Answers 87 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 01 Sep 2011, 01:12 AM

I am looking to use artificial types/properties in my app and am trying to put all the scattered pieces together on how to handle some real world scenarios.
I've been able to do the following:
1. Extend my existing model following the guide in the documentation section "Extend Existing Models with New Types"
2. Create new artificial types and run the schema update against the database
3. Create new instances of the artificial types to insert new entries like so:

 

IPersistentTypeDescriptor customdataDescriptor = CustomDataContext.Scope.PersistentMetaData.GetPersistentTypeDescriptor("customfieldsNamespace.customfields");
 object customdata = customdataDescriptor.CreateInstance(1);

 

4. Also figured out how to query using dynamic linq like:

var test = CustomDataContext.Scope.ExtentByName("customfieldsNamespace.customfields")
                .Where("testcolumn == @0", "test");

 

Is there a beter way to query artificial types?

Now I'm having trouble with trying update and delete. Are there any examples that show how to do all CRUD operations using artificial types? I'm not sure where to begin. I understand that I can set values using:

 

PropertyDescriptor productNameDescriptor = productDescriptor.GetProperties()["name"];
 productNameDescriptor.SetValue(product, "My Product");

but how can I load an existing record from the database into the PropertyDescriptor instance to be updated?
Also... how can I do a delete operation on an Artificial Type instance?

Any guidance is appreciated.

Thanks,
Mike

2 Answers, 1 is accepted

Sort by
0
Nikola
Telerik team
answered on 06 Sep 2011, 02:05 PM
Hi Mike,

Sorry for the late response. You have actually found the best way to query the context for artificial types.
Another approach would be to dynamically create a generic implementation of the scope Extent<> method that would utilize your artificial type. Something like this:

Type FindArtificial(string name)
{
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        if (assembly.IsDynamic)
        {
            Type t = assembly.GetType(name, false);
            if (t != null)
                return t;
        }
    }
    throw new ArgumentOutOfRangeException("name", name, "Type not found");
}
  
public void QueryArtificialType()
{
    Type t = FindArtificial("Telerik.OpenAccess.Tests.CSModel.BType");
    MethodInfo mi =typeof(ExtensionMethods).GetMethod("Telerik.OpenAccess.Extent").MakeGenericMethod(t);
    IQueryable e = (IQueryable)mi.Invoke(null, new object[] { Scope });
}


As for you other questions, here is an example of how you can obtain and modify an artificial object of type Car. Getting a type descriptor for a loaded instance can be done with the GetExtendedTypeDescriptor
method.

IQueryable<PersistenceCapable> query
    = fluentContext.Scope.ExtentByName("ConsoleApplication10.Car").Where("CarID == @0", carId) as IQueryable<PersistenceCapable>;
PersistenceCapable instance = query.FirstOrDefault();
 
ICustomTypeDescriptor descriptor = fluentContext.Scope.PersistentMetaData.GetExtendedTypeDescriptor(instance);
PropertyDescriptor propertyDescriptor = descriptor.GetProperties()["make"];
propertyDescriptor.SetValue(instance, "Opel");

fluentContext.SaveChanges();

Alternatively, you could invoke the following code if you wanted to delete this Car.
fluentContext.Delete(instance);
 
fluentContext.SaveChanges();

If you need any further assistance please don't hesitate to ask.

 All the best,
Nikola
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

0
Mike
Top achievements
Rank 1
answered on 07 Sep 2011, 07:42 PM
Thank you! This helped tremendously.

-Mike
Tags
Development (API, general questions)
Asked by
Mike
Top achievements
Rank 1
Answers by
Nikola
Telerik team
Mike
Top achievements
Rank 1
Share this question
or