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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:35:50+00:00 2026-05-22T00:35:50+00:00

i use generic properties on my project,but i dont know,is there any disadvantage use

  • 0

i use generic properties on my project,but i dont know,is there any disadvantage use them,please tell me a scenario,they have a disadvantage?my part of code below.

public class GenericResult<T>
{
    public T Data { get; set; }
    public bool IsSuccess { get; set; }
    public string Message { get; set; }
}

public GenericResult<int> AddCategory(TCategory tCategory)
{
    GenericResult<int> result = new GenericResult<int>();

    //business logic validation,dont make sense,only example :)
    if (tCategory.Name.Lenght > 100)
    {
        result.IsSuccess = false;
        result.Message = "Category Name length is too long";
        result.Data = 0;
    }

    //handle .net runtime error//may be database is not aviable.
    try
    {
        result.Data = this.catalogRepository.AddCategory(tCategory);
        result.IsSuccess = true;
    }
    catch (Exception ex)
    {
        result.Data = 0;
        result.IsSuccess = false;
        result.Message = ex.Message;
    }
    return result;
}

public GenericResult<IEnumerable<TCategory>> GetCategoryHierarchy(TCategory parentCategory)
{
    GenericResult<IEnumerable<TCategory>> result = new GenericResult<IEnumerable<TCategory>>();
    try
    {
        IEnumerable<TCategory> allCategories = catalogRepository.GetAllCategories();
        result.Data = GetCategoryHierarchy(allCategories, parentCategory);
        result.IsSuccess = true;

    }
    catch (Exception ex)
    {
        result.IsSuccess = false;
        result.Data = null;
        result.Message = ex.Message;
    }
    return result;
} 
  • 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-22T00:35:50+00:00Added an answer on May 22, 2026 at 12:35 am

    If you don’t want to throw an exception but prefer to return a result containing either the error or the value i.e. a MayBe that’s fine in some situations. But to be honest in this situation I’d prefer simply throwing/passing through the exception.

    I’d prefer returning an immutable struct as MayBe instead of a mutable class like you did. It’s very similar to Nullable<T>, except it works on reference types and can store an error. Something like:

    public struct MayBe<T>
    {
        private T value;
        private Exception error;
    
        public bool HasValue{get{return error==null;}}
        public T Value
        {
          if(error!=null)
            throw error;
          else
            return value;
        }
    
        public static MayBe<T> CreateError(Exception exception)
        {
          return new MayBe<T>(default(T),exception);
        }
    
        public static MayBe<T> CreateValue(T value)
        {
          return new MayBe<T>(value,null);
        }
    
        public static implicit operator MayBe<T>(T value)
        {
            return CreateValue(value);
        }
    
        public override string ToString()
        {
            if(HasValue)
                return "Value: "+Value.ToString();
            else
                return "Error: "+Error.GetType().Name+" "+Error.Message;
        }
    }
    

    Your code becomes

    public MayBe<int> AddCategory(TCategory tCategory)
    {
        try
        {
           return this.catalogRepository.AddCategory(tCategory);
        }
        catch (Exception ex)
        {
            return MayBe<int>.CreateError(ex);
        }
    
        return result;
    }
    
    public MayBe<IEnumerable<TCategory>> GetCategoryHierarchy(TCategory parentCategory)
    {
        try
        {
            IEnumerable<TCategory> allCategories = catalogRepository.GetAllCategories();
            return allCategories;
    
        }
        catch (Exception ex)
        {
            return MayBe<int>.CreateError(ex);
        }
    
        return result;
    }
    

    One problem I see with this implementation is that exceptions are not completely immutable. That can cause problems if the same MayBe<T> throws on multiple threads. Perhaps someone can suggest a better implementation.

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

Sidebar

Related Questions

or the other way around? I use generic lists all the time. But I
We are using DynamicObject for dynamic properties creation, but then we want to use
First of all, I know you can use Computed observables. They are really great
I use a generic algorithm to write CSV files that in theory works for
I make use of generic views and I am attempting to query my MySQL
I need to understand how to use the generic Delphi 2009 TObjectList . My
I'm trying to re-use a Java generic collection I wrote, looks a lot like
I've read posts about why you can't have a (Edit -- generic) (which use
Is it possible to use django class based generic views with a ManyToManyField relation?
I want to use the DbProvider class to construct a generic DAL component. This

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.