Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a group of methods/properties within a class?

I am using entity framework with a web service and I have entity partial class objects that were generated automatically by the web service.

I would like to extend these classes, but I would like to group them in the generated class in a way similar to the way a namespace would (except inside a class).

Here is my generated class:

public partial class Employee : Entity
{
   public int ID { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

And I want to add some new properties, functions, etc similar to:

public partial class Employee : Entity
{
   public string FullName {
      get { return this.FirstName + " " + this.LastName; }
   }
}

However, I would like to group any additional properties together so I have a little more visible separation from the generated methods. I would like to be able to call something like:

myEmployee.CustomMethods.FullName

I could create another class within the partial class called CustomMethods and pass a reference to the base class so I can access the generated properties. Or maybe just name them a particular way. But, I am not sure what is the best solution. I am looking for community ideas that are clean and fall under good practice. Thanks.

like image 936
Adam Avatar asked Sep 01 '10 01:09

Adam


People also ask

What are properties in a class?

Properties are attributes or features that characterize classes. While classes are groups of objects, an instance is a specific object that actually belongs to a class.

What are class properties called?

We call properties to the variables inside a class. Properties can accept values like strings, integers, and booleans (true/false values), like any other variable. Let's add some properties to the Car class. We put the public keyword in front of a class property.

Why do we use get and set properties in C#?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

What does get set mean in C#?

The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. If you don't fully understand it, take a look at the example below.


2 Answers

Here is another solution using explicit interfaces:

public interface ICustomMethods {
    string FullName {get;}
}

public partial class Employee: Entity, ICustomMethods {
    public ICustomMethods CustomMethods {
       get {return (ICustomMethods)this;}
    }
    //explicitly implemented
    string ICustomMethods.FullName {
       get { return this.FirstName + " " + this.LastName; }
    }
}

Usage:

string fullName;
fullName = employee.FullName; //Compiler error    
fullName = employee.CustomMethods.FullName; //OK
like image 193
Igor Zevaka Avatar answered Nov 15 '22 12:11

Igor Zevaka


public class CustomMethods
{
    Employee _employee;
    public CustomMethods(Employee employee)
    {
        _employee = employee;
    }

    public string FullName 
    {
        get 
        {
            return string.Format("{0} {1}", 
                _employee.FirstName, _employee.LastName); 
        }
    }
}

public partial class Employee : Entity
{
    CustomMethods _customMethods;
    public CustomMethods CustomMethods
    {
        get 
        {
            if (_customMethods == null)
                _customMethods = new CustomMethods(this);
            return _customMethods;
        }
    }
}

typically I would put Properties like FullName right on the Partial class but I can see why you might want separation.

like image 30
hunter Avatar answered Nov 15 '22 11:11

hunter