0

I am designing a 3 layer framework

I would like to know if It's possible to pass attribiutes of an object to a function without declaring them explicitly ? For example If I want to pass Id,Name to personnelBL.ValidateInsert(...)

I don't want the ValidateInsert function interface look like this : ValidateInsert(Id,Name)

The reason for that is that I want to write a base abstract class to contain a ValidateInsert(...) abstract function so I will Inherit from that class in my BL Layer classes and If the ValidateInsert input parameters could be declared in a way that I could pass an object attribiutes in a general form It would really be nice .

Note: Someone might say that I can pass an object to the function using generics but I really don't want to pass an object ! I want to pass any object's attribiutes so I can Inherit that abstract base class in any entityBL classes .

I really could not explain what I want better ! Sorry for that and thanks for understanding me .

3
  • What about declaring your base method as: Validate(Dictionary<string, string> aValuesToCheck). Doesn't give you type safety but is a way to generically pass an arbitrary amount of attribute values. Commented Jan 20, 2014 at 11:24
  • @TheEdge What you say is really a nice way ! and type safty is not important because I check the types and values in DAL Layer . I will sure try what you said . But It would be really nice if there was a better way to pass any objects attribiutes ;) Commented Jan 20, 2014 at 11:27
  • Check out my answer. I offer a typed ans safe way to do this, but i'm not sure how you would implement that. Commented Jan 20, 2014 at 11:28

3 Answers 3

1

not sure that I fully understand what you want , but I think the below can help

  1. You can use reflection.You can avoid the performance issues, is you create method per class on the fly and compile it (can use compile expression tree). and add your own attribute that you put only on relevant attributes.
  2. Create an Interface, It can return dictionary of column name and their values. your abstract class will implement this interface.

hope this answer your question

Sign up to request clarification or add additional context in comments.

2 Comments

could you please provide me with a simple sample code for the second way you mentioned ?
it should be simple. could you explain more about your ValidateInsert ? what it should do ? could it receive a dictionary ?
1

I am not sure if i understand your question correctly, but are you looking for something similar to this-

public class Base<T, TFiled>
{
      public void ValidateInsert(TFiled filed)
      {

      }
}

 public class Derived : Base<Derived, long>
    {
        public long Id { get; set; }
    }

    public class AnotherDerived : Base<Derived, string>
    {
        public string IdSring { get; set; }
    }


  public class MyObject
    {
        private Derived d = new Derived();

        private AnotherDerived anotherIsntance = new AnotherDerived();

        public MyObject()
        {
            d.ValidateInsert(10);
            anotherIsntance.ValidateInsert("some string");
        }

    }

Comments

1

Well, not really.

But you can get very close to!

You can use the Expression API. It's awesome. The code I'll post here is just pseudocode but you'll get the idea. I'll not worry about syntax but I'll try the hardest I can.

public static bool ValidateInsert(params Expression<Func<object,object>>[] properties)
{

     //Here you'll do some code to get every property. You can do a foreach loop.
     //I think you will need to use reflection to get the property values

} //Change Func<Object,Object> accordingly. This represents a function that takes an object and returns another object.

This is how you can achieve the syntax, but I'm not sure about functionality. You'll need an "instance" object where you'll get the properties values from.

So, you could call it like this:

ValidadeInsert(x => x.Id, x => x.Name, x => x.Whatever)

Here you can see how to get the Getter method of a property. I think you can get the PropertyInfo from the lambda expression, but I'm not sure. You'll have to do some research and adapt it to your code, if you decide to follow this way.

Sorry about my english, but I think you understood what I meant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.