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 7027167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:44+00:00 2026-05-28T00:12:44+00:00

Consider the following code: public abstract class RepositoryBase<T> where T : class { #region

  • 0

Consider the following code:

public abstract class RepositoryBase<T> where T : class
{
    #region Members

    private MyContext dataContext;
    private readonly IDbSet<T> dbset;

    #endregion

    protected RepositoryBase(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get; private set;
    }

    protected MyContext DataContext
    {
        get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
    }

    public virtual void Delete(T entity)
    {
        dbset.Remove(entity);
    }

I would like to replace the Delete method with the one below, since I would prefer to simply set a Deleted field to true in my object to indicated that it is deleted, instead of really deleting it.

    public virtual void Delete(T entity)
    {
        entity.Deleted = true;
        dbset.Attach(entity);
        dataContext.Entry(entity).State = EntityState.Modified;
    }

I am using POCO entities and the Deleted property exists in all of them. However, in the code above entity is of type T, and T “doesn’t know” that there is a Deleted property in all objects it can represent. What is the most elegant way to solve this?

By the way, I would like to access other fields (DateCreated, CreatedBy, DateModified and ModifiedBy) in a similar way in my abstract class.

UPDATE: I tried both the interface and the Abstract class solution, which seemed good at first, but then I got the following error message in both cases when compiling:

Error 11 The type ‘MyProject.Domain.Person’ cannot be used as type parameter ‘T’ in the generic type or method ‘MyProject.Data.Infrastructure.RepositoryBase’. There is no implicit reference conversion from ‘MyProject.Domain.Person’ to ‘MyProject.Domain.AbstractEntity’.

And here is the code the error message is referring to:

namespace MyProject.Data
{ 
    public class PersonRepository : RepositoryBase<Person>, IPersonRepository
    {
        public PersonRepository(IDatabaseFactory databaseFactory)
            : base(databaseFactory)
        {
        }
    }

    public interface IPersonRepository : IRepository<Person>
    {

    }
}

Update 2:

I finally got it to work with the solution proposed by SLaks. I used an interface, and I modified the template generating all the POCO entities so that they would all derive from the following IEntity interface:

namespace MyProject.Domain
{
    public interface IEntity
    {
        System.DateTime CreatedDate
        {
            get;
            set;
        }

        string CreatedBy
        {
            get;
            set;
        }

        System.DateTime ModifiedDate
        {
            get;
            set;
        }

        string ModifiedBy
        {
            get;
            set;
        }

        bool Deleted
        {
            get;
            set;
        }
    }
}

Using an abstract entity class would be more complicated, since then all the properties in the abstract class would have to be overridden in the entity classes.

  • 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-05-28T00:12:45+00:00Added an answer on May 28, 2026 at 12:12 am

    You should create an interface with Deleted and other properties and implement it in your entity classes.
    You can then constrain the generic type parameter to implement the interface.

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

Sidebar

Related Questions

Consider the following code : public class RandomClass { private readonly string randomString; public
I have a question about .net generics. Consider the following code: public abstract class
Consider the following piece of code: class B { private: // some data members
Consider the following code: public class Foo { private static final Object LOCK =
Consider the following code snipet: public class A { private final Executor executor =
Consider the following code: public class InboxMenuItemDynamicProvider : DynamicNodeProviderBase { private IMyService _myService {
Let's consider the following code: public class MyPanel extends JPanel { private long secretInfo
Consider the following code: public abstract class Test1 { public object Data { get;
Consider the following code: public abstract class Base { public void getAnswer(); } public
Consider the following code: [Serializable] public class Region { public double North {get; set;}

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.