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

Forward Mapping in Runtime

6 Answers 109 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.
Joebet
Top achievements
Rank 1
Joebet asked on 24 Jun 2010, 06:48 AM
Good day.. Been searching the forum for an answer but i had no luck. Im new in Telerik ORM may i know how can i forward map my class in runtime? i already defined the properties. tnx

6 Answers, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 24 Jun 2010, 05:25 PM
Hello Joebet,

I suppose you want to know how to persist your objects with Telerik OpenAccess forward mapping capabilities. You mention “runtime mapping” and what you probably mean is persisting new objects of an already defined type when you run the application. Here I provide a brief overview of the basic steps you need to follow. The first step which you have already done is to define a class you want to persist. After enabling your project to use Telerik OpenAccess you have two options at your disposal to proceed. You can either mark the class with an attribute [Telerik.OpenAccess.Persistent] or use the Forward Mapping Wizard to select the classes you want to make persistent. In order to interact with the database you need a context to bridge the gap between your application logic and the database storage. Such a context is the ObjectScopeProvider which you can specify to be generated when using the Forward Mapping Wizard. When you have the IObjectScope context ready, you can start transactions and implement your custom logic for handling your objects. Beware that you need to handle your objects through the context, i.e. the objects that need to be changed should be either retrieved with the scope or added to it. Since there is a step by step description in our documentation regarding this and other basic topics, here I provide a link:       

http://www.telerik.com/support/documentation-and-tutorials/step-by-step-tutorial-for-openaccess.aspx

The Getting Started section is a good start.

Hope this is the information you need. If you want to know something else or elaborate on the question you have posed, contact us again and we will be available to help.



Sincerely yours,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joebet
Top achievements
Rank 1
answered on 25 Jun 2010, 07:15 AM
Hi Petko,
Thanks for the reply. I had already accomplished forward mapping my  class types and made its table using this script.
 Try 
            Dim db As DatabaseDatabase = Database.Get("Connection") 
            ' Testing if database exists 
            If db.GetSchemaHandler().DatabaseExists() = False Then 
                db.GetSchemaHandler().CreateDatabase() ' conditionally creating it 
            End If 
            Dim ddl As String = db.GetSchemaHandler().CreateUpdateDDLScript(Nothing) 
            If String.IsNullOrEmpty(ddl) = False Then 
                db.GetSchemaHandler().ExecuteDDLScript(ddl) ' conditionally correcting it 
            End If 
 
            MsgBox("Object Successfully Created") 
        Catch ex As Exception 
            MessageBox.Show(ex.Message) 
        End Try 

My question now is how can i get the API of the backend configuration setting that can be found at the telerik context menu to set that my class to be forward map will be using a stored procedure so that it will automatically create its CRUD SP. also can i manually add the bellow stored procedures in forward map during runtime?  Sorry for my bad english..tnx

Public Class StoredProcedure  
  
    Public Shared Function UpdateSupplier(ByVal scope As IObjectScope,ByVal m_Counter As String,ByVal m_FirstName As String,ByVal m_MiddleName As String,ByVal m_LastName As String) As IQueryResult  
        Dim query As IQuery = scope.GetSqlQuery("[UpdateSupplier] ?,?,?,?", Nothing, "LONGVARCHAR m_Counter,LONGVARCHAR m_FirstName,LONGVARCHAR m_MiddleName,LONGVARCHAR m_LastName")  
        Dim res As IQueryResult = query.Execute(New Object() {m_Counter,m_FirstName,m_MiddleName,m_LastName})  
        Dim count As Integer = res.Count 'executes the query  
          
          
          
        Return res  
    End Function  
  
    Public Shared Function ReadSupplier(ByVal scope As IObjectScope,ByVal counter As Nullable(Of Long)) As IQueryResult  
        Dim query As IQuery = scope.GetSqlQuery("[ReadSupplier] ?", Nothing, "BIGINT Counter")  
        Dim res As IQueryResult = query.Execute(New Object() {counter})  
        Dim count As Integer = res.Count 'executes the query  
          
          
          
        Return res  
    End Function  
  
    Public Shared Function DeleteSupplier(ByVal scope As IObjectScope,ByVal m_Counter As Nullable(Of Integer)) As IQueryResult  
        Dim query As IQuery = scope.GetSqlQuery("[DeleteSupplier] ?", Nothing, "INTEGER m_Counter")  
        Dim res As IQueryResult = query.Execute(New Object() {m_Counter})  
        Dim count As Integer = res.Count 'executes the query  
          
          
          
        Return res  
    End Function  
  
    Public Shared Function CreateSupplier(ByVal scope As IObjectScope,ByVal m_FirstName As String,ByVal m_MiddleName As String,ByVal m_LastName As String) As IQueryResult  
        Dim query As IQuery = scope.GetSqlQuery("[CreateSupplier] ?,?,?", Nothing, "LONGVARCHAR m_FirstName,LONGVARCHAR m_MiddleName,LONGVARCHAR m_LastName")  
        Dim res As IQueryResult = query.Execute(New Object() {m_FirstName,m_MiddleName,m_LastName})  
        Dim count As Integer = res.Count 'executes the query  
          
          
          
        Return res  
    End Function  
