StaticRepository in test project

Feb 16, 2010 at 10:51 AM

Hi guys,

So i managed to get the base generic functions to work in my own project (standard CRUDs) no problem following the guidelines which was great.

However trying to follow the example with the static repository then use those functions in the GenericObjectDataSource has been throwing me.

Im getting multiple errors and when i go to designer view to try and configure it that way i cant even see the methods in the dropdowns (this is after just manually trying to enter it into source view).

Then i noticed this is similar to the test project, i then realised that potentially this is more useful for when you dont have the object as such and you still want to perform things statically is this correct

and i am just an idiot?

 

I also after a while of re-reading etc noticed that it says on the homepage "Concrete Repository classes which inherit from GenericRepository and implement custom data access code for specific database entities - This project has two,

ProductRepository and CategoryRepository" which isnt in the project i downloaded however is the right track if i want to be able to use them in the GenericObjectDataSource? Doing this through implementing a partial class of the actual

repository says "public partial class ProductRepository" then creating the extra methods? As i cant get this to work either...

 

I hope this question isnt to simple and i truly am an idiot lol...

 

Thanks again,

Pete

Feb 16, 2010 at 11:36 AM

Ok i dont know what i was doing wrong before but i have got it working as per the project just doing it through source view for a select statement so thats all well and good.

 

My next question would be is there a way to say pass in the object along with another variable for an insert. For example in the ProductStaticRepository have the method

InsertWithCategory(Product entity, Category category)? As i get an error about non-generic method with parameters etc etc

 

Or is it better to go with the approach of creating the category in the OnInserting event of the GenericObjectDataSource and placing it into the object there?

Or in different words what is the best way to add a child to a parent at the same time the parent is created so that if the parent fails/child fails they both fail which does seem to work using the line above but im wondering

if there is a better way to go around it, if possible in some scenarios at the DataLayer?

 

Thanks again,

Pete

Developer
Feb 16, 2010 at 2:10 PM

Where are the generated entities and repositories located?
"<Your DBML name>.generated.tt" (e.g. NorthWindDataLayer\DataClasses.generates.cs) file contains all the generated entites and repository helper classes.

How do I regenerate the entities and repositories after updating my dbml?
Click on the "<Your DBML name>.tt" (e.g. NorthWindDataLayer\DataClasses.tt) file and click save. It will recreate the "<Your DBML name>.generated.cs" (e.g. NorthWindDataLayer\DataClasses.generates.cs) file.

How do I extend the entities or repository helpers?
Create a partial class in the same namespace as the generated entities. At compile time they will be combined into a single class. We use partial classes as you don't wont your custom code to be overwritten when regenerating the tt template.

How to do complex inserts (always override default insert behaviour as its easier in the long run)

    protected void SchoolBlogFormView_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        var blogFormView = (FormView)sender;
        switch (e.CommandName)
        {
            case "Cancel":
                Response.Redirect(this.ResolveUrl(Urls.View_SchoolBlogList(State.CurrentSchoolUrl)), false);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                break;
            case "Insert":
                InsertBlog(blogFormView);
                break;
        }
    }
    
    protected void SchoolBlogFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        e.Cancel = true; //Handled via InsertBlog
    }
    
    protected void InsertMessage(bool visible)
    {
        SchoolBlogFormView.Visible = !visible;
        InsertCompletePanel.Visible = visible;
    }
    
    protected void InsertBlog(FormView insertItem)
    {
        Page.Validate("InsertSchoolBlogEntry");
        if (!IsValid) return;

        var insertSchoolBlogTypeDropDownList = (DropDownList)insertItem.FindControl("InsertSchoolBlogTypeDropDownList");
        var blogShareTypeId = Convert.ToInt32(insertSchoolBlogTypeDropDownList.SelectedValue);
        
        var insertNickNameTextBox = (TextBox)insertItem.FindControl("InsertNickNameTextBox");
        var nickName = insertNickNameTextBox.Text;
        
        using (var transactionScope = new TransactionScope())
        {
            var schoolBlog = new SchoolBlog
            {
                BlogShareTypeId = blogShareTypeId,
            };
            SchoolBlogRepositoryHelper.Insert(schoolBlog);
            
            var user = UserRepositoryHelper.GetEntity(State.UserId);
            user.Nickname = nickName;
            UserRepositoryHelper.Update(user);
            
            transactionScope.Complete();
        }
        
        InsertMessage(true);
    }

Feb 16, 2010 at 11:13 PM

Thanks for the indepth response i had already got the tt files and generated.cs working correctly.

And managed to get the staticrepository to work too just through source view of the GenericObjectDataSource still doesnt work in Design View but meh.

 

I was guessing that the complex object would have to be external, so lastly if i was just generating the complex object with only 1 information, in this case the UserId with

a few defaults would it be acceptable to do the following?

 

protected void GenericObjectDataSource1_OnInserting(object sender, ObjectDataSourceMethodEventArgs e)
    {

        if (currentUser.IsAuthenticated())
        {

            ActivityMember activityCreator = new ActivityMember()
            {
                UserID = currentUser.UserID,
                DateJoined = DateTime.Now,
                DateModified = DateTime.Now,
                IsAccepted = true,

            };
            ((Activity)e.InputParameters[0]).ActivityMembers.Add(activityCreator);
        }
}

 

Where InputParameters[0] is obviously the ActivityObject and ActivityMembers a child property of Activity

 

Thanks again,

Pete