Need advice on Class. Thank You

S

shapper

Hello,

I have an object which properties I want to have default values.
However, I would like to be able to update these values. I have
something like:

// View
public class View {
public IList<Asset> Assets { get; set; }
public String Author { get; set; }
public IList<String> Scripts { get; set; }
public String Title { get; set; }

public View() {
Assets = assetRepository.GetAssets().ToList();
Author = "Me";
Author = "My Company";
Scripts = new List<String>();
foreach (Asset a in Assets.Where(a => a.Type == "Script").OrderBy
(a => a.Priority)) Scripts.Add(a.Content);
} // View

}

Then I am using it as follows:

MyModel model = new MyModel {
View = new View {
Title = "Homepage - Welcome" }
};
model.View.Assets.Add(assetService.GetAsset("Script.TinyMce"));

As you can see I am trying to change the title default value and
adding new values to Assets.
But then I need to add this Asset to Scripts as I do in the
constructor.

I am just a little bit confused on how to implement the View class and
what methods needed.

When I change something, something else stops working.

Could someone advice me on this?

Thanks
Miguel
 
P

Peter Duniho

[...]
As you can see I am trying to change the title default value and
adding new values to Assets.
But then I need to add this Asset to Scripts as I do in the
constructor.

I am just a little bit confused on how to implement the View class and
what methods needed.

When I change something, something else stops working.

The above statement is far too vague to be of any use here.

As for the larger question, it seems to me you've got two things that need
to happen when you add an addition asset:

-- The Asset instance needs to be added _in sorted order_ to your
Assets collection
-- The Asset.Content property needs to be added to your Scripts
collection, only if the Asset.Type is "Script"

So, write a method that does that. The most difficult aspect would be the
management of the Assets collection, and even that not terribly so (you
need to implement an insertion sort, or switch to a collection that
inherently supports sorting, like SortedList<TKey, TValue> or
SortedDictionary<TKey, TValue>).

Pete
 
P

Peter Duniho

[...]
As for the larger question, it seems to me you've got two things that
need to happen when you add an addition asset:

-- The Asset instance needs to be added _in sorted order_ to your
Assets collection
-- The Asset.Content property needs to be added to your Scripts
collection, only if the Asset.Type is "Script"

Sorry, I misread the code the first time. The above isn't correct. Your
original code doesn't appear to care about the order of the Assets
collection itself (though, without a complete example it's impossible to
say for sure).

What you _do_ need to worry about is the order of your Scripts
collection. So, rather than your method implementing those two different
points, what you really need is a method that simply adds the Asset
instance to the Assets collection, and then if the Asset.Type is "Script",
adds the value of Asset.Content to your Scripts collection in sorted order
(according to the Asset.Priority value).

My other comments about the specifics of doing the sorting still apply,
just to the different collection.

Pete
 

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