End Class  

0
Ady
Telerik team
answered on 25 Jun 2010, 03:37 PM
Hi Joebet,

 From what I understand you want OpenAccess to create the CUD stored procedures at runtime, similar to the way you are creating the tables at runtime. Am I right?
If you add the following entry to the app.config - <useStoredProceduresForDelete>True</useStoredProceduresForDelete>  (similar entries for Insert and Update will be required)
and use the code you provided below, OpenAccess will calculate the required CUD stored procedures, create them (if there are not already present) and will use them for CUD operations.

Hope I have answered your question. Do get back in case you need further clarification

Best wishes,
Ady
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joebet
Top achievements
Rank 1
answered on 28 Jun 2010, 03:09 AM
Good day sir.. sorry for my bad explaination. what i am trying to accomplish is to forward map a SP that is not CRUD. example like checking if the data already exist. copying a data from a table to another.  an example is below.. this sp checks if the data already existed in the database.
USE [ISSIDEMOUS] 
GO 
/****** Object:  StoredProcedure [dbo].[CheckExistingData]    Script Date: 06/28/2010 10:02:29 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[CheckExistingData]       
   @TableName NVARCHAR(128) = NULL,       
   @ColumnName NVARCHAR(128) = NULL,       
   @Value NVARCHAR(30) = NULL,       
   @RecordExists BIT OUTPUT       
       
 AS       
       
   
SET NOCOUNT ON       
   
SELECT ChildTableName, ChildColumn,0 AS 'ObjectID'  INTO ##TempTable FROM DataDictionaryRelationship  WHERE ParentTableName  = @TableName ORDER BY ChildTableName    
   
UPDATE ##TempTable SET ##TempTable.ObjectID = S.ID FROM SYSOBJECTS S  WHERE (OBJECTPROPERTY(S.ID, 'IsUserTable') = 1) AND (S.NAME <> 'dtProperties') AND (S.NAME = ##TempTable.ChildTableName)   
   
SET ROWCOUNT 1       
       
DECLARE        
@TempTableName NVARCHAR(128),       
@Query NVARCHAR(500),       
@ObjectID INT,       
@ColName NVARCHAR(128)       
   
     
WHILE EXISTS (SELECT 1 FROM ##TempTable)       
   BEGIN       
      SELECT @TempTableName = ChildTableName,       
             @ObjectIDObjectID = ObjectID,       
      @ColName = ChildColumn,        
             @Query = N'SELECT 1 AS IsRecordExist FROM [' + ChildTableName + '] WHERE ' + ChildColumn + ' = ''' + @Value + ''''       
      FROM ##TempTable       
       
      PRINT @TempTableName       
       
      -- Check column existence on the table       
      SELECT 1 AS IsColumnExisting      
      FROM SYSCOLUMNS C       
      WHERE C.ID = @ObjectID       
      AND C.Name = @ColName       
       
      IF @@ROWCOUNT > 0       
         BEGIN       
            -- Check data existence on the table       
            EXEC SP_EXECUTESQL @Query       
       
            SET @RecordExists = @@ROWCOUNT       
                
            IF @RecordExists > 0       
               BEGIN       
                  PRINT 'Table : ' + @TempTableName       
                  BREAK       
               END       
          END       
       
      -- Delete the temp table record one by one       
      DELETE       
      FROM ##TempTable       
      WHERE ChildTableName = @TempTableName       
   END       
       
SET ROWCOUNT 0       
       
DROP TABLE ##TempTable    
   
   
 


What I am planning to do in order to covert this code in telerik readable format is to reverse map the above code. then eventually forward map this in runtime (during system installation). again sir sorry for my bad explanation about my early post. how can I forward map the above sp to the database? and is it possible to have a SP without the physical SP in the database?  
0
Joebet
Top achievements
Rank 1
answered on 29 Jun 2010, 10:09 AM
ANYONE?
0
Ady
Telerik team
answered on 29 Jun 2010, 05:52 PM
Hi Joebet,

 When you reverse map a stored procedure , the Reversemapping wizard creates a static method(that uses ObjectScope.GetSqlQuery) which can be used to call this stored procedure. OpenAccess does not know/analyze the actual stored procedure content.

If you want to generate your stored procedure when you deploy your application you could use the 'ISchemaHandler.ExecuteDDLScript' method and specify the stored procedure SQL. You can find an example of how to use this method, here.

Hope this answers your query. Do get back in case you need further assistance.

Kind regards,
Ady
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Development (API, general questions)
Asked by
Joebet
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Joebet
Top achievements
Rank 1
Ady
Telerik team
Share this question
or