Maybe an interface?

S

shapper

Hello,

I have an attribute on a project as follows:

public class GoogleVerifyAttribute : FilterAttribute, IActionFilter
{

public void OnActionExecuting(ActionExecutingContext context) {

ViewResult view = (ViewResult)context.Result;
if (view == null) return;

ViewModel model = (ViewModel)view.ViewData.Model;
if (model == null) return;

if (model.Verify == null) model.Verify = new GoogleVerify();
model.Verify.Key = Setup.Settings.GoogleVerifyKey;

} // OnActionExecuting

} // GoogleVerifyAttribute

ViewModel is a class that I have in all my projects:

public class ViewModel {
public GoogleVerify Verify { get; set; }
// Some other properties
} // ViewModel

And all my models inherit view model:

public class SomeViewModel {
// Some properties
}

What I am trying to do is somehow to move GoogleVerifyAttribute to a
common library but when using it in each project to be able to define
the following line different ways from project to project:

model.Verify.Key = Setup.Settings.GoogleVerifyKey;

For example in other project I might have just:

model.Verify.Key = "ldsjk";

Is there a way to do this?

I was "playing" a little bit with interfaces but I got lost in the
way ...

Thanks,
Miguel
 
A

Arne Vajhøj

I have an attribute on a project as follows:

public class GoogleVerifyAttribute : FilterAttribute, IActionFilter
{

public void OnActionExecuting(ActionExecutingContext context) {

ViewResult view = (ViewResult)context.Result;
if (view == null) return;

ViewModel model = (ViewModel)view.ViewData.Model;
if (model == null) return;

if (model.Verify == null) model.Verify = new GoogleVerify();
model.Verify.Key = Setup.Settings.GoogleVerifyKey;

} // OnActionExecuting

} // GoogleVerifyAttribute

ViewModel is a class that I have in all my projects:

public class ViewModel {
public GoogleVerify Verify { get; set; }
// Some other properties
} // ViewModel

And all my models inherit view model:

public class SomeViewModel {
// Some properties
}

What I am trying to do is somehow to move GoogleVerifyAttribute to a
common library but when using it in each project to be able to define
the following line different ways from project to project:

model.Verify.Key = Setup.Settings.GoogleVerifyKey;

For example in other project I might have just:

model.Verify.Key = "ldsjk";

Is there a way to do this?

I was "playing" a little bit with interfaces but I got lost in the
way ...

IMHO the two most obvious solutions are:

1)

model.Verify.Key = GetKey();

where GetKey is an abstract method in this class that gets
implemented in sub classes.

2)

model.Verify.Key = KeyDel();

where KeyDel is a delegate that was send over as an argument
(most likely to constructor and saved in a field).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top