Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:29:37+00:00 2026-06-06T21:29:37+00:00

A bit new at the Repository Pattern. Trying to build a generic repository that

  • 0

A bit new at the Repository Pattern. Trying to build a generic repository that will also handle state changes to derived entities. What I’ve done so far is create a custom attribute to flag the property as one which needs to be upserted.

Attribute:

public class DerivedObjectAttribute : Attribute
{
    public enum EntityType
    {
        REFERENCE,
        OBJECT
    }

    public EntityType DerivedType { get; set; }
}

I define this attribute for any property on which I want to cascade state changes.

Sample Entity:

public class FightingCharacter : BaseEntity 
{
    public Costume costume { get; set; }

    [DerivedObject(DerivedType=DerivedObjectAttribute.EntityType.REFERENCE)]
    public SpecialFinish specialFinish { get; set; }

    [DerivedObject(DerivedType = DerivedObjectAttribute.EntityType.REFERENCE)]
    public List<MoveList> moveList { get; set; }
}

So for this class, the costume property would not need to cascade, but the specialFinish and moveList properties should.

Then in my repository:

public class DataRepository<T> : IRepository<T> where T : BaseEntity {
    private void TryDerivedUpsert(T entity)
    {
        Type type = entity.GetType();
        PropertyInfo[] piList = type.GetProperties();
        foreach (PropertyInfo pi in piList)
        {
            foreach (DerivedObjectAttribute attr in pi.GetCustomAttributes(typeof(DerivedObjectAttribute), false))
            {
                // What to do here?
            }
        }
    }
}

In the innermost loop, I’m able to pinpoint DerivedObjectAttributes without any problem. The question is: how do I obtain the Type and Value of the object, then upsert it? In other words: if property pi is flagged to cascade changes, create a repo cast to the appropriate Entity, and Upsert it. E.G.:

DataRepository<EntityType> repo = new DataRepository<EntityType> ();
repo.Upsert(property as EntityType);

Does that make sense? Or am I going about generic repo entirely the wrong way? If it does make sense (I’ll be surprised), how to do it? (The examples listed here are just examples, BTW. I’m still architecting and have no EF classes at all yet.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:29:41+00:00Added an answer on June 6, 2026 at 9:29 pm

    You could get the value with pi.GetValue(entity, null), and create the generic repository (Activate.CreateInstance) for the property type (from pi), but you will have to do a lot of reflection.

    In this case you should think about dropping the classic generic repository idea (separate repository per type) and use something like an extended DbContext, that can handle all types.

    If you have a disconnected scenario (WCF), the main problem will be EF itself, because you have to replicate all changes to nested lists on the server side and manually change the EntityState.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit new to js and have been trying to figure out how
I'm a bit new to Papervision3D, and I'm trying to set the alpha level
I'm having a bit of trouble figuring out how to make this repository pattern
I am new to MVC and using the repository pattern in an attempt to
I'm trying to implement the repository pattern using entity framework code first rc 1.
I am using the Generic Repository pattern to abstract my EF 4.1 DbContext. I
I am trying to understand something a bit better with being new to C#,
I'm a bit new to linq and I'm unsure if what I desire is
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
I'm a bit new to wordpress plugin coding. I've been working on a plugin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